<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="dates">
              <el-date-picker
                v-model="searchForm.dates"
                type="daterange"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                range-separator="至"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="隐患发现人" prop="CREATORNAME">
              <el-input v-model="searchForm.CREATORNAME" />
            </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="隐患级别" prop="HIDDENLEVEL">
              <el-select v-model="searchForm.HIDDENLEVEL">
                <el-option
                  v-for="item in hazardLevelList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.BIANMA"
                />
              </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="HIDDENDESCR"
          label="隐患描述"
          show-overflow-tooltip
        />
        <el-table-column label="隐患处置" width="100">
          <template v-slot="{ row }">
            <span v-if="row.RECTIFICATIONTYPE === '1'">立即整改</span>
            <span v-else-if="row.RECTIFICATIONTYPE === '2'">限期整改</span>
          </template>
        </el-table-column>
        <el-table-column prop="HIDDENLEVELNAME" label="隐患级别" width="120" />
        <el-table-column label="隐患状态" width="100">
          <template v-slot="{ row }">
            {{ translationStatus(row.STATE, stateList) }}
          </template>
        </el-table-column>
        <el-table-column prop="CREATTIME" label="发现时间" width="160" />
        <el-table-column prop="CREATORNAME" label="发现人" width="100" />
        <el-table-column
          prop="RECTIFICATIONTIME"
          label="整改时间"
          width="160"
        />
        <el-table-column
          prop="RECTIFICATIONORNAME"
          label="整改人"
          width="100"
        />
        <el-table-column label="验收人" width="100">
          <template v-slot="{ row }">
            <span v-if="row.RECTIFICATIONTYPE === '2'">
              {{ row.CHECKNAME }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/continuous_improvement/hazard_analysis/view',
                  query: { HIDDEN_ID: row.HIDDEN_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button type="primary" text link @click="fnAnalysis(row)">
              分析
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <analysis
      v-model:visible="data.analysisDialog.visible"
      v-model:form="data.analysisDialog.form"
      @get-data="fnResetPaginationTransfer"
    />
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { serialNumber, translationStatus } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import LayoutDepartment from "@/components/department/index.vue";
import { getHiddenDangerList } from "@/request/hidden_danger_government.js";
import { layoutFnGetHazardLevel } from "@/assets/js/data_dictionary.js";
import { nextTick, reactive } from "vue";
import Analysis from "./components/analysis.vue";
import { getRiskControlLedgerView } from "@/request/risk_control.js";

const stateList = [
  { NAME: "未整改", ID: "1" },
  { NAME: "已整改", ID: "2" },
  { NAME: "已整改", ID: "3" },
  { NAME: "已验收", ID: "4" },
  { NAME: "已过期", ID: "-1" },
];
const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getHiddenDangerList, {
    otherParams: { STATE: 4, DISPOSESTATE: "-1", ANALYSIS: 1 },
  });
const data = reactive({
  analysisDialog: {
    visible: false,
    form: {
      DEPARTMENT_ID: "",
      USER_ID: "",
      RISK_UNIT_ID: "",
      IDENTIFICATION_ID: "",
      RISK_DESCR: "",
      LIKELIHOOD: "",
      EXPOSURE: "",
      CONSEQUENCE: "",
      LEVEL_NAME: "",
      DANGER: "",
      MEASURES: "",
      ACCIDENTS: [],
      EME_MEASURES: "",
      IS_NEW: 1,
    },
  },
});
const fnGetDataTransfer = () => {
  fnGetData({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    STARTTIME: searchForm.value.dates?.[0],
    ENDTIME: searchForm.value.dates?.[1],
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const hazardLevelList = await layoutFnGetHazardLevel();
const fnAnalysis = async ({
  HIDDEN_ID,
  RISKPOINT_ID,
  HIDDENDESCR,
  RECTIFYDESCR,
}) => {
  data.analysisDialog.visible = true;
  await nextTick();
  if (RISKPOINT_ID) {
    const resData = await getRiskControlLedgerView({ RISKPOINT_ID });
    resData.pd.IS_NEW = 0;
    data.analysisDialog.form = resData.pd;
    data.analysisDialog.form.ACCIDENTS = resData.pd.ACCIDENTS.split(",");
    data.analysisDialog.form.RISK_DESCR += HIDDENDESCR;
    data.analysisDialog.form.MEASURES += RECTIFYDESCR;
  }
  data.analysisDialog.form.HIDDEN_ID = HIDDEN_ID;
};
</script>

<style scoped></style>
