<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <plan-search @search="reload"> </plan-search>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="systemRoleTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openAdd('新增巡点检计划')"
          >
            新建临时计划
          </el-button>
        </template>
        <!-- 	     <template v-slot:enable="{ row }">
	       <el-switch
	         v-model="row.enable"
	         active-color="#13ce66"
	         inactive-color="#ff4949"
	         :active-value="1"
	         :inactive-value="0"
	         @change="changeEnable(row)"
	       >
	       </el-switch>
	     </template> -->
        <template v-slot:planCode="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            {{ row.planCode }}
          </el-link>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            v-if="row.cycleType === 0"
            icon="el-icon-edit"
            @click="openAdd('编辑巡点检计划', row)"
          >
            编辑
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openDispatch(row)"
          >
            派单
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="handleWithdraw(row)"
          >
            撤回
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 新建或编辑弹窗 -->
    <AddPatrolPlanDialog ref="addPatrolPlanDialogRef" @refreshList="reload" />
    <dispatchDialog ref="dispatchDialogRef" @refreshList="reload" />
  </div>
</template>

<script>
  import AddPatrolPlanDialog from '@/components/addPatrolPlanDialog';
  import DispatchDialog from '@/components/addPatrolPlanDialog/DispatchDialog';
  import PlanSearch from './components/plan-search.vue';
  import { getPage, revocation } from '@/api/maintenance/patrol_maintenance';
  export default {
    components: {
      PlanSearch,
      DispatchDialog,
      AddPatrolPlanDialog
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            columnKey: 'planCode',
            slot: 'planCode',
            prop: 'planCode',
            label: '计划单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planName',
            label: '计划名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '计划规则',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'enable',
            minWidth: 110
          },
          {
            prop: 'categoryLevelName',
            label: '设备分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'cycleType',
            label: '计划性质',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter(item) {
              return { 1: '自动', 0: '手动' }[item.cycleType];
            }
          },
          {
            prop: 'planStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter(item) {
              return {
                0: '待审核',
                1: '已派单',
                2: '执行中',
                3: '已完成',
                4: '已撤回',
                5: '已驳回'
              }[item.cycleType];
            }
          },
          {
            prop: 'approvalUserName',
            label: '审批人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '生成时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 230,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        pageType: 'add',
        isBindPlan: false
      };
    },
    computed: {},
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getPage({ pageNum: page, size: limit, ...where, planType: 4 });
      },
      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 });
      },
      handleWithdraw(row) {
        // 撤回

        this.$confirm(`确认撤回计划${row.name}?`, '提示').then(async () => {
          revocation(row.id)
            .then(() => {
              this.$message.success('撤回成功！');
              this.reload();
            })
            .catch((err) => {
              this.$message.success(err.message || '撤回失败！');
            });
        });
      },

      openDispatch(row) {
        this.$refs.dispatchDialogRef.open(row);
      },
      openAdd(dialogTitle, row) {
        this.$refs.addPatrolPlanDialogRef.open(dialogTitle, row);
      },

      goDetail({ id }) {
        this.$router.push({
          path: '/maintenance/patrol/plan/details',
          query: {
            id
          }
        });
      }
    }
  };
</script>

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