<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <!-- <DeptTree ref="childRef" class="w-1/4 xl:w-1/5" @select="handleSelect" /> -->
    <BasicTable class="w-4/4 xl:w-5/5" @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <PermissionBtn @btnEvent="onBtnClicked"></PermissionBtn>
      </template>

      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'state'">
          <a-tag color="orange" v-if="record.state == 0">待领取</a-tag>
          <a-tag color="blue" v-if="record.state == 1">领取中</a-tag>
          <a-tag color="green" v-if="record.state == 2">已领完</a-tag>
        </template>
        
        <template v-if="column.key === 'action'">
          <a-button type="link" @click="handlerGetDetail(record.id,record)" >查看详情</a-button>
        </template>
      </template>

    </BasicTable>
    <!-- add & edit -->
    <DeptModal @register="registerModal" @success="handleSuccess" />
    <!-- detail -->
    <DetailModal @register="registerDetailModal" @success="handleSuccess"></DetailModal>

    <PosGroupModal @register="registerPosGroupModal" @success="handleSuccess"></PosGroupModal>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, ref } from 'vue';

  import { BasicTable, useTable } from '@/components/Table';
  import { getOrgList, deleteDept } from '@/api/demo/system';
  import {getTaskList,deleteTask,getTaskDetail} from '@/api/task'
  import { PageWrapper } from '@/components/Page';
  import { useModal } from '@/components/Modal';
  import { useMessage } from '@/hooks/web/useMessage';

//   import { DeptTree } from './page';
  import { DeptModal } from './page';
  import { DetailModal } from './page';

  import { PosGroupModal } from './page';
  import { PermissionBtn } from '@/components/PermissionBtn/index';

  import { columns, searchFormSchema } from './dept.data';

  defineOptions({ name: 'DeptManagement' });
  const { createConfirm, createMessage } = useMessage();

  const [registerModal, { openModal: openDeptModal }] = useModal();
  const [registerDetailModal,{openModal:openDetailModal}] = useModal();
  const [registerPosGroupModal, { openModal: openPosGroupModal }] = useModal();
  const searchInfo = reactive<Recordable>({});

  const [registerTable, { reload, getSelectRows, clearSelectedRowKeys }] = useTable({
    title: '任务列表',
    api: getTaskList,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    rowSelection: {
      //多选框
      // type: 'checkbox',
      type: 'radio',
    },
    useSearchForm: true,
    showIndexColumn: false,
    showTableSetting: true,
    bordered: true,
    handleSearchInfoFn(info) {
      return info;
    },
  });
  async function handlePoGroup() {
    let rows = getSelectRows();
    if (!rows.length) {
      return createMessage.warn('请选择一个任务');
    }
    var record = rows[0];
    openPosGroupModal(true, {
      record,
      isUpdate: false,
    });
  }
  const childRef = ref<any>();

  function handleCreate() {
    openDeptModal(true, {
      isUpdate: false,
    });
  }

  async function handleEdit() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个任务进行编辑！');
    }
    if(rows[0].state != 0){
        return createMessage.warn('当前任务已被领取，无法编辑！');
    }
    // const record = rows[0];

    const record =await getTaskDetail({id:rows[0].id});

    console.log("record",record);

    openDeptModal(true, {
        record,
        isUpdate: true,
    });
  } 

  async function handlerGetDetail(id,record){
    const tifAndShp = await getTaskDetail({id:id});
    openDetailModal(true, {
      record:{record:record,info:tifAndShp},
      isUpdate: true,
    });
  }

  function handleDetail(){
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个任务进行查看');
    }
    const record = rows[0];
    
    openDetailModal(true, {
      record,
      isUpdate: true,
    });
  }

  async function handleDelete() {
    let rows = getSelectRows();
    if (rows.length == 0) {
      return createMessage.warn('请选择一个任务进行删除');
    }
    if(rows[0].state != 0){
        return createMessage.warn('当前任务已被领取，无法删除！');
    }
    const query = [rows[0].id];
    createConfirm({
      iconType: 'info',
      title: '删除',
      content: '确定要删除当前任务吗',
      onOk: async () => {
        try{
            const data = await deleteTask({id:rows[0].id});
            if (data) {
            handleSuccess();
            createMessage.success('删除成功');
            } else {
            createMessage.error('删除失败');
            }
        }catch(e){
            console.log(e);
        }
        
      },
    });
  }
  function onBtnClicked(domId) {
    switch (domId) {
      case 'btnAdd':
        handleCreate();
        break;
      case 'btnEdit':
        handleEdit();
        break;
      case 'btnDelete':
        handleDelete();
        break;
      case 'btnPoGroup':
        handlePoGroup();
        break;
      case 'btnDetail':
        handleDetail();
        break;
      default:
        break;
    }
  }
  function handleSelect(orgId = '') {
    searchInfo.orgId = orgId;
    reload();
  }
  function handleSuccess() {
    clearSelectedRowKeys();
    // childRef.value.fetch();
    reload();
  }
</script>
