<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="fnResetPagination"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="通知标题" prop="TITLE">
              <el-input v-model="searchForm.TITLE" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="回复状态" prop="REPLYSTATUS">
              <el-select v-model="searchForm.REPLYSTATUS">
                <el-option label="已回复" :value="1" />
                <!-- 1代表已回复 -->
                <el-option label="未回复" :value="0" />
                <!-- 0代表未回复 -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="通知等级" prop="RISKUNITNAME">
              <el-select v-model="searchForm.RISKUNITNAME">
                <el-option
                  v-for="item in relatedClassificationList"
                  :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="fnResetPagination">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        ref="tableRef"
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="TITLE" label="通知标题" width="250" />
        <el-table-column label="通知等级" width="150">
          <template #default="{ row }">
            <el-tag>{{ row.LEVEL }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="POSTSTATUS" label="发布状态" width="150">
          <template #default="{ row }">
            <el-tag v-if="row.POSTSTATUS === 0">未发布</el-tag>
            <el-tag v-else>已发布</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="回复情况" width="150">
          <template #default="{ row }">
            {{
              row.REPLYSTATUSNUM
                ? row.REPLYSTATUSNUM + "/" + row.TOTALREADDETAIL + "人"
                : "0/" + row.TOTALREADDETAIL + "人"
            }}
          </template>
        </el-table-column>

        <el-table-column label="签收情况" width="150">
          <template #default="{ row }">
            {{
              row.SIGNEDSTATUSNUM
                ? row.SIGNEDSTATUSNUM + "/" + row.TOTALREADDETAIL + "人"
                : "0/" + row.TOTALREADDETAIL + "人"
            }}
          </template>
        </el-table-column>
        <el-table-column prop="CREATETIME" label="创建时间" width="200" />
        <el-table-column prop="CORP_NAME" label="运输企业" width="250">
        </el-table-column>
        <el-table-column label="操作" width="200"> </el-table-column>
        <template #button>
          <el-button
            type="primary"
            @click="
              router.push({
                path: '/safety_production_related/security_notice/add',
              })
            "
          >
            添加
          </el-button>
          <el-button
            v-if="buttonJurisdiction.del"
            type="danger"
            @click="fnBatchDelete"
          >
            批量删除
          </el-button>
        </template>
      </layout-table>
    </layout-card>
    <edit
      v-model:visible="data.analysisDialog.visible"
      :info="data.analysisDialog.info"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData.js";
import { onMounted, reactive, ref } from "vue";
import {
  getSecurityNoticeList,
  layoutFnGetNotificationsClassification,
  dateteSecurityAllNotice,
} from "@/request/safety_production_related.js";
import router from "@/router/index.js";
import { ElMessageBox, ElMessage } from "element-plus";
import { debounce } from "throttle-debounce";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";

const buttonJurisdiction = await useButtonJurisdiction("riskunit");

const tableRef = ref(null);
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getSecurityNoticeList);
const data = reactive({
  analysisDialog: {
    visible: false,
    info: {},
  },
});
const fnResetPaginationTransfer = () => {
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnBatchDelete = debounce(
  1000,
  async () => {
    // eslint-disable-next-line no-undef
    const selectionData = tableRef.value.getSelectionRows();
    if (selectionData.length === 0) {
      ElMessage.warning("请选中要删除的项");
      return;
    }
    await ElMessageBox.confirm("确定要删除选中的数据吗？", { type: "warning" });
    const DATA_IDS = selectionData
      .map((item) => item.NOTIFICATION_ID)
      .join(",");
    await dateteSecurityAllNotice({ DATA_IDS });
    ElMessage.success("删除成功");
    fnResetPaginationTransfer();
  },
  { atBegin: true }
);
const relatedClassificationList = ref(null);

onMounted(async () => {
  const data = await layoutFnGetNotificationsClassification();
  relatedClassificationList.value = JSON.parse(data.value.zTreeNodes);
});
</script>

<style scoped></style>
