<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <order-search @search="reload" ref="searchRef"> </order-search>
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        row-key="id"
        :page-size="20"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
      >
        <template v-slot:type="{ row }">
          <span> {{ getDictValue('异常类型', row.type + '') }}</span>
        </template>
        <template v-slot:approvalStatus="{ row }">
          <span
            v-if="row.exceptionClose == 2 || !row.exceptionClose"
            :style="{
              color:
                row.approvalStatus == 1
                  ? '#409EFF'
                  : row.approvalStatus == 2
                  ? '#67C23A'
                  : row.approvalStatus == 3
                  ? '#F56C6C'
                  : ''
            }"
          >
            {{
              row.approvalStatus == 1
                ? '审核中'
                : row.approvalStatus == 2
                ? '审核通过'
                : row.approvalStatus == 3
                ? '审核不通过'
                : row.approvalStatus == 4
                ? '已处置'
                : '未提交'
            }}
          </span>
          <span v-else>关闭</span>
        </template>
        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="dispose('detail', row)"
          >
            {{ row.code }}
          </el-link>
        </template>
        <!-- 操作列 -->
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <!-- <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="open('add')"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="remove()"
            plain
            :disabled="selection.length == 0"
          >
            删除
          </el-button>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            plain
          >
            导出
          </el-button>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-download"
            class="ele-btn-icon"
            plain
          >
            导入
          </el-button> -->
          <!-- <el-button
            size="small"
            type="primary"
            icon="el-icon-circle-plus-outline"
            class="ele-btn-icon"
            plain
            :disabled="selection.length == 0"
          >
            处置
          </el-button> -->
        </template>

        <!-- <template v-slot:action="{ row }">

          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            v-if="row.approvalStatus == 0 || row.approvalStatus == 3"
            @confirm="remove([row.id])"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
 
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-circle-plus-outline"
            v-if="(row.approvalStatus == 0 || row.approvalStatus == 3)&&(!row.exceptionClose||row.exceptionClose==2)"
            @click="submit(row.id)"
          >
            提交
          </el-link>
        </template> -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            v-if="row.approvalStatus == 0 || row.approvalStatus == 3"
            @click="dispose('edit', row)"
          >
            编辑
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            v-if="row.approvalStatus == 0 || row.approvalStatus == 3"
            @click="submit(row)"
          >
            提交
          </el-link>
        </template>
        <dispose ref="disposeRef"></dispose>
      </ele-pro-table>
    </el-card>
    <process-submit-dialog
      :isNotNeedProcess="false"
      :processSubmitDialogFlag.sync="processSubmitDialogFlag"
      v-if="processSubmitDialogFlag"
      ref="processSubmitDialogRef"
      @reload="reload"
    ></process-submit-dialog>
  </div>
</template>

<script>
  import OrderSearch from '../components/order-search.vue';
  import dictMixins from '@/mixins/dictMixins';
  // import Create from '../components/create';
  import dispose from '../components/addOrEditDialog';
  import { getList, remove, submit } from '@/api/exceptionManagement/index';
  import { relationType } from '@/enum/dict.js';
  import processSubmitDialog from '@/BIZComponents/processSubmitDialog/processSubmitDialog.vue';
  import tabMixins from '@/mixins/tableColumnsMixin';

  export default {
    components: {
      OrderSearch,
      dispose,
      processSubmitDialog
    },
    mixins: [dictMixins,tabMixins],
    data() {
      return {
        visible: false,
        loading: false,
        processSubmitDialogFlag: false,
        releasParams: {
          teamId: '',
          id: ''
        },
        selection: [],
        // userOptions: [],
        viewId: '',
        qualityInspector: '',
        cacheKeyUrl:'eos-7a45afd7-saleManage-exceptionList',
        columnsVersion:1,
      };
    },
    computed: {
      // 表格列配置
      columns() {
        // 当columnsVersion变化时会重新计算,用作更新列配置
        const version = this.columnsVersion;
        return [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },

          {
            prop: 'code',
            label: '编码',
            align: 'center',
            slot: 'code'
          },
          {
            prop: 'name',
            label: '名称',
            align: 'center'
          },
          {
            prop: 'progress',
            label: '进度',
            align: 'center',
            formatter: (row, column, cellValue) => {
              return cellValue == 1
                ? '处置中'
                : cellValue == 2
                ? '处置完毕'
                : cellValue == 3
                ? '暂时搁置'
                : cellValue == 4
                ? '关闭'
                : '未开始';
            }
          },

          // {
          //   prop: 'describes',
          //   label: '异常描述',
          //   align: 'center'
          // },
          // {
          //   prop: 'disposalStatus',
          //   label: '处置类型',
          //   align: 'center',

          // },
          {
            prop: 'relationType',
            label: '来源类型',
            align: 'center',
            formatter: (row, column, cellValue) => {
              return relationType[cellValue] || '';
            }
          },
          {
            prop: 'relationCode',
            label: '来源编码',
            align: 'center'
          },
          {
            prop: 'relationName',
            label: '来源名称',
            align: 'center'
          },

          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center'
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center'
          },

          {
            prop: 'approvalStatus',
            label: '审批状态',
            align: 'center',
            slot: 'approvalStatus'
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 140,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action',
            showOverflowTooltip: true
          }
        ];
      }
    },
    created() {
      this.requestDict('异常类型');
      // this.userOptions = [];
      // listAllUserBind().then((data) => {
      //   return;
      //   this.userOptions.push(...data);
      // });
    },
    filters: {},
    methods: {
      //处置
      dispose(type, row) {
        this.$refs.disposeRef.open(type, row);
      },
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      open(type, row) {
        this.$refs.create.open(type, row);
      },
      remove(id) {
        let ids = id || this.selection.map((item) => item.id);
        remove(ids).then((res) => {
          this.$message.success('删除' + res);
          this.reload();
        });
      },

      async submit(res) {
        this.processSubmitDialogFlag = true;

        this.$nextTick(() => {
          let params = {
            businessId: res.id,
            businessKey: 'eom_exception_approve',
            formCreateUserId: res.createUserId,
            variables: {
              businessCode: res.code,
              businessName: res.name,
              businessType: res.relationName
            }
          };
          this.$refs.processSubmitDialogRef.init(params);
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      }
    }
  };
</script>

<style lang="scss" scoped></style>
