<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="handleCaluculatAllMetric" 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="goToFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <MetricStatReportModal @register="registerModal" @success="handleSuccess"></MetricStatReportModal>
    <ShowFileContentModal @register="registerShowFileContentModal" />
  </div>
</template>

<script lang="ts" name="metric-metricStatReport" setup>
  import { message } from 'ant-design-vue';
  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 MetricStatReportModal from './components/MetricStatReportModal.vue'
  import {columns, searchFormSchema} from './MetricStatReport.data';
  import {list, deleteOne, batchDelete, getImportUrl,getExportUrl,caluculatAllMetric} from './MetricStatReport.api';

  import { getFileAccessHttpUrl } from '/@/utils/common/compUtils';
  import ShowFileContentModal from './components/ShowFileContentModal.vue';

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


  const [registerShowFileContentModal, { openModal: openFileContentModel }] = useModal();

  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册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: [
              ],
            },
            defSort :{
             column: 'createTime',
             order: 'desc',
            },
            actionColumn: {
                width: 160,
                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 getTableAction(record){
       return [
          {
           label: '预览',
           onClick: handleShowReport.bind(null, record),
         },
         {
           label: '下载',
           popConfirm: {
             title: '是否确认下载',
             confirm: goToFile.bind(null, record),
           }
         },
        //  {
        //    label: '编辑',
        //    onClick: handleEdit.bind(null, record),
        //  }
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
         {
           label: '详情',
           onClick: handleDetail.bind(null, record),
         }, {
           label: '删除',
           popConfirm: {
             title: '是否确认删除',
             confirm: handleDelete.bind(null, record),
           }
         }
       ]
   }

   function goToFile(record){
    if(!!record.filepath){
      let url = getFileAccessHttpUrl(record.filepath);
      window.open(url);
    } else {
      message.warning("没有找到要下载的文件")
    }
  }

  
  function handleCaluculatAllMetric(){
    caluculatAllMetric()
  }


  function handleShowReport(row) {
    if (row.filepath) {
      openFileContentModel(true, {
        record: row,
        isUpdate: false,
        showFooter: true,
      });
    }
  }



</script>

<style scoped>

</style>