<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <plan-search @search="reload"> </plan-search>
      <!-- 数据表格 -->
      <!-- :default-sort="{ prop: 'createTime', order: 'descending' }" -->
      <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 || row.planStatus == 4"
            icon="el-icon-edit"
            @click="openAdd('编辑运行记录配置', row)"
          >
            编辑
          </el-link>
          <el-link
            v-if="row.cycleType == 0 || row.planStatus == 4"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openAdd('派单', row)"
          >
            派单
          </el-link>
          <el-link
            v-if="row.planStatus != 2 && row.planStatus != 3"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="handleWithdraw(row)"
          >
            撤回
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 新建或编辑弹窗 -->
    <!-- <AddPatrolPlanDialog ref="addPatrolPlanDialogRef" @refreshList="reload" /> -->
    <programRulesDialog
      ref="programRulesDialog"
      :visible.sync="addProgramRulesDialog"
      :dialogTitle="dialogTitle"
      :isBindPlan="isBindPlan"
      @done="reload"
    />
    <dispatchDialog ref="dispatchDialogRef" @refreshList="reload" />
  </div>
</template>

<script>
  import ProgramRulesDialog from '../../components/programRulesDialog.vue';
  import AddPatrolPlanDialog from '@/components/addPatrolPlanDialog/index.vue';
  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,
      ProgramRulesDialog
    },
    data() {
      return {
        addProgramRulesDialog: false,
        isBindPlan: false,
        dialogTitle: '',
        // 表格列配置
        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: 200
          },
          {
            prop: 'planName',
            label: '计划名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '计划规则',
            align: 'center',
            showOverflowTooltip: true,
            slot: 'enable',
            minWidth: 200
          },
          {
            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.planStatus];
            }
          },
          {
            prop: 'approvalUserName',
            label: '审批人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'createTime',
            label: '生成时间',
            align: 'center',
            // sortable: true,
            showOverflowTooltip: true,
            minWidth: 150,
            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
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return getPage({ pageNum: page, size: limit, ...where, planType: 6 });
      },
      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.addProgramRulesDialog = false;
        this.$refs.table.reload({ page: 1, where });
      },
      handleWithdraw(row) {
        // 撤回
        this.$confirm(`确认撤回?`, '提示').then(async () => {
          revocation(row.id)
            .then(() => {
              this.$message.success('撤回成功！');
              this.reload();
            })
            .catch((err) => {
              this.$message.success(err.message || '撤回失败！');
            });
        });
      },

      openAdd(dialogTitle, row) {
        // this.$refs.addPatrolPlanDialogRef.open(dialogTitle, row);
        this.isBindPlan = false;
        this.addProgramRulesDialog = true;
        this.dialogTitle = dialogTitle;
        this.$nextTick(() => {
          this.$refs.programRulesDialog.init(row, '运行记录');
        });
      },
      goDetail({ id }) {
        this.$router.push({
          path: '/maintenance/runRecord/plan/details',
          query: {
            id
          }
        });
      }
    }
  };
</script>

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