<template>
  <div style="height:100%">
    <Drawer
      title="草稿列表"
      v-model="drawerShow"
      width="730"
      :mask-closable="false"
      class-name="yc"
    >
      <Table
        width="100%"
        :height="maxHeight"
        border
        :columns="columns"
        :data="queryData"
         ref="table" 
         style="height:100%"
      >
        <template slot-scope="{ row }" slot="name">
          {{ row.name }}
        </template>
        <template slot-scope="{ row }" slot="action">
          <Button
            type="default"
            size="small"
            style="margin-right: 5px"
            @click="edit(row)"
            >编辑</Button
          >
          <Button
            type="default"
            size="small"
            style="margin-right: 5px"
            @click="showData(row)"
            >确定</Button
          >
          <Button
            type="default"
            size="small"
            style="margin-right: 5px"
            @click="detail(row)"
            >详情</Button
          >
          <Button type="default" size="small" @click="remove(row)">删除</Button>
        </template>
      </Table>
    </Drawer>
    <Modal v-model="draftModal" title="草稿名称">
      <Input
        v-model="value"
        placeholder="请修改草稿名称"
        style="width: 250px"
      />
      <div slot="footer">
        <Button type="primary"  @click="asyncOK">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
export default {
  name: "CustomerSelect",
  props: {
    context: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      maxHeight:'100',
      value: null,
      draftModal: false,
      bill: {},
      drawerShow: false,
      templateName: "",
      queryData: [],
      currentId: null,
      columns: [
        {
          title: "草稿名称",
          key: "title",
        },
        {
          title: "创建时间",
          key: "createTime",
        },
        {
          title: "修改时间",
          key: "modifyTime",
        },
        {
          title: "制稿人",
          key: "creatorTitle",
        },
        {
          title: "操作",
          slot: "action",
          width: 228,
          align: "center",
          fixed: "right",
        },
      ],
    };
  },

  mounted() {
    this.maxHeight= window.innerHeight - this.$refs.table.$el.offsetTop - 87
  },
  methods: {
    getList() {
      GAMS.Util.invokeServer({
        path: "rbcfunc/billdraft/list",
        type: "POST",
        data: JSON.stringify({
          billDefineName:this.bill.curForm.content.billdefine,
          orgCode:nros.getUser() && nros.getUser().loginUnit
        }),
        contentType: "application/json",
      })
        .then(
          function (data, textStatus, response) {
            console.log(data, textStatus, response);
            let arr = data.rows.map((v) => {
              if (v.createTime) {
                v.createTime = v.createTime.split('T')[0]
              }
              if (v.modifyTime) {
                v.modifyTime = v.modifyTime.split('T')[0]
              }
              return v;
            });
            this.queryData = arr;
          }.bind(this)
        )
        .catch((XMLHttpRequest, textStatus, errorThrown) => {
          console.log("不行啊" + errorThrown);
        });
    },
    asyncOK() {
      if(!this.value){
        this.$Message.info('请输入草稿名称');
        return 
      }
      let obj = {
        title: this.value,
        id: this.currentId,
      };
      GAMS.Util.invokeServer({
        path: "rbcfunc/billdraft/update",
        type: "POST",
        data: JSON.stringify(obj),
        contentType: "application/json",
      })
        .then(
          function (data, textStatus, response) {
            if(data.code == 0){
              this.getList();
              this.draftModal = false;
            }else{
              data.msg && this.$Message.error(data.msg);
            }
          }.bind(this)
        )
        .catch((XMLHttpRequest, textStatus, errorThrown) => {});
    },
    showData(row) {
      let billObj = this.bill.getBuillData();
      let that = this;
      Object.keys(billObj).forEach((i) => {
          if (
            Object.prototype.toString.call(billObj[i]) ===
            "[object Array]" && billObj[i].length>0
          ) {
             that.bill.clearSubDatas(i);
          }
      })
      this.bill.getDraftDetailData(row.id);
      this.bill.refreshBillForm(this.bill.curForm.content.billdefine);
      this.drawerShow = false;
    },
    edit(row) {
      this.currentId = row.id;
      this.value = row.title;
      this.draftModal = true;
    },
    detail(row) {
      let billDefineName = this.convertOnlyReadBillDefine(this.bill.curForm.content.billdefine)
      let a = `/showDraftBillForm/${billDefineName}_R/${row.id}`;
      let routeData = GMS.routerManager.getRouter().resolve({
        path: a,
      });
      window.open(routeData.href, "_blank");
    },
    remove(row) {
      this.$Modal.confirm({
        title: "删除草稿",
        content: "<p>确定删除此草稿吗？</p>",
        onOk: () => {
          GAMS.Util.invokeServer({
            path: "rbcfunc/billdraft/remove/" + row.id,
            type: "POST",
            data: JSON.stringify({}),
            contentType: "application/json",
          })
            .then(
              function (data, textStatus, response) {
                this.getList();
              }.bind(this)
            )
            .catch((XMLHttpRequest, textStatus, errorThrown) => {
              this.$Message.info("删除失败");
            });
        },
        onCancel: () => {},
      });
    },
    handleConfirm() {
      this.drawerShow = false;
    },
    convertOnlyReadBillDefine(billDefine){
      // 解决遗留问题,接待单子的只读界面标识特殊处理
      let temp = billDefine
      if (temp) {
        switch(temp){
          case 'bill.JDHDFYZCDJD':
            temp = 'bill.JDHDFYZC';
            break;
          default:
        }
      }
      return temp
    }
  },
};
</script>

<style lang="less">
.yc{
  height:100%;
  .ivu-drawer-body{
    //overflow: hidden;
  }
  .tableClass{
    height: calc(100% - 16px);
  }
}
</style>


