<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="条目描述" prop="itemDesc">
        <el-input
          v-model="queryParams.itemDesc"
          placeholder="请输入条目描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-select v-model="queryParams.riskLevel" placeholder="请选择风险等级" clearable>
          <el-option label="低风险" :value="1" />
          <el-option label="一般风险" :value="2" />
          <el-option label="较大风险" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:safePenetration:verticalCheckCfg:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:safePenetration:verticalCheckCfg:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:safePenetration:verticalCheckCfg:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:safePenetration:verticalCheckCfg:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="80" />
      <el-table-column label="父级" align="center" prop="parentId" width="80" />
      <el-table-column label="条目类型" align="center" prop="itemType" width="100">
        <template #default="scope">
          <span v-if="scope.row.itemType === 0">条目</span>
          <span v-else-if="scope.row.itemType === 1">分类</span>
          <span v-else-if="scope.row.itemType === 2">事故</span>
        </template>
      </el-table-column>
      <el-table-column label="条目描述" align="center" prop="itemDesc" show-overflow-tooltip />
      <el-table-column label="外部ID" align="center" prop="itemKey" width="100" />
      <el-table-column label="风险因素" align="center" prop="riskFactor" show-overflow-tooltip />
      <el-table-column label="风险等级" align="center" prop="riskLevel" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.riskLevel === 1">低风险</el-tag>
          <el-tag v-else-if="scope.row.riskLevel === 2" type="warning">一般风险</el-tag>
          <el-tag v-else-if="scope.row.riskLevel === 3" type="danger">较大风险</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="控制措施" align="center" prop="controlMeasure" show-overflow-tooltip />
      <el-table-column label="排序" align="center" prop="sorted" width="60" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:safePenetration:verticalCheckCfg:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:safePenetration:verticalCheckCfg:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="父级" prop="parentId">
              <el-input-number v-model="form.parentId" :min="0" placeholder="请输入父级ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条目类型" prop="itemType">
              <el-select v-model="form.itemType" placeholder="请选择条目类型">
                <el-option label="条目" :value="0" />
                <el-option label="分类" :value="1" />
                <el-option label="事故" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="条目描述" prop="itemDesc">
          <el-input v-model="form.itemDesc" placeholder="请输入条目描述" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="外部ID" prop="itemKey">
              <el-input v-model="form.itemKey" placeholder="请输入外部ID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sorted">
              <el-input-number v-model="form.sorted" :min="0" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="风险因素" prop="riskFactor">
          <el-input v-model="form.riskFactor" type="textarea" placeholder="请输入风险因素" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="风险等级" prop="riskLevel">
              <el-select v-model="form.riskLevel" placeholder="请选择风险等级">
                <el-option label="低风险" :value="1" />
                <el-option label="一般风险" :value="2" />
                <el-option label="较大风险" :value="3" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="控制措施" prop="controlMeasure">
          <el-input v-model="form.controlMeasure" type="textarea" placeholder="请输入控制措施" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="VerticalCheckCfg">
import { listVerticalCheckCfg, getVerticalCheckCfg, delVerticalCheckCfg, addVerticalCheckCfg, updateVerticalCheckCfg } from "@/api/manage/verticalCheckCfg";

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {
    itemCatetory: 2,
    itemType: 0
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemDesc: null,
    riskLevel: null,
    itemCatetory: 2
  },
  rules: {
    itemDesc: [{ required: true, message: "条目描述不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listVerticalCheckCfg(queryParams.value).then(response => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = {
    id: null,
    parentId: 0,
    itemType: 0,
    itemDesc: null,
    itemCatetory: 2,
    itemKey: null,
    riskFactor: null,
    riskLevel: null,
    controlMeasure: null,
    sorted: 0
  };
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加纵向检查配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getVerticalCheckCfg(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改纵向检查配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateVerticalCheckCfg(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVerticalCheckCfg(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除编号为"' + _ids + '"的数据项？').then(function() {
    return delVerticalCheckCfg(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/verticalCheckCfg/export', {
    ...queryParams.value
  }, `verticalCheckCfg_${new Date().getTime()}.xlsx`)
}

getList();
</script>
