<template>
  <div class="disaster-precaution-container">
    <header class="disaster-precaution-container__header">
      <span class="disaster-precaution-container__title">防御通知管理列表</span>
    </header>
    <main class="disaster-precaution-container__main">
      <div class="disaster-precaution">
        <header class="disaster-precaution__header">
          <el-button
            type="primary"
            class="disaster-precaution__header--button"
            :icon="Plus"
            @click="handleCreateDefenseNotice"
            >创建灾害防御通知
          </el-button>
          <Search
            :searchConfig="DEFENSE_NOTICE_SEARCH_CONFIG"
            :searchData="searchData"
            @update:searchData="handleSearch"
          />
        </header>
        <BasicTable
          :tableConfig="tableConfig"
          :tableData="tableData"
          @update:pageSize="handleSizeChange"
          @update:pageNumber="handleCurrentChange"
        >
          <template #activeStatus="scope">
            <div class="active-status--div">
              <div
                class="dot"
                :style="{ backgroundColor: ACTIVE_STATUS_COLOR[scope.row.activeStatus as ACTIVE_STATUS] }"
              />
              <span>{{ ACTIVE_STATUS_MAP[scope.row.activeStatus] }}</span>
            </div>
          </template>
          <template #pushStatus="scope">
            <span :style="{ color: scope.row.pushStatus === PUSH_STATUS.PUSHED ? '' : '#ff4d4f' }">
              {{ PUSH_STATUS_MAP[scope.row.pushStatus as PUSH_STATUS] }}
            </span>
          </template>
          <template #action="scope">
            <ActionButton
              text="编辑"
              v-if="scope.row.activeStatus === ACTIVE_STATUS.NOT_EFFECTIVE"
              @click="handleEditDefenseNotice(scope.row.id)"
            />
            <ActionButton text="查看" @click="handleViewDefenseNotice(scope.row.id)" />
            <ActionButton
              text="发布"
              :popconfirm="{
                title: '确定要发布?',
              }"
              v-if="scope.row.activeStatus === ACTIVE_STATUS.NOT_EFFECTIVE"
            />
            <ActionButton
              text="撤回"
              :popconfirm="{
                title: '确定要撤回?',
              }"
              v-else-if="scope.row.activeStatus === ACTIVE_STATUS.ACTIVE"
            />
            <ActionButton
              text="删除"
              :popconfirm="{
                title: '确定要删除?',
              }"
            />
          </template>
        </BasicTable>
      </div>
    </main>
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted, reactive } from 'vue';
  import { Plus } from '@element-plus/icons-vue';
  import Search from '@/views/disaster/components/Search.vue';
  import BasicTable from '@/components/BasicTable.vue';
  import ActionButton from '@/components/ActionButton.vue';
  import useTableConfig from '@/hooks/useTableConfigHook';
  import { getDefenseNoticeList } from '@/api/disaster-warning';
  import type { DefenseNoticeListResponse } from '@/types/disaster-warning';
  import { ACTIVE_STATUS, ACTIVE_STATUS_COLOR, ACTIVE_STATUS_MAP } from '@/views/disaster/constant';
  import { PUSH_STATUS_MAP, PUSH_STATUS } from './src/constant';
  import { DEFENSE_NOTICE_SEARCH_CONFIG, TABLE_OPTIONS, DEFENSE_NOTICE_TABLE_COLUMNS } from './src/config';
  import { useRouter } from 'vue-router';
  const tableData = ref<DefenseNoticeListResponse[]>([]);
  const router = useRouter();
  const defaultPath = '/disaster-prevention/disaster-warning/defense-notice-item';
  const handleEditDefenseNotice = (id: number) => {
    router.push({
      path: defaultPath,
      query: { id, operate: 'edit' },
    });
  };
  const handleCreateDefenseNotice = () => {
    router.push({
      path: defaultPath,
      query: {
        operate: 'create',
      },
    });
  };
  const handleViewDefenseNotice = (id: number) => {
    router.push({
      path: defaultPath,
      query: { id },
    });
  };
  const searchData = reactive({
    disasterType: '',
    disasterLevel: '',
    activeStatus: '',
  });
  const handleSearch = (data: any) => {
    console.log(data);
  };
  const { tableConfig, pagination } = useTableConfig(DEFENSE_NOTICE_TABLE_COLUMNS, TABLE_OPTIONS);
  const handleSizeChange = (value: number) => {
    pagination.pageSize = value;
    tableConfig.loading = true;
    getTableData();
  };
  const handleCurrentChange = (value: number) => {
    pagination.pageNumber = value;
    tableConfig.loading = true;
    getTableData();
  };
  const getTableData = async () => {
    const res = await getDefenseNoticeList();
    tableData.value = res;
    pagination.total = tableData.value.length;
    tableConfig.loading = false;
  };
  onMounted(() => {
    getTableData();
  });
</script>

<style lang="scss" scoped>
  @use '../style/disaster.scss' as *;
  @use './src/common.scss' as *;
</style>
