<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"
      >
        <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="open('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-link
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            v-if="row.approvalStatus == 0 || row.approvalStatus == 3"
            @click="open('edit', row)"
          >
            编辑
          </el-link>
          <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 == 2&&row.exceptionClose==2"
            @click="dispose(row)"
          >
            处置
          </el-link>
          <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>
      </ele-pro-table>
    </el-card>
    <Create ref="create" @refresh="reload" />
    <dispose ref="dispose" @refresh="reload" />
    
    <ele-modal :visible.sync="visible" width="20vw" append-to-body :maxable="true">
      <el-form ref="form" label-width="100px" class="create-form">
        <el-form-item
          label="审核人"
          prop="qualityInspector"
          style="margin-bottom: 20px"
        >
          <el-select
            v-model="qualityInspector"
            clearable
            style="width: 100%"
            :filterable="true"
          >
            <el-option
              v-for="item in userOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="submit()"> 确定 </el-button>
      </template></ele-modal
    >
  </div>
</template>
  
  <script>
import OrderSearch from '../components/order-search.vue';
import dictMixins from '@/mixins/dictMixins';
import Create from '../components/create';
import dispose from '../components/dispose';

import { getList, remove,submit } from '@/api/exceptionManagement/index';
import { listAllUserBind } from '@/api/bpm/index';
import { btnList } from '../components/transition';

export default {
  components: {
    OrderSearch,
    Create,dispose
  },
  mixins: [dictMixins],
  data() {
    return {
      visible: false,
      loading: false,
      releasParams: {
        teamId: '',
        id: ''
      },
      selection:[],
      userOptions: [],
      viewId: '',
      qualityInspector: ''
    };
  },
  computed: {
    // 表格列配置
    columns() {
      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: 'type',
          label: '类型',
          align: 'center',
          slot: 'type'
        },
        {
          prop: 'code',
          label: '编码',
          align: 'center',
          slot: 'code'
        },
        {
          prop: 'name',
          label: '名称',
          align: 'center'
        },
        {
          prop: 'describes',
          label: '异常描述',
          align: 'center'
        },
        {
          prop: 'disposalStatus',
          label: '处置类型',
          align: 'center',
          formatter: (row, column, cellValue) => {
            return btnList.find(item=>item.type==cellValue)?.name
          }
        },
        {
          prop: 'relationType',
          label: '关联类型',
          align: 'center',
          formatter: (row, column, cellValue) => {
            return cellValue == 1
              ? '生产计划'
              : cellValue == 2
              ? '生产订单'
              : '生产工单';
          }
        },
        {
          prop: 'relationCode',
          label: '关联编码',
          align: 'center'
        },
        // {
        //   prop: 'a',
        //   label: '关联名称',
        //   align: 'center'
        // },

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

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

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

        {
          columnKey: 'action',
          label: '操作',
          width: 240,
          align: 'center',
          resizable: false,
          fixed: 'right',
          slot: 'action',
          showOverflowTooltip: true
        }
      ];
    }
  },
  created() {
    this.requestDict('异常类型');
    this.userOptions = [];
    listAllUserBind().then((data) => {
      this.userOptions.push(...data);
    });
  },
  filters: {},
  methods: {
    //处置
    dispose(row) {
      this.$refs.dispose.open(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();
      });
    },
    
    submit(id) {
      if(id){
        this.visible=true
        this.viewId=id
        return;
      }
      if (!this.qualityInspector) {
        this.$message.error('审核人不能为空');
        return;
      }
      submit({businessId: this.viewId,approvalUserId:this.qualityInspector}).then((res) => {
        this.$message.success('提交' + res.message);
        this.reload();
        this.visible=false
      });
      
    },
    /* 刷新表格 */
    reload(where) {
      this.$nextTick(() => {
        this.$refs.table.reload({ page: 1, where });
      });
    }
  }
};
</script>
  
  <style lang="scss" scoped></style>