<!--version: 3, 选择弹窗-->
<!--版本更新内容：添加prop属性，修复多选-->

<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small"  v-show="showSearch" :inline="true" label-width="5em">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-checkbox-group v-model="queryParams.orderStatusList" @change="handleQuery">
          <el-checkbox-button v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.value">
            {{dict.label}}
          </el-checkbox-button>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="产品名称" prop="orderProdName">
        <el-input
          v-model="queryParams.orderProdName"
          placeholder="请输入订单产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产车间" prop="deptId">
        <dept-tree-select v-model="queryParams.deptId" class="small-tree-select" @change="handleQuery"/>
      </el-form-item>
      <el-form-item label="负责工序" prop="deptProcess">
        <el-input
          v-model="queryParams.deptProcess"
          placeholder="请输入负责工序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否最终" prop="isEnd">
        <el-radio-group v-model="queryParams.isEnd" @change="handleQuery">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="处理方式" prop="handleWay">
        <el-input
          v-model="queryParams.handleWay"
          placeholder="请输入处理方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="效果" prop="effect">
        <el-input
          v-model="queryParams.effect"
          placeholder="请输入效果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盖子颜色" prop="coverColor">
        <el-input
          v-model="queryParams.coverColor"
          placeholder="请输入盖子颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      ref="multipleTable"
      :data="tableData"
      v-loading="loadTable"
      @row-click="changeSelection"
      @row-dblclick="select"
      @select-all="selectionAll"
      @select="changeSelection"
      highlight-current-row
    >
      <el-table-column align="center" type="selection" v-if="multiple"/>
      <el-table-column label="#" type="index" align="center"/>
      <template v-for="column of showColumns">
        <el-table-column
          :key="column.key"
          :label="column.label"
          :prop="column.key"
          :align="column.align"
          :min-width="column.minWidth"
          :sort-orders="orderSorts"
          :sortable="column.sortable"
          :show-overflow-tooltip="column.overflow"
          :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'isEnd'">
              <boolean-tag :value="d.row.isEnd"/>
            </template>
            <template v-else-if="column.key === 'orderStatus'">
              <dict-tag :value="d.row.orderStatus" :options="dict.type.order_status"/>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import DeptTreeSelect from '@/components/Business/Dept/DeptTreeSelect.vue'
import BooleanTag from '@/components/BooleanTag/index.vue'
import { $showColumns } from '@/utils/mixins'
import { listProdProcess } from '@/api/bst/prodProcess'

const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "ProcessCheck",
  mixins: [$showColumns],
  dicts: ['order_status'],
  components: {BooleanTag, DeptTreeSelect},
  props: {
    // 标题
    title: {
      type: String,
      default: `选择工序`
    },
    value: {
      type: [String, Number, Array],
      default: null,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
    show: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default: () => ({})
    },
    initSelect: {
      type: Array,
      default: null,
    },
    prop: {
      type: String,
      default: 'id'
    },
    listApi: {
      type: Function,
      default: listProdProcess
    }
  },
  data() {
    return {
      showSearch: true,
      loadTable: false,
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        orderStatusList: [],
      },
      total: 0,
      row: null,
      selected: [],
      // 字段列表
      columns: [
        {key: 'id', visible: true, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: "80"},
        {key: 'orderNo', visible: true, label: '订单编号', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'orderStatus', visible: true, label: '订单状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'orderProdName', visible: true, label: '产品名称', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'deptName', visible: true, label: '生产车间', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'deptProcess', visible: true, label: '负责工序', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'num', visible: true, label: '数量', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'isEnd', visible: true, label: '是否最终工序', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'handleWay', visible: true, label: '处理方式', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'effect', visible: true, label: '效果', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'color', visible: true, label: '颜色', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'coverColor', visible: true, label: '盖子颜色', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'remark', visible: true, label: '备注', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
    }
  },
  created() {
      this.queryParams = {
        ...this.queryParams,
        ...this.query,
      }

      if (this.initSelect != null) {
        this.selected = deepClone(this.initSelect);
      } else {
        this.selected = [];
      }

      this.getList();
  },
  methods: {
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 获取数据列表
    getList() {
      this.loadTable = true;
      this.listApi(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        // 刷新表格状态
        this.$nextTick(()=>{
          this.refreshTableSelection();
        })
      }).finally(() =>{
        this.loadTable = false;
      })
    },
    // 刷新表格的选中状态
    refreshTableSelection() {
      if(this.multiple){
        this.tableData.forEach(item => {
          if (this.selected.map(j => j[this.prop]).includes(item[this.prop])) {
            this.$refs.multipleTable.toggleRowSelection(item, true);
          } else {
            this.$refs.multipleTable.toggleRowSelection(item, false);
          }
        });
      }
    },
    // 全选
    selectionAll(val){
      let flag = val.length > 0;
      this.tableData.forEach(item => {
        if (flag && !this.selected.map(i => i[this.prop]).includes(item[this.prop])){
          this.selected.push(item);
        } else if (!flag && this.selected.map(i => i[this.prop]).includes(item[this.prop])){
          this.selected = this.selected.filter(i => i[this.prop] !== item[this.prop]);
        }
      })
    },
    // 确认选中
    submit() {
      if (this.multiple) {
        this.$emit('select', this.selected);
      } else {
        this.select(this.row);
      }
    },
    // 更换某一行的选中状态
    changeSelection(row){
      if(this.multiple){
        if (this.selected.map(i => i[this.prop]).includes(row[this.prop])){
          this.$refs.multipleTable.toggleRowSelection(row, false);
          this.selected = this.selected.filter(i => i[this.prop] !== row[this.prop]);
        }else {
          this.$refs.multipleTable.toggleRowSelection(row, true);
          this.selected.push(row);
        }
      } else {
        this.selected = [row];
      }
    },
    // 点击搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 选中一行
    select(row) {
      if (!this.multiple) {
        if (!row) return this.$message.error('请选择一行');
        this.selected = [row];
        this.$emit('select', row);
      }
    },
  },
}
</script>
