<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="handleCreateWarningInfo"
          >
            创建灾害预警信息
          </el-button>
          <Search
            :searchConfig="WARNING_INFO_SEARCH_CONFIG"
            :searchData="searchData"
            @update:searchData="handleSearch"
          />
        </header>
        <BasicTable
          :tableConfig="tableConfig"
          :tableData="tableData"
          @update:pageSize="handleSizeChange"
          @update:pageNumber="handleCurrentChange"
        >
          <template #warningIcon="scope">
            <img :src="getWarningIcon(scope.row.disasterType)" alt="预警图标" class="weather-warning-icon" />
          </template>
          <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="编辑"
              @click="handleEditWarningInfo(scope.row.id)"
              v-if="scope.row.activeStatus === ACTIVE_STATUS.NOT_EFFECTIVE"
            />
            <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 { getWarningInfoList } from '@/api/disaster-warning';
  import type { WarningInfoListResponse } from '@/types/disaster-warning';
  import { ACTIVE_STATUS, ACTIVE_STATUS_COLOR, ACTIVE_STATUS_MAP } from '@/views/disaster/constant';
  import PlaceHolderWeather from './src/images/placeholder-weather@1X.png';
  import { PUSH_STATUS_MAP, PUSH_STATUS, WEATHER_DISASTER_ALERT_TYPE } from './src/constant';
  import { WARNING_INFO_SEARCH_CONFIG, TABLE_OPTIONS, WARNING_INFO_TABLE_COLUMNS } from './src/config';
  import { useRouter } from 'vue-router';
  const router = useRouter();
  const searchData = reactive({
    disasterType: '',
    disasterLevel: '',
  });
  const handleSearch = (data: any) => {
    console.log(data);
  };
  const getWarningIcon = (disasterType: string) => {
    const icon = WEATHER_DISASTER_ALERT_TYPE.find((item) => item.label === disasterType)?.icon;
    return icon ?? PlaceHolderWeather;
  };
  const defaultPath = '/disaster-prevention/disaster-warning/warning-info-item';
  const handleCreateWarningInfo = () => {
    router.push({
      path: defaultPath,
      query: {
        operate: 'create',
      },
    });
  };
  const handleEditWarningInfo = (id: number) => {
    router.push({
      path: defaultPath,
      query: {
        operate: 'edit',
        id,
      },
    });
  };
  const tableData = ref<WarningInfoListResponse[]>([]);
  const { tableConfig, pagination } = useTableConfig(WARNING_INFO_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 getWarningInfoList();
    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 *;
  .weather-warning-icon {
    width: 50cpx;
    height: 50cpx;
  }
</style>
