<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" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-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"></Icon>
                    删除
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>批量操作
                <Icon icon="mdi:chevron-down"></Icon>
              </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>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{text}">
         {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{text}">
         <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
         <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!--角色用户表格-->
    <RateUserTable @register="rateUserDrawer" />
    <!-- 表单区域 -->
    <RateTaskdetailModal @register="registerModal" @success="handleSuccess"></RateTaskdetailModal>
  </div>
</template>

<script lang="ts" name="rate-rateTaskdetail" setup>
  import {ref, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  import { useListPage } from '/@/hooks/system/useListPage'
  import RateTaskdetailModal from './components/RateTaskdetailModal.vue'
  import {columns, searchFormSchema} from './RateTaskdetail.data';
  import {list, deleteOne, batchDelete, getImportUrl,getExportUrl,wriSubOne} from './RateTaskdetail.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import { useRouter } from 'vue-router';
  import RateUserTable from './components/RateUserTable.vue';
  const router = useRouter();
  const userStore = useUserStore();
  const checkedKeys = ref<Array<string | number>>([]);
  //注册model
  const [registerModal, {openModal}] = useModal();
  const [rateUserDrawer, { openDrawer: openRateUserDrawer }] = useDrawer();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '评估表填报',
           api: list,
           columns,
           canResize:false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 120,
               fixed:'right'
            },
      },
       exportConfig: {
            name:"评估表填报",
            url: getExportUrl,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

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

   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
     openModal(true, {
       record,
       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);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
  /**
    * 填报 
   */
   function handleWriSubContent(record: Recordable) {
    
    router.push({path: '/rate/rateWri',query:{id:record.id}})
   }

   /**
    * 审核 approve
   */
   function handleApproveContent(record: Recordable) {
   
    router.push({path: '/rate/rateApprove',query:{id:record.id}})
   }

    /**
    * 自评估
   */
   function handleAppTableContent(record: Recordable) {
    openRateUserDrawer(true, record);
   }

    function handleWriViewContent(record: Recordable) {
    
    router.push({path: '/rate/rateWriView',query:{id:record.id}})
   }

   /**
      * 操作栏
      */
  function getTableAction(record){
       return [ /**
         {
           label: '编辑',
           onClick: handleEdit.bind(null, record),
         },*/
         {
           label: '填报',
           onClick: handleWriSubContent.bind(null, record),
           ifShow: ()=> { 
            if(userStore.getUserInfo.id ==record['dutyUser']&&(record['rateTaskstatus']=='4'||record['rateTaskstatus']=='9'))
            return true
            else return false
            }
         },
          {
           label: '提交',
           onClick: handleWriSub.bind(null, record),
           ifShow: ()=> { 
            if(userStore.getUserInfo.id ==record['dutyUser']&&record['rateApprove']=='1'&&record['rateTaskstatus']=='6')
            return true
            else return false
            }
         },
         {
           label: '审核',
           onClick: handleApproveContent.bind(null, record),
           ifShow: ()=> { 
            if(userStore.getUserInfo.id ==record['approveuser']&&record['rateTaskstatus']=='7')
            return true
            else return false
            }
         },
         {
           label: '自评估结果',
           onClick: handleAppTableContent.bind(null, record),
           ifShow: ()=> { 
            if((userStore.getUserInfo.id ==record['dutyUser']||userStore.getUserInfo.username ==record['approveuser'])&&(record['rateTaskstatus']=='8'||record['rateTaskstatus']=='6'||record['rateTaskstatus']=='7'))
            return true
            else return false
            }
         },
         {
           label: '查看',
           onClick: handleWriViewContent.bind(null, record),
           ifShow: ()=> { 
            if((userStore.getUserInfo.id ==record['dutyUser']||userStore.getUserInfo.username ==record['approveuser'])&&(record['rateTaskstatus']=='8'||record['rateTaskstatus']=='6'||record['rateTaskstatus']=='7'))
            return true
            else return false
            }
         }


       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [ /**
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
           }
         } */
       ]
   }

/**
    * 提报事件
    */
  async function handleWriSub(record) {
     await wriSubOne({id: record.id}, handleSuccess);
   }


</script>

<style scoped>

</style>