<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <PermissionBtn @btnEvent="buttonClick"></PermissionBtn>
      </template>
    </BasicTable>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted, reactive, watch, createVNode, unref } from 'vue';
  import { useModal } from '@/components/Modal';
  import { BasicTable, useTable } from '@/components/Table';
  import { PermissionBtn } from '@/components/PermissionBtn/index';
  import { PageWrapper } from '@/components/Page';
  import { cloneDeep } from 'lodash-es';
  import { CaseOffenceMineralsNew } from '@/api/demo/system';
  import { columnsNew, searchFormSchema } from './index.data';
  import { useMessage } from '@/hooks/web/useMessage';
  import { getAppEnvConfig } from '@/utils/env';
  import axios from 'axios';
  import dayjs from 'dayjs';

  const { createMessage } = useMessage();
  const { VITE_GLOB_API_URL } = getAppEnvConfig();

  // 页面表格
  const searchParams = ref();
  const [registerTable] = useTable({
    api: CaseOffenceMineralsNew,
    columns: columnsNew,
    // title: '无人机全域巡查线索情况统计表',
    // titleHelpMessage: '合法、其他、非粮化、补办手续、拆除复耕均需市级审核通过才可判定为举证类型。',
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    showIndexColumn: false,
    striped: false,
    bordered: true,
    canResize: false,
    useSearchForm: true,
    showTableSetting: true,
    pagination: false,
    beforeFetch: (data) => {
      // 接口请求前 参数处理
      // console.log(data);
      // 第一次进入页面-默认时间
      if (Object.keys(data).length == 0) {
        var temp = {
          startTime: dayjs('2024-08-01').startOf('day').format('YYYY-MM-DD'),
          endTime: dayjs().endOf('day').add(-1, 'day').format('YYYY-MM-DD HH:mm:ss'),
          tubanlaiyuan: data.tubanlaiyuan && data.tubanlaiyuan.join(',')
        };
        searchParams.value = temp;
        return temp;
      }
      // 时间选择改变前-默认时间
      if (data.startTime === undefined && data.endTime === undefined) {
        var temp = {
          startTime: dayjs('2024-08-01').startOf('day').format('YYYY-MM-DD'),
          endTime: dayjs().endOf('day').add(-1, 'day').format('YYYY-MM-DD HH:mm:ss'),
          tubanlaiyuan: data.tubanlaiyuan && data.tubanlaiyuan.join(',')
        };
        searchParams.value = temp;
        return temp;
      }
      // 时间选择改变后-起始时间和结束时间格式和精确值
      if (data.startTime && data.endTime) {
        var temp = {
          startTime: dayjs(data.startTime).startOf('day').format('YYYY-MM-DD'),
          endTime: dayjs(data.endTime).endOf('day').format('YYYY-MM-DD HH:mm:ss'),
          tubanlaiyuan: data.tubanlaiyuan && data.tubanlaiyuan.join(',')
        };
        searchParams.value = temp;
        return temp;
      }
      // 时间选择点X后
      if (data.startTime === null && data.endTime === undefined) {
        searchParams.value = {};
        var temp = {
          tubanlaiyuan: data.tubanlaiyuan && data.tubanlaiyuan.join(',')
        };
        searchParams.value = temp;
        return temp
      }
    },
    afterFetch: (res) => {},
  });

  // 直接下载接口返回的二进制流
  function handleExport(exportType) {
    let url = '';
    let fileName = '';
    let params: any = {};
    let afterTitle = '';

    if (searchParams.value?.startTime === undefined && searchParams.value?.endTime === undefined) {
      afterTitle = afterTitle;
    } else if (
      dayjs(searchParams.value?.startTime).startOf('day').format('YYYY-MM-DD') ===
      dayjs(searchParams.value?.endTime).endOf('day').format('YYYY-MM-DD')
    ) {
      afterTitle = dayjs(searchParams.value?.startTime).startOf('day').format('YYYY-MM-DD');
    } else {
      afterTitle =
        dayjs(searchParams.value?.startTime).startOf('day').format('YYYY-MM-DD') +
        '至' +
        dayjs(searchParams.value?.endTime).endOf('day').format('YYYY-MM-DD');
    }

    switch (exportType) {
      case '':
        url = VITE_GLOB_API_URL + '/api/DroneCaseInfoMinerals/CaseOfMineralsToExcelNew';
        fileName = '汇总表 ' + afterTitle + '.xls';
        break;
      default:
        break;
    }

    if (
      !(searchParams.value?.startTime === undefined && searchParams.value?.endTime === undefined)
    ) {
      params = {
        startTime: dayjs(searchParams.value?.startTime).startOf('day').format('YYYY-MM-DD'),
        endTime: dayjs(searchParams.value?.endTime).endOf('day').format('YYYY-MM-DD HH:mm:ss'),
        tubanlaiyuan: searchParams.value?.tubanlaiyuan
      };
    }else{
      params = {
        tubanlaiyuan: searchParams.value?.tubanlaiyuan
      };
    }

    axios({
      method: 'post',
      url: url,
      params: params,
      headers: {
        'X-Token': localStorage.getItem('X-Token'),
      },
      responseType: 'blob',
    }).then((res) => {
      const elink = document.createElement('a');
      elink.download = fileName;
      elink.style.display = 'none';
      elink.href = URL.createObjectURL(res.data);
      document.body.appendChild(elink);
      elink.click();
      URL.revokeObjectURL(elink.href);
      document.body.removeChild(elink);
    });
  }
  // 按键分类
  const buttonClick = async (type) => {
    switch (type) {
      case 'btnExport':
        handleExport('');
        break;
      default:
        break;
    }
  };
</script>
<style lang="less" scoped>
  #closeIconButton {
    width: 40px;
    height: 50px;
    line-height: 50px;
    float: right;
    text-align: center;
    cursor: pointer;
  }

  .exportbutton {
    width: 510px;
    height: 60px;
    margin: 5px;
  }

  ::v-deep .ant-table-container table {
    line-height: 1.1 !important;

    .ant-table-row {
      td {
        padding-top: 8px !important;
        padding-bottom: 8px !important;
      }
    }
  }
</style>
