<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button type="primary" @click="handleImportData" preIcon="ant-design:plus-outlined">导入填报数据</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button
            >批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BaosongTaskModal @register="registerModal" @success="handleSuccess" />
    <BaosongTaskDrawer ref="refTaskDrawer"  @callback="handleSuccess"/>

    <BaosongTaskImportDataModal @register="registerImportDataModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="baosong-baosongTask" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import BaosongTaskModal from './components/BaosongTaskModal.vue';
  import BaosongTaskDrawer from './components/BaosongTaskDrawer.vue';

  import BaosongTaskImportDataModal from './components/BaosongTaskImportDataModal.vue';

  

  import { columns, searchFormSchema } from './BaosongTask.data';
  import { list, deleteOne, batchDelete, addApproval,saveOrUpdate,importData } from './BaosongTask.api';
  import { useMessage } from "/@/hooks/web/useMessage";
  const { createMessage } = useMessage();

  const refTaskDrawer = ref();
  const checkedKeys = ref<Array<string | number>>([]);

  const [registerModal, { openModal }] = useModal();
  const [registerImportDataModal, { openModal:openImportDataModal }] = useModal();

  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '任务表',
      api: list,
      columns,
      canResize: false,
      formConfig: {
        labelWidth: "30%",
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
      },
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
    },
  });

  const [registerTable, { reload }, {rowSelection, selectedRowKeys,selectedRows}] = tableContext;

  function handleAlloc(record: Recordable) {
    refTaskDrawer.value.setIniData(record);
  }

  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }


  function handleImportData(record: Recordable) {
    if(selectedRows.value?.length==0) {
      createMessage.warning("请选择记录");
      return false
    }

    if(selectedRows.value?.length>1) {
      createMessage.warning("只能选中一条记录");
      return false
    }

    openImportDataModal(true, {
      record:selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }

  
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  async function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  
  async function handleSubmitAdd(record){
    let params = {id:record.id,sta:2}
    await saveOrUpdate(params,true)
    await handleSuccess()
  }

  function getTableAction(record) {

    return [
      {
        label: '分配',
        //disabled:record.sta>1,
        onClick: handleAlloc.bind(null, record),
      },
      // {
      //   label: '提交填报',
      //   disabled:record.sta!=1,
      //   popConfirm: {
      //     title: '提交后，不能继续分配，您确认要提交填报吗？',
      //     confirm:  handleSubmitAdd.bind(null, record)
      //   }
      // },
      {
        label: '提交审批',
        disabled:!(record.allocNum==record.fillStaNum &&record.sta==2),
        popConfirm: {
          title: '您确认要提交哦啊审批吗？',
          confirm:  handleApproval.bind(null, record)
        }
      },
    ];
  }

  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  function handleApproval(task) {
    addApproval(task, handleSuccess);
  }


</script>

<style scoped></style>
