<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="50px"
        @submit.prevent="fnResetPagination"
      >
        <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-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
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetData"
      >
        <el-table-column label="序号" width="60">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>
        <el-table-column prop="NAME" label="类型" />
        <el-table-column prop="USERNAME" label="抄送人员" />
        <el-table-column label="启用状态" width="150">
          <template #default="{ row }">
            <el-switch
              :model-value="row.ISENABLE"
              active-value="1"
              inactive-value="0"
              @change="
                fnChangeStatus(row.NOTICE_MANAGEMENT_ID, row.NOTICETEMPLATE_ID)
              "
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                fnConfigure(row.NOTICE_MANAGEMENT_ID, row.NOTICETEMPLATE_ID)
              "
            >
              配置
            </el-button>
          </template>
        </el-table-column>
      </layout-table>
    </layout-card>
    <configure
      v-model:visible="data.configureDialog.visible"
      :notice-management-id="data.configureDialog.NOTICE_MANAGEMENT_ID"
      :notice-template-id="data.configureDialog.NOTICETEMPLATE_ID"
      @get-data="fnResetPagination"
    />
  </div>
</template>

<script setup>
import { reactive } from "vue";
import { serialNumber } from "@/assets/js/utils";
import { debounce } from "throttle-debounce";
import { ElMessage } from "element-plus";
import useListData from "@/assets/js/useListData.js";
import Configure from "./components/configure.vue";
import {
  getNotificationTemplateConfigurationList,
  setNotificationTemplateConfigurationAdd,
} from "@/request/notification_announcement.js";

const data = reactive({
  configureDialog: {
    visible: false,
    NOTICE_MANAGEMENT_ID: "",
    NOTICETEMPLATE_ID: "",
  },
});
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getNotificationTemplateConfigurationList, {
    callbackFn: (list) => {
      list.forEach((item) => {
        item.ISENABLE = item.ISENABLE || "0";
      });
    },
  });
const fnChangeStatus = debounce(
  1000,
  async (NOTICE_MANAGEMENT_ID, NOTICETEMPLATE_ID) => {
    await setNotificationTemplateConfigurationAdd({
      NOTICE_MANAGEMENT_ID,
      NOTICETEMPLATE_ID,
    });
    ElMessage.success("状态修改成功");
    fnResetPagination();
  },
  { atBegin: true }
);
const fnConfigure = async (NOTICE_MANAGEMENT_ID, NOTICETEMPLATE_ID) => {
  data.configureDialog.visible = true;
  data.configureDialog.NOTICE_MANAGEMENT_ID = NOTICE_MANAGEMENT_ID;
  data.configureDialog.NOTICETEMPLATE_ID = NOTICETEMPLATE_ID;
};
</script>

<style scoped></style>
