<template>
  <div class="part-box is-filter">
    <a-space class="filter-line" :size="12" wrap>
      <SelectTeaching
        v-model="searchModel.teachingId"
        placeholder="请选择"
        allow-clear
        prefix
      />
      <SelectAgent
        v-model="searchModel.agentId"
        placeholder="请选择"
        allow-clear
        prefix
        :teaching-id="searchModel.teachingId"
      />
      <a-input v-model.trim="searchModel.name" placeholder="请输入" allow-clear>
        <template #prefix>考场名称</template>
      </a-input>
      <a-select
        v-model="searchModel.enable"
        placeholder="请选择"
        allow-clear
        :options="ableOptions"
      >
        <template #prefix>状态</template>
      </a-select>

      <a-button type="primary" @click="toPage(1)">查询</a-button>
    </a-space>
  </div>
  <div class="part-box">
    <a-space class="part-action" :size="6">
      <a-button type="text" @click="toAdd">
        <template #icon>
          <svg-icon name="icon-add" />
        </template>
        新增
      </a-button>
      <a-divider direction="vertical" />
      <a-button type="text" @click="toImport">
        <template #icon>
          <svg-icon name="icon-import" />
        </template>
        导入
      </a-button>
    </a-space>
    <a-table
      class="page-table"
      :columns="columns"
      :data="dataList"
      :pagination="pagination"
      :scroll="{ x: 1200 }"
      :bordered="false"
    >
      <template #enable="{ record }">
        <status-tag type="enable" :value="record.enable" />
      </template>
      <template #action="{ record }">
        <a-button type="text" class="btn-primary" @click="toEdit(record)"
          >编辑</a-button
        >
        <a-button
          :class="record.enable ? 'btn-danger' : 'btn-primary'"
          type="text"
          @click="toEnable(record)"
          >{{ record.enable ? '禁用' : '启用' }}</a-button
        >
      </template>
    </a-table>

    <!-- ImportDialog -->
    <ImportDialog
      ref="importDialogRef"
      title="导入考场"
      upload-url="/api/admin/room/import"
      :format="['xls', 'xlsx']"
      :download-handle="downloadTemplate"
      download-filename="考场导入模板.xlsx"
      :auto-upload="false"
      @upload-success="getList"
    />
    <!-- ModifyRoom -->
    <ModifyRoom ref="modifyRoomRef" :row-data="curRow" @modified="getList" />
  </div>
</template>

<script setup lang="ts">
  import { reactive, ref } from 'vue';
  import { Message, TableColumnData } from '@arco-design/web-vue';
  import { roomListPage, roomTemplate, ableRoom } from '@/api/base';
  import { RoomItem } from '@/api/types/base';
  import useTable from '@/hooks/table';
  import useDictOption from '@/hooks/dict-option';
  import { modalConfirm } from '@/utils/arco';
  import { downloadByApi } from '@/utils/download';
  import { useAppStore } from '@/store';

  import ImportDialog from '@/components/import-dialog/index.vue';
  import ModifyRoom from './modifyRoom.vue';

  defineOptions({
    name: 'RoomManage',
  });

  const appStore = useAppStore();
  appStore.setInfo({ breadcrumbs: ['基础信息管理', '考场管理'] });

  const { optionList: ableOptions } = useDictOption('ABLE_TYPE');

  const searchModel = reactive({
    teachingId: null,
    agentId: '',
    name: '',
    enable: undefined,
  });

  const columns: TableColumnData[] = [
    {
      title: '考场代码',
      dataIndex: 'code',
      width: 120,
    },
    {
      title: '考场名称',
      dataIndex: 'name',
    },
    {
      title: '考场地址',
      dataIndex: 'address',
    },
    {
      title: '机房容量',
      dataIndex: 'capacity',
      width: 100,
    },
    {
      title: '所属考点',
      dataIndex: 'agentName',
    },
    {
      title: '所属教学点',
      dataIndex: 'teachingName',
    },
    {
      title: '状态',
      slotName: 'enable',
      width: 100,
    },
    {
      title: '操作',
      slotName: 'action',
      width: 120,
      fixed: 'right',
      cellClass: 'action-column',
    },
  ];
  const { dataList, pagination, toPage, getList } = useTable<RoomItem>(
    roomListPage,
    searchModel,
    true
  );

  async function downloadTemplate() {
    const res = await downloadByApi(() => roomTemplate()).catch((e) => {
      Message.error(e || '下载失败，请重新尝试！');
    });
    if (!res) return;
    Message.success('下载成功！');
  }

  // table action
  // 导入
  const importDialogRef = ref();
  function toImport() {
    importDialogRef.value?.open();
  }

  const modifyRoomRef = ref();
  const curRow = ref({} as RoomItem);
  function toAdd() {
    curRow.value = {} as RoomItem;
    modifyRoomRef.value?.open();
  }
  function toEdit(row: RoomItem) {
    curRow.value = row;
    modifyRoomRef.value?.open();
  }

  async function toEnable(row: RoomItem) {
    const action = row.enable ? '禁用' : '启用';
    const confirmRes = await modalConfirm(
      '提示',
      `确定要${action}考场【${row.name}】吗?`
    ).catch(() => false);
    if (confirmRes !== 'confirm') return;

    await ableRoom({ id: row.id, enable: !row.enable });
    Message.success('操作成功！');
    getList();
  }
</script>
