<template>
  <PageWrapper>
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :actionColumn="actionColumn"
         @selection-change="onSelectionChange"
      >
        <template #tableTitle>
          <el-space>
          <el-button type="primary" @click="handleAdd" v-perm="['sys:notice:add']">
            <template #icon>
              <el-icon class="el-input__icon">
                <PlusOutlined />
              </el-icon>
            </template>
            添加公告
          </el-button>
          <el-button type="danger" @click="handleDelete()" :disabled="!selectionData.length" v-perm="['sys:notice:batchDelete']">
              <template #icon>
                <el-icon class="el-input__icon">
                  <Delete />
                </el-icon>
              </template>
              删除
            </el-button>
          </el-space>
        </template>
      </BasicTable>
    </el-card>

    <editDialog
        v-if="editVisible"
        :noticeId="noticeId"
        v-model:visible="editVisible"
        @success="reloadTable('noRefresh')"
    >
    </editDialog>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { reactive, ref, h,nextTick,defineAsyncComponent } from 'vue';
  import { ColProps } from 'element-plus';
  import { schemas } from './querySchemas';
  import { useForm } from '@/components/Form/index';
  import { TableAction } from '@/components/Table';
  import { getNoticeList,noticeDelete,noticeBatchDelete } from '@/api/data/notice';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import {message,confirm,loading, closeLoading} from "@/utils/auth";
  const editDialog = defineAsyncComponent(() =>
    import('./edit.vue')
)
  const noticeId =ref(0)
  const editVisible=ref(false)
  const selectionData = ref([])
  const tableRef = ref();
  const formParams = reactive({
    title:'',
    status:'',
    type:''
  });
  const actionColumn = reactive({
    width: 250,
    label: '操作',
    prop: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            icon:'Edit',
            type: 'warning',
            onClick: handleEdit.bind(null, record),
            auth: ['sys:notice:update'],
          },
          {
            label: '删除',
            icon:'Delete',
            type: 'danger',
            onClick: handleDelete.bind(null, record),
            auth: ['sys:notice:delete'],
          },
        ],
      });
    },
  });

  const loadDataTable = async (res: any) => {
    const result = await getNoticeList({ ...formParams, ...res });
    return result;
  };

  function reloadTable(noRefresh='') {
    tableRef.value.reload(noRefresh?{}:{pageNo:1});
  }
  const [register, {}] = useForm({
    labelWidth: 80,
    layout: 'horizontal',
    colProps: { span: 6 } as ColProps,
    submitOnReset:true,
    schemas
  });
  function handleSubmit(values: Recordable) {
    handleReset()
    for (const key in values) {
      formParams[key] = values[key]
    }
    reloadTable();
  }

  function handleReset() {
    for (const key in formParams) {
        formParams[key] ='';
    }
  }
  const handleAdd = async () => {
  noticeId.value=0
  await nextTick();
  editVisible.value=true
};

const handleEdit = async (record: Recordable) => {
  noticeId.value=record.row.id
  await nextTick();
  editVisible.value=true
};

async function handleDelete(record: Recordable) {
    let ids = []
    if(!record){
      ids = selectionData.value.map(({id}) => id);
    }
    await confirm('确定要删除？');
    record? await noticeDelete(record.row.id):await noticeBatchDelete(ids);
    message("删除成功");
    reloadTable()
  }
</script>

<style lang="scss" scoped></style>
