<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
          <a-button type="primary" @click="handleTaskAlloc" preIcon="ant-design:plus-outlined"> 批量分配</a-button>
          <a-button type="primary" @click="handleDeleteTaskAlloc" preIcon="ant-design:minus-outlined">删除分配</a-button>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
         <div v-html="text"></div>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <MetricTaskAllocModal @register="registerModal" @success="handleSuccess"></MetricTaskAllocModal>
    <DataAutoDrawer @register="registerDrawer" @success="handleSuccess"></DataAutoDrawer>
  </div>
</template>

<script lang="ts" name="metric-metricTaskAlloc" 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 MetricTaskAllocModal from './components/MetricTaskAllocModal.vue'
  import {columns, searchFormSchema} from './MetricTaskAlloc.data';
  import {list,deleteTaskAlloc} from './MetricTaskAlloc.api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import DataAutoDrawer from './components/DataAutoDrawer.vue'
  import { useDrawer } from '/@/components/Drawer';
  
  const [registerDrawer, { openDrawer }] = useDrawer();
  const { createMessage } = useMessage();
  const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext } = useListPage({
      tableProps:{
           title: '任务分配',
           api: list,
           columns,
           canResize:true,
          //  rowSelection: {
          //       type: 'radio',
          //   },
           formConfig: {
              labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
      },
  })

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

   /**
    * 分配任务
    */
  function handleTaskAlloc() {
    if(selectedRowKeys.value.length==0) {
      createMessage.warning('请选择记录!');
      return false
    }
    let mtrcNoAry = [];
    for(let row of rowSelection.selectedRows) {
      mtrcNoAry.push(row.mtrcNo)
    }
    openModal(true, {
       mtrcNos:mtrcNoAry.join(","),
       isUpdate: false,
       showFooter: true,
     });
  }


     /**
    * 删除任务
    */
   async function handleDeleteTaskAlloc() {
      if(selectedRowKeys.value.length==0) {
        createMessage.warning('请选择记录!');
        return false
      }
      let mtrcNoAry = [];
      for(let row of rowSelection.selectedRows) {
        mtrcNoAry.push(row.mtrcNo)
      }
      
      deleteTaskAlloc(mtrcNoAry,handleSuccess)
  }



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



   function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }

  function getTableAction(record){
       return [
         {
           label: '设置填报人',
           onClick: handleEdit.bind(null, record),
         },
         {
           label: '删除填报人',
           onClick: deleteFillUser.bind(null, record),
         },
        //  {
        //    label: '数据采集',
        //    onClick: handleDataAutoDrawer.bind(null, record),
        //  }
       ]
   }

   function deleteFillUser(row){
      let mtrcNoAry = [row.mtrcNo];
      deleteTaskAlloc(mtrcNoAry,handleSuccess)
   }

</script>

<style scoped>

</style>