<template>
  <el-form
    :model="searchForm"
    label-width="70px"
    @submit.prevent="fnResetPaginationTransfer"
  >
    <el-row>
      <el-col :span="4">
        <el-form-item label="关键字" prop="KEYWORDS">
          <el-input v-model="searchForm.KEYWORDS" placeholder="请输入关键字" />
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <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="4">
        <el-form-item label="等级" label-width="50px">
          <el-select v-model="searchForm.LEVEL">
            <el-option label="一级" value="1" />
            <el-option label="二级" value="2" />
            <el-option label="三级" value="3" />
            <el-option label="四级" value="4" />
          </el-select>
        </el-form-item>
      </el-col>
      <slot :searchForm="searchForm" />
      <el-col :span="4">
        <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-col :span="type === 'season' ? 2 : 4">
        <el-form-item label-width="10px" class="end">
          <el-button @click="emits('assessment-standards')">
            考核标准
          </el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <layout-table
    :data="list"
    v-model:pagination="pagination"
    @get-data="fnGetDataTransfer"
  >
    <el-table-column label="序号" width="70">
      <template v-slot="{ $index }">
        {{ serialNumber(pagination, $index) }}
      </template>
    </el-table-column>
    <el-table-column prop="DEPT_NAME_ALL" label="部门" />
    <el-table-column label="发现隐患">
      <template v-slot:header>
        <span>
          发现隐患
          <el-tooltip
            content="部门发现隐患数除以部门人数，乘以20，最高分不可超过20。"
            placement="top-start"
          >
            <el-icon><question-filled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <template v-slot="{ row }">{{ row.SCORE1.toFixed(2) }}</template>
    </el-table-column>
    <el-table-column label="隐患整改">
      <template v-slot:header>
        <span>
          隐患整改
          <el-tooltip
            content="部门整改隐患数除以部门人数，乘以20，最高分不可超过20。"
            placement="top-start"
          >
            <el-icon><question-filled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <template v-slot="{ row }">{{ row.SCORE2.toFixed(2) }}</template>
    </el-table-column>
    <el-table-column label="检查得分">
      <template v-slot:header>
        <span>
          检查得分
          <el-tooltip
            content="部门清单检查率乘以20，最高分不可超过20。"
            placement="top-start"
          >
            <el-icon><question-filled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <template v-slot="{ row }">{{ row.SCORE3.toFixed(2) }}</template>
    </el-table-column>
    <el-table-column label="培训得分">
      <template v-slot:header>
        <span>
          培训得分
          <el-tooltip
            content="部门人员数除以参与培训人员数，乘以20，最高分不可超过20。"
            placement="top-start"
          >
            <el-icon><question-filled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <template v-slot="{ row }">{{ row.SCORE4.toFixed(2) }}</template>
    </el-table-column>
    <el-table-column label="风险辨识完善得分">
      <template v-slot:header>
        <span>
          风险辨识完善得分
          <el-tooltip
            content="部门内人员通过隐患辨识重新辨识风险数，乘以2。"
            placement="top-start"
          >
            <el-icon><question-filled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <template v-slot="{ row }">{{ row.SCORE5.toFixed(2) }}</template>
    </el-table-column>
    <el-table-column label="考核日期">
      {{ currentDate }}
    </el-table-column>
    <el-table-column label="考核等级" width="200">
      <template v-slot="{ row }">
        {{
          fnComputeLevel(
            row.SCORE1 + row.SCORE2 + row.SCORE3 + row.SCORE4 + row.SCORE5
          )
        }}
      </template>
    </el-table-column>
    <el-table-column label="合计" width="200">
      <template v-slot="{ row }">
        {{
          (
            row.SCORE1 +
            row.SCORE2 +
            row.SCORE3 +
            row.SCORE4 +
            row.SCORE5
          ).toFixed(2)
        }}
      </template>
    </el-table-column>
  </layout-table>
</template>

<script setup>
import LayoutDepartment from "@/components/department/index.vue";
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import { getDepartmentalList } from "@/request/performance_appraisal.js";
import dayjs from "dayjs";
import { QuestionFilled } from "@element-plus/icons-vue";
import { ref } from "vue";
import quarterOfYear from "dayjs/plugin/quarterOfYear";

dayjs.extend(quarterOfYear);
const props = defineProps({
  type: {
    type: String,
    required: true,
    default: "",
  },
});
const emits = defineEmits(["assessment-standards"]);
const currentDate = ref("");
const defaultSearchForm = {};
if (props.type === "month") {
  currentDate.value = dayjs().format("YYYY-MM");
  defaultSearchForm.MONTH = currentDate.value;
} else if (props.type === "year") {
  currentDate.value = dayjs().format("YYYY");
  defaultSearchForm.YEAR = currentDate.value;
} else if (props.type === "season") {
  defaultSearchForm.YEAR = dayjs().format("YYYY");
  defaultSearchForm.QUARTER = dayjs().quarter();
}
const { list, pagination, searchForm, fnResetPagination, fnGetData } =
  useListData(getDepartmentalList, {
    defaultSearchForm,
    callbackFn: () => {
      currentDate.value = searchForm.value.MONTH;
    },
  });
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnComputeLevel = (total) => {
  if (total >= 75) return "一级";
  else if (total >= 50) return "二级";
  else if (total >= 25) return "三级";
  else return "四级";
};
</script>

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