<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="searchNotifications"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="从业人员" prop="PERSON">
              <el-input v-model="searchForm.PERSON" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="从业类型" prop="PRACTITIONERTYPE">
              <el-select v-model="searchForm.PRACTITIONERTYPE">
                <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="8">
            <el-form-item label="签收状态" prop="SIGNEDSTATUS">
              <el-select v-model="searchForm.SIGNEDSTATUS">
                <el-option label="已签收" :value="1" />
                <!-- 1代表已签收 -->
                <el-option label="未签收" :value="0" />
                <!-- 0代表未签收 -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <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="8">
            <el-form-item>
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="resetSearchForm"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        :data="list"
        @get-data="fnGetData"
        v-model:pagination="pagination"
        stripe
        border
        show-header
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column label="从业人员" prop="PERSON" />
        <el-table-column label="从业类型" prop="PRACTITIONERTYPE" />
        <el-table-column label="签收时间" prop="SIGNEDDATE">
          <template #default="{ row }">
            <span v-if="row.SIGNEDDATE">{{ row.SIGNEDDATE }}</span>
            <el-tag v-else>未签收</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="回复时间" prop="REPLYDATE">
          <template #default="{ row }">
            <span v-if="row.REPLYDATE">{{ row.REPLYDATE }}</span>
            <el-tag v-else>未回复</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="回复内容" prop="REPLYCONTENT">
          <template #default="{ row }">
            <span v-if="row.REPLYCONTENT">{{ row.REPLYCONTENT }}</span>
            <el-tag v-else>未回复</el-tag>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { reactive, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import {
  getSecurityNotice,
  layoutFnGetSIGNEDSTATUSClassification,
} from "@/request/safety_production_related.js";
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData";

const route = useRoute();
const NOTIFICATION_ID = route.query.NOTIFICATION_ID;

const searchForm = reactive({
  PERSON: "",
  PRACTITIONERTYPE: "",
  SIGNEDSTATUS: null,
  REPLYSTATUS: null,
});

const { list, pagination, fnGetData, fnResetPagination } = useListData(
  getSecurityNotice,
  {
    searchForm,
    otherParams: { NOTIFICATION_ID },
    usePagination: true,
  }
);

async function searchNotifications() {
  const params = Object.keys(searchForm).reduce((acc, key) => {
    acc[key] = searchForm[key];
    return acc;
  }, {});

  params.NOTIFICATION_ID = NOTIFICATION_ID;
  await fnGetData(params);
}

// 重置表单时，也确保重置为初始值
function resetSearchForm() {
  Object.keys(searchForm).forEach((key) => {
    searchForm[key] = "";
  });
  fnResetPagination({ NOTIFICATION_ID });
}

const relatedClassificationList = ref(null);

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

<style scoped></style>
