<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <work-search @search="reload"> </work-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable"
        :default-sort="{ prop: 'createTime', order: 'ascending' }"
      >
        <!-- 表头工具栏 -->
        <!-- <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            class="ele-btn-icon"
            @click="goDetail()"
          >
            详情
          </el-button>
        </template> -->
        <template v-slot:code="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.code }}
          </el-link>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            v-if="row.orderStatus !== 3 && row.orderStatus !== 4"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="toSigningUpWork(row)"
          >
            报工
          </el-link>
          <el-link
            v-if="row.orderStatus !== 3 && row.orderStatus !== 4"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="toRedeploy(row)"
          >
            转派
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 转派弹窗 -->
    <redeployOther ref="redeployOtherRef" @refresh="reload" />
    <!-- 报工弹框 -->
    <signingUpWork ref="signingUpWorkRef" @refresh="reload" />
  </div>
</template>

<script>
  import WorkSearch from './components/work-search.vue';
  // import { pageRoles } from '@/api/system/role';
  import { workPage } from '@/api/equipment/work';
  import redeployOther from '@/views/maintenance/components/redeployOther.vue';
  import signingUpWork from '@/views/maintenance/components/signingUpWork.vue';
  export default {
    components: {
      WorkSearch,
      redeployOther,
      signingUpWork
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            columnKey: 'code',
            slot: 'code',
            prop: 'code',
            label: '工单单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'planCode',
            label: '计划单号',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'planCode',
            minWidth: 140
          },
          {
            prop: 'planName',
            label: '保养名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'executeGroupName',
            label: '保养部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'executeUserName',
            label: '保养人员',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'createTime',
            label: '工单生成时间',
            sortable: true,
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 170
          },
          {
            prop: 'acceptTime',
            label: '开工时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'finishTime',
            label: '报工时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            columnKey: 'orderHour',
            label: '实际工时(分钟)',
            align: 'center',
            resizable: false,
            showOverflowTooltip: true,
            minWidth: 120,
            formatter: (row) => {
              if (row.finishTime && row.acceptTime) {
                return parseInt(
                  (new Date(row.finishTime).getTime() -
                    new Date(row.acceptTime).getTime()) /
                    60000
                );
              }
            }
          },
          {
            prop: 'orderStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row) => {
              return {
                0: '待接收',
                1: '已接收',
                2: '执行中',
                3: '已完成'
              }[row.orderStatus];
            }
          },
          {
            prop: 'executeResult',
            label: '执行结果',
            align: 'center',
            showOverflowTooltip: true,
            formatter(row) {
              return { 0: '缺陷', 1: '正常' }[row.isAbnormal];
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 150,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        pageType: 'add',
        dialogTitle: '',
        isBindPlan: false
      };
    },
    computed: {},
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        where.type = 2;
        return workPage({ pageNum: page, size: limit, ...where });
      },
      async changeEnable(row) {
        const res = await putRoles(row);
        if (res.code == 0) {
          this.$message({
            type: 'success',
            message: '修改成功',
            customClass: 'ele-message-border'
          });
          this.reload();
        }
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where });
      },

      goDetail({ id }) {
        this.$router.push({
          path: '/maintenance/equipment/workOrder/details',
          query: {
            id
          }
        });
      },
      // 转派
      toRedeploy(row) {
        this.$refs.redeployOtherRef.open(row);
      },
      // 报工
      toSigningUpWork(row) {
        this.$refs.signingUpWorkRef.open(row);
      }
    }
  };
</script>

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