<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="100px"
        @submit.prevent="searchNotifications"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="从业人员" prop="PRACTITIONER">
              <el-input v-model="searchForm.PRACTITIONER" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="从业类型" prop="PRACTITIONER_TYPE">
              <el-select v-model="searchForm.PRACTITIONER_TYPE">
                <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="参会状态" prop="ATTENDANCE_STATUS">
              <el-select v-model="searchForm.ATTENDANCE_STATUS">
                <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>
              <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
        v-model:pagination="pagination"
        :data="list"
        stripe
        border
        show-header
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column label="从业人员" prop="NAME" />
        <el-table-column label="从业类型" prop="PRACTITIONER_TYPE" />
        <el-table-column label="联系电话" prop="USERNAME" />
        <el-table-column label="参会状态" prop="ATTENDANCE_STATUS">
          <template #default="{ row }">
            <el-tag v-if="row.ATTENDANCE_STATUS === '1'">{{ "已参会" }}</el-tag>
            <el-tag v-else>未参会</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button
              v-if="row.ATTENDANCE_STATUS === '1'"
              type="danger"
              text
              link
              @click="editItem(row.SAFETY_MEETING_ID, row.PRACTITIONER, 0)"
            >
              移除
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { reactive, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { layoutFnGetSIGNEDSTATUSClassification } from "@/request/safety_production_related.js";
import {
  editSafetyMeetingRecipient,
  getSafetyMeetingRecipient,
} from "@/request/traffic_safety_meeting.js";
import { serialNumber } from "@/assets/js/utils";
import useListData from "@/assets/js/useListData";
import { ElMessage, ElMessageBox } from "element-plus";

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

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

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

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

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

// 重置表单时，也确保重置为初始值
function resetSearchForm() {
  Object.keys(searchForm).forEach((key) => {
    searchForm[key] = "";
  });
  fnResetPagination({ SAFETY_MEETING_ID });
}
const editItem = async (
  safetyMeetingId,
  practitioner,
  attendanceStatus = 0
) => {
  await ElMessageBox.confirm(`确定要移除吗？`, {
    type: "warning",
  });
  await editSafetyMeetingRecipient({
    SAFETY_MEETING_ID: safetyMeetingId,
    PRACTITIONER: practitioner,
    ATTENDANCE_STATUS: attendanceStatus, // 设置参会状态为0
  });
  ElMessage.success("移除成功");
  fnGetData();
};

const relatedClassificationList = ref(null);

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

<style scoped></style>
