<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPaginationTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字" prop="KEYWORDS">
              <el-input v-model="searchForm.KEYWORDS" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="风险分级" prop="LEVELID">
              <el-select v-model="searchForm.LEVELID">
                <el-option
                  v-for="item in riskClassificationList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="DEPTIDS">
              <layout-department
                v-model="searchForm.DEPTIDS"
                multiple
                show-checkbox
                collapse-tags
                root-disabled="Y"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="事故类型">
              <el-select v-model="searchForm.BIANMA" multiple>
                <el-option
                  v-for="item in accidentTypeList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="管控责任人" prop="gkzrName">
              <el-input v-model="searchForm.gkzrName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否审核" prop="STATUS">
              <el-select v-model="searchForm.STATUS">
                <el-option label="未审核" value="0" />
                <el-option label="已审核" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetDataTransfer"
        v-model:pagination="pagination"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="DEPT_NAME_ALL" label="管控部门" width="250" />
        <el-table-column
          prop="RISKUNITNAME"
          label="风险点（单元）"
          width="250"
        />
        <el-table-column prop="PARTSNAME" label="辨识部位" width="250" />
        <el-table-column prop="LEVELID" label="风险分级" width="120">
          <template v-slot="{ row }">
            <span v-if="row.LEVELID === 'levelD'" class="text-blue">
              低风险/D级
            </span>
            <span v-else-if="row.LEVELID === 'levelC'" class="text-yellow">
              一般风险/C级
            </span>
            <span v-else-if="row.LEVELID === 'levelB'" class="text-orange">
              较大风险/B级
            </span>
            <span v-else-if="row.LEVELID === 'levelA'" class="text-red">
              重大风险/A级
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="ACCIDENTS_NAME" label="事故类型" />
        <el-table-column prop="USER_ID" label="管控责任人" width="100" />
        <el-table-column prop="STATUS" label="状态" width="90">
          <template v-slot="{ row }">
            {{ row.STATUS === 1 ? "已审核" : "未审核" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button
              v-if="row.STATUS === 0"
              type="primary"
              text
              link
              @click="fnAnalysis(row)"
            >
              分析
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <analysis
      v-model:visible="data.analysisDialog.visible"
      :info="data.analysisDialog.info"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import LayoutDepartment from "@/components/department/index.vue";
import {
  layoutFnGetAccidentType,
  layoutFnGetRiskClassification,
} from "@/assets/js/data_dictionary.js";
import { reactive } from "vue";
import { getRiskAnalysisList } from "@/request/continuous_improvement.js";
import { useUserStore } from "@/pinia/user.js";
import { ElMessageBox } from "element-plus";
import { cloneDeep } from "lodash-es";
import Analysis from "./components/analysis.vue";

const userStore = useUserStore();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getRiskAnalysisList);
const data = reactive({
  analysisDialog: {
    visible: false,
    info: {},
  },
});
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
    BIANMA: searchForm.value.BIANMA?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
    BIANMA: searchForm.value.BIANMA?.join(","),
  });
};
const riskClassificationList = await layoutFnGetRiskClassification();
const accidentTypeList = await layoutFnGetAccidentType();
const fnAnalysis = (row) => {
  if (
    userStore.getUserInfo.ISMAIN === "1" ||
    userStore.getUserInfo.IS_SAFETY === 1
  ) {
    data.analysisDialog.visible = true;
    data.analysisDialog.info = cloneDeep(row);
  } else {
    ElMessageBox.alert("您无权进行此项操作");
  }
};
</script>

<style scoped></style>
