<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <work-search @search="reload"> </work-search>
      <!-- 数据表格 -->
      <!-- :default-sort="{ prop: 'createTime', order: 'descending' }" -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="patrolOrderTable"
      >
        <template v-slot:code="{ row }">
          <el-link type="primary" @click="goDetail(row)">
            {{ row.code }}
          </el-link>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <jimureportBrowse
            :businessId="row.id"
            businessCode="eampatrolinspectionprint"
            v-if="row.orderStatus==3"
          ></jimureportBrowse>
          <el-link
            v-if="row.orderStatus !== 3 && row.orderStatus !== 4"
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            @click="addSpareItems(row)"
          >
            申请备品备件
          </el-link>
          <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 && row.isGroup != 1
            "
            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" />
    <edit @refresh="reload" ref="edit" />
  </div>
</template>

<script>
  import edit from '@/views/sparePartsApply/components/edit';
  import WorkSearch from './components/work-search.vue';
  import redeployOther from '@/views/maintenance/components/redeployOther.vue';
  import signingUpWork from '@/views/maintenance/components/signingUpWork.vue';
  import {
    getWorkOrderPage,
    getWordOrderDetail
  } from '@/api/maintenance/patrol_maintenance';
  import jimureportBrowse from '@/components/jimureport/browseModal.vue';

  import dictMixins from '@/mixins/dictMixins';

  import { getToken } from '@/utils/token-util';
  export default {
    components: {
      WorkSearch,
      redeployOther,
      signingUpWork,
      jimureportBrowse,
      edit
    },
    mixins: [dictMixins],
    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,
            minWidth: 140
          },
          {
            prop: 'planName',
            label: '巡点检名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'executeGroupName',
            label: '巡点检部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120,
            formatter: (row) => {
              if (row.executeUserType == 0) {
                return row.executeUsers.map((i) => i.groupName).join(',');
              }
              return '';
            }
          },
          {
            prop: 'executeUserName',
            label: '巡点检人员',
            align: 'center',
            showOverflowTooltip: true,
            formatter: (row) => {
              if (row.executeUserType == 0) {
                return row.executeUsers.map((i) => i.userName).join(',');
              }
              return '';
            }
          },
          {
            prop: 'executeUsers',
            label: '班组',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (row) => {
              if (row.executeUserType == 1) {
                return row.executeUsers.map((i) => i.teamName).join(',');
              }
              return '';
            }
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'createTime',
            label: '工单生成时间',
            align: 'center',
            // sortable: true,
            showOverflowTooltip: true,
            width: 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: 'acceptanceStatus',
            label: '执行结果',
            align: 'center',
            showOverflowTooltip: true,
            // formatter(row) {
            //   return { 0: '缺陷', 1: '正常' }[row.isAbnormal];
            // }
            formatter(row) {
              return { 0: '异常', 1: '正常', 2: '异常', 3: '待检' }[
                row.isAbnormal
              ];
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 240,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        pageType: 'add',
        dialogTitle: '',
        isBindPlan: false
      };
    },
    created() {
      this.requestDict('巡点检工单状态');
    },
    computed: {},
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getWorkOrderPage({
          pageNum: page,
          size: limit,
          ...where,
          type: 1
        });
      },
      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 });
      },

      // 添加备品备件
      async addSpareItems(row) {
        let data = await getWordOrderDetail(row.id);
        data.deviceList = data.deviceList.map((item) => {
          return item.substance;
        });

        this.$refs.edit.open(data, 'add');
      },
      goDetail({ id, executeUserType }) {
        this.$router.push({
          path: '/maintenance/patrol/workOrder/details',
          query: {
            id,
            executeUserType
          }
        });
      },
      // 转派
      toRedeploy(row) {
        this.$refs.redeployOtherRef.open(row);
      },
      // 报工
      toSigningUpWork(row) {
        this.$refs.signingUpWorkRef.open(row);
      },
      handleExport(row) {
        const url = `http://192.168.120.128:8085/jmreport/view/1060046036862939136?token=${getToken()}&id=${
          row.id
        }`;
        window.open(url, '_blank');
      }
    }
  };
</script>

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