<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <patrol-search @search="reload"> </patrol-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="openEdit()"
          >
            新建
          </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
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此巡检点配置吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 新建或编辑弹窗 -->
    <AddPatrolConfigDialog
      ref="addPatrolConfigDialogRef"
      :dialogTitle="dialogTitle"
      :isBindPlan="isBindPlan"
      @done="reload"
    />
  </div>
</template>

<script>
  import AddPatrolConfigDialog from '@/components/addPatrolConfigDialog';
  import PatrolSearch from './components/patrol-search.vue';
  import { planConfigPage, removeRule } from '@/api/ruleManagement/plan';
  import dictMixins from '@/mixins/dictMixins';
  export default {
    mixins: [dictMixins],
    components: {
      PatrolSearch,
      AddPatrolConfigDialog
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '计划配置单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            slot: 'code'
          },
          {
            prop: 'name',
            label: '计划配置名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'groupName',
            label: '巡点检部门',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'categoryName',
            label: '设备分类',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'ruleName',
            label: '规则名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'autoOrder',
            label: '自动派单',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              let autoOrder =
                _row.autoOrder == 1 ? '是' : _row.autoOrder == 0 ? '否' : '-';
              return autoOrder;
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110,
            formatter: (_row, _column, cellValue) => {
              return this.getDictValue('规则状态', _row.status);
            }
          },
          {
            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: 150,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 加载状态
        loading: false,
        pageType: 'add',
        dialogTitle: '',
        isBindPlan: false
      };
    },
    computed: {},
    created() {
      this.requestDict('规则状态');
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return planConfigPage({
          pageNum: page,
          size: limit,
          ...where,
          groupId: where.groupId == null ? '' : where.groupId,
          categoryLevelId:
            where.categoryLevelId == null ? '' : where.categoryLevelId,
          ruleType: 1
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where, ruleType: 1 });
      },

      openEdit(row) {
        this.isBindPlan = false;
        this.$refs.addPatrolConfigDialogRef.addPatrolConfigDialog = true;
        if (row) {
          this.dialogTitle = '编辑巡点检计划配置';
        } else {
          this.dialogTitle = '新增巡点检计划配置';
        }
        this.$refs.addPatrolConfigDialogRef.init(row, '巡点检');
      },

      remove(row) {
        removeRule([row.id]).then((res) => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },

      goDetail({ id }) {
        this.$router.push({
          path: '/ruleConfiguration/inspectionPoint/detail',
          query: {
            id
          }
        });
      }
    }
  };
</script>

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