<template>
  <div>
    <seek-page :seekList="seekList" @search="search"></seek-page>
    <ele-pro-table
      ref="table"
      row-key="id"
      :columns="columns"
      :datasource="datasource"
      :cache-key="cacheKeyUrl"
      autoAmendPage
      height="calc(86vh - 230px)"
    >
      <template v-slot:action="{ row }">
        <el-link type="primary" :underline="false" @click="goToDetail(row)">
          详情
        </el-link>
      </template>

      <template v-slot:code="{ row }">
        <el-link type="primary" @click="goToDetail(row)">{{
          row.code
        }}</el-link>
      </template>
    </ele-pro-table>
  </div>
</template>

<script>
  import dictMixins from '@/mixins/dictMixins';
  import tableColumnsMixin from '@/mixins/tableColumnsMixin';
  import { batchRecordPage } from '@/api/eam/index.js';

  export default {
    mixins: [dictMixins, tableColumnsMixin],
    props: {
      tableQuery: {
        type: Object,
        default: () => {
          return {};
        }
      }
    },
    data() {
      return {
        columns: [
          {
            width: 50,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            label: '序号'
          },
          {
            prop: 'code',
            label: '工单单号',
            align: 'center',
            minWidth: 110,
            showOverflowTooltip: true,
            slot: 'code'
          },
          {
            prop: 'planCode',
            label: '计划单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'planName',
            label: '名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'workOrderCode',
            label: '关联生产工单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            prop: 'finishTime',
            label: '报工时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 150
          },
          {
            label: '状态',
            minWidth: 180,
            prop: 'orderStatus',
            formatter: (row) => {
              switch (row.orderStatus) {
                case 0:
                  return '待接收';
                case 1:
                  return '已接收';
                case 2:
                  return '执行中';
                case 3:
                  return '已完成';
                default:
                  return '';
              }
            }
          },
          {
            prop: 'isAbnormal',
            label: '执行结果',
            align: 'center',
            showOverflowTooltip: true,
            formatter(row) {
              return { 0: '异常', 1: '正常', 2: '异常', 3: '待检' }[
                row.isAbnormal
              ];
            }
          },
          {
            label: '操作',
            columnKey: 'action',
            slot: 'action',
            showOverflowTooltip: true,
            minWidth: 110,
            align: 'center',
            fixed: 'right'
          }
        ],
        cacheKeyUrl: 'mes-259231549-device-batchRecord-table'
      };
    },
    computed: {
      seekList() {
        return [
          {
            label: '工单单号',
            value: 'code',
            type: 'input',
            placeholder: '请输入'
          },
          // {
          //   label: '计划单号:',
          //   value: 'planCode',
          //   type: 'input',
          //   placeholder: '请输入'
          // },
          {
            label: '状态:',
            value: 'orderStatus',
            type: 'select',
            placeholder: '请选择',
            planList: [
              {
                label: '待接收',
                value: 0
              },
              {
                label: '已接收',
                value: 1
              },
              {
                label: '执行中',
                value: 2
              },
              {
                label: '已完成',
                value: 3
              }
            ]
          }
          // ,
          // {
          //   label: '保养名称:',
          //   value: 'planName',
          //   type: 'input',
          //   placeholder: '请输入'
          // }
        ];
      }
    },
    methods: {
      // 刷新表格
      reload(where = {}) {
        this.$refs.table.reload({
          where,
          ...this.tableQuery
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        // 参数
        const body = {
          ...where,
          ...order,
          pageNum: page,
          size: limit,
          types: [1, 2],
          ...this.tableQuery
        };
        return batchRecordPage(body);
      },
      search(where) {
        this.reload(where);
      },
      goToDetail(row) {
        const path = `/page-eam/maintenance/patrol/workOrder/details?id=${row.id}`;
        window.history.pushState(null, '', path);
      }
    }
  };
</script>

<style></style>
