<template>
  <div class="app-container" ref="appPanel">
    <div ref="toolPanel">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:bound:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button 
            icon="el-icon-search" 
            size="mini" 
            @click="handleQuery"
          >搜索</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button 
            icon="el-icon-refresh"
            size="mini" 
            @click="resetQuery"
            >重置</el-button>
        </el-col>
        <right-toolbar v-if="rightToolbarShow" :showSearch.sync="showSearch" @queryTable="getList" showColumnsType="transfer" :columns="columns" :businessCode="businessCodeTrk" :tableNo="tableNo" ></right-toolbar>
      </el-row>
    </div>
    <div class="search-container" ref="searchPanel">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="来源单号" prop="deliveryId">
                    <el-input
                        v-model="queryParams.deliveryId"
                        placeholder="请输入来源单号"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="申请时间" prop="applyTime">
                    <el-date-picker clearable
                                    v-model="queryParams.applyTime"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="请选择申请时间">
                    </el-date-picker>
                  </el-form-item>
                  <el-form-item label="申请人" prop="applyer">
                    <el-input
                        v-model="queryParams.applyer"
                        placeholder="请输入申请人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="第二审批人" prop="managerName">
                    <el-input
                        v-model="queryParams.managerName"
                        placeholder="请输入第二审批人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="第一审批人" prop="deptmanager">
                    <el-input
                        v-model="queryParams.deptmanager"
                        placeholder="请输入第一审批人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
                  <el-form-item label="第三审批人" prop="crossDeptManager">
                    <el-input
                        v-model="queryParams.crossDeptManager"
                        placeholder="请输入第三审批人"
                        clearable
                        @keyup.enter.native="handleQuery"
                    />
                  </el-form-item>
    </el-form>
    </div>
    <div class="table-container">
      <div ref="mainTablePanel" :style="{height: mainTableDivHeight}">
        <el-table v-loading="loading" border size="mini" :height="mainTableHeight" :data="boundList" 
        @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="主键" align="center" prop="id" v-if="columns[0].visible"/>
              <el-table-column label="来源单号" align="center" prop="deliveryId" v-if="columns[1].visible"/>
              <el-table-column label="物料列表" align="center" prop="itemList" v-if="columns[2].visible">
              <template slot-scope="scope">
                <el-tooltip class="item" effect="dark" :content="scope.row.itemList" placement="top">
                  <span>{{ truncateText(scope.row.itemList) }}</span>
                </el-tooltip>
              </template>
            </el-table-column>
              <el-table-column label="出库类型" align="center" prop="ckType" v-if="columns[3].visible"/>
              <el-table-column label="申请时间" align="center" prop="applyTime" width="180" v-if="columns[4].visible">
                <template slot-scope="scope">
                  <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="申请人" align="center" prop="applyer" v-if="columns[5].visible"/>
              <el-table-column label="第二审批人" align="center" prop="managerName" v-if="columns[6].visible"/>
              <el-table-column label="第一审批人" align="center" prop="deptmanager" v-if="columns[7].visible"/>
              <el-table-column label="第三审批人" align="center" prop="crossDeptManager" v-if="columns[8].visible"/>
              <el-table-column label="钉钉实例ID" align="center" prop="instanceId" v-if="columns[9].visible"/>
              <el-table-column label="后台任务ID" align="center" prop="taskId" v-if="columns[10].visible"/>
              <el-table-column label="后台实例ID" align="center" prop="fInstanceId" v-if="columns[11].visible"/>
              <el-table-column label="当前审批人" align="center" prop="finishName" v-if="columns[14].visible"/>
        </el-table>
        <div class="pagePanel">
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        </div>
      </div>

    </div>

    <!-- 添加或修改审核单对话框 -->
    <el-dialog id="dialog-container" :title="title" :visible.sync="open" width="1000px" append-to-body>
      <div class="edit-container">
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
                        <el-row>
                      <el-col :span="6">
                        <el-form-item label="来源单号" prop="deliveryId">
                          <el-input v-model="form.deliveryId" placeholder="请输入来源单号" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="物料列表" prop="itemList">
                          <el-input v-model="form.itemList" type="textarea" placeholder="请输入内容" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="申请时间" prop="applyTime">
                          <el-date-picker clearable
                                          v-model="form.applyTime"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择申请时间">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="6">
                        <el-form-item label="申请人" prop="applyer">
                          <el-input v-model="form.applyer" placeholder="请输入申请人" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="第二审批人" prop="managerName">
                          <el-input v-model="form.managerName" placeholder="请输入第二审批人" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="第一审批人" prop="deptmanager">
                          <el-input v-model="form.deptmanager" placeholder="请输入第一审批人" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="第三审批人" prop="crossDeptManager">
                          <el-input v-model="form.crossDeptManager" placeholder="请输入第三审批人" />
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
                      <el-col :span="6">
                        <el-form-item label="钉钉实例ID" prop="instanceId">
                          <el-input v-model="form.instanceId" placeholder="请输入钉钉实例ID" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="后台任务ID" prop="taskId">
                          <el-input v-model="form.taskId" placeholder="请输入后台任务ID" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="后台实例ID" prop="fInstanceId">
                          <el-input v-model="form.fInstanceId" placeholder="请输入后台实例ID" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="6">
                        <el-form-item label="当前审批人" prop="finishName">
                          <el-input v-model="form.finishName" placeholder="请输入当前审批人" />
                        </el-form-item>
                      </el-col>
                        </el-row>
                        <el-row>
          </el-row>
      </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listBound, getBound, delBound, addBound, updateBound, disabled, enabled, changeStatus } from "@/api/system/bound";
  import { listCOLUMN } from "@/api/system/column";

  export default {
    name: "Bound",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 审核单表格数据
              boundList: [],
        // 弹出层标题
        title: "",
        mainTableHeight:"0px",
        mainTableDivHeight:"0px",
        businessCodeTrk: "bound",
        rightToolbarShow: false,
        tableNo: 0,
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 100,
          deliveryId: null,
          applyTime: null,
          applyer: null,
          managerName: null,
          deptmanager: null,
          crossDeptManager: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        itemList: [
                    { required: true, message: "物料列表不能为空", trigger: "blur" }
                  ],
        },
        columns: [
            { key: 0, label: `主键`, visible: true, columnName: "id" },
            { key: 1, label: `来源单号`, visible: true, columnName: "delivery_id" },
            { key: 2, label: `物料列表`, visible: true, columnName: "item_list" },
            { key: 3, label: `出库类型`, visible: true, columnName: "ck_type" },
            { key: 4, label: `申请时间`, visible: true, columnName: "apply_time" },
            { key: 5, label: `申请人`, visible: true, columnName: "applyer" },
            { key: 6, label: `第二审批人`, visible: true, columnName: "manager_name" },
            { key: 7, label: `第一审批人`, visible: true, columnName: "deptmanager" },
            { key: 8, label: `第三审批人`, visible: true, columnName: "cross_dept_manager" },
            { key: 9, label: `钉钉实例ID`, visible: true, columnName: "instance_id" },
            { key: 10, label: `后台任务ID`, visible: true, columnName: "task_id" },
            { key: 11, label: `后台实例ID`, visible: true, columnName: "f_instance_id" },
            { key: 12, label: `创建时间`, visible: true, columnName: "create_time" },
            { key: 13, label: `更新时间`, visible: true, columnName: "update_time" },
            { key: 14, label: `当前审批人`, visible: true, columnName: "finish_name" },
        ]
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.computedTableHeight();
    },
    watch:{
      'showSearch': function() {
        this.computedTableHeight();
      }
    },
    methods: {
      truncateText(text) {
      const maxLength = 20;  // 控制文本的最大显示长度
      if (text.length > maxLength) {
        return text.slice(0, maxLength) + '...';
      }
      return text;
    },
      /** 查询用户展示字段定义管理列表 */
      getHideColumn() {
        var queryHideColumnParams = {
          pageNum: 1,
          pageSize: 100,
            businessCode: this.businessCodeTrk,
            tableNo: this.tableNo
        };
        listCOLUMN(queryHideColumnParams).then(response => {
          if(response.rows.length > 0) {
            var row = response.rows[0];
            var columnHide = row.columnHide.split(",");
            if (columnHide.length > 0) {
              for (let item in this.columns) {
                if (columnHide.indexOf(this.columns[item].columnName) > -1) {
                  this.columns[item].visible = false;
                }
              }
            }
          }
          this.rightToolbarShow = true;
        });
      },
      computedTableHeight(){
        this.$nextTick(() => {
            var totalHeight = this.$refs.appPanel.clientHeight;
            var toolHeight = this.$refs.toolPanel.clientHeight;
            var searchHeight = this.$refs.searchPanel.clientHeight;

            var tablePanelHeight = totalHeight - toolHeight -searchHeight - 14 - 5;

            this.mainTableDivHeight = tablePanelHeight + "px";
            this.mainTableHeight = (tablePanelHeight - 25) + "px";
            
          })
      },
      /** 查询审核单列表 */
      getList() {
        this.loading = true;
        listBound(this.queryParams).then(response => {
          this.boundList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.getHideColumn();
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                        id: null,
                        deliveryId: null,
                        itemList: null,
                        ckType: null,
                        applyTime: null,
                        applyer: null,
                        managerName: null,
                        deptmanager: null,
                        crossDeptManager: null,
                        instanceId: null,
                        taskId: null,
                        fInstanceId: null,
                        createTime: null,
                        updateTime: null,
                        finishName: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加审核单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getBound(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改审核单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateBound(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBound(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除审核单编号为"' + ids + '"的数据项？').then(function() {
          return delBound(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },

      /** 用户状态修改 */
      handleStatusChange (row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '"id为"' + row.id + '"数据项？').then(function () {
          return changeStatus(row.id, row.status);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      /** 启用按钮操作 */
      handleEnable (row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认启用审核单编号为"' + ids + '"的数据项？').then(function () {
          return enabled(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("启用成功");
        }).catch(() => { });
      },
      /** 禁用按钮操作 */
      handleDisabled (row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认禁用审核单编号为"' + ids + '"的数据项？').then(function () {
          return disabled(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("禁用成功");
        }).catch(() => { });
      },

  /** 导出按钮操作 */
  handleExport() {
    this.download('system/bound/export', {
      ...this.queryParams
    }, `bound_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
<style scoped>
 .app-container {
  height: calc(100vh - 84px);
 }
</style>