/*
 * @Author: your name
 * @Date: 2021-09-23 09:02:09
 * @LastEditTime: 2022-02-14 16:56:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /MedicalWisdomCheckSys/src/pages/DataManagement/publicData/model.ts
 */

import { useState, useEffect } from 'react';
import {
  getTreeList,
  getTreeTableList,
  addTreeTableList,
  editTreeTableList,
  delTreeTableList,
  getAllAccreditionLevelFourList,
  connectAccreditation,
  publicResourceUpload,
  batchDownloadResource,
  delTableFlieList,
} from './serve';

const publicData = () => {
  const [treeData, setTreeData] = useState<MccsFileTree.childTree[]>([]);
  const [currentActivedTree, setCurrentActivedTree] = useState<SelectedTreeDataType>();
  const [currentActived, setCurrentActived] = useState<SelectedTreeDataType>();
  const [reloadTable, setReloadTable] = useState(false);

  const getTreeData = async (keyword?: string | number) => {
    const data = await getTreeList(keyword);
    setTreeData([data]);
  };

  const getTreeTableListHandle = async (params: {
    keyword: string;
    date: any;
    current: number;
    pageSize: number;
  }) => {
    //   console.log({params});
    const { current,pageSize, keyword, date = [] } = params;
    if (currentActivedTree) {
      // console.log({currentActivedTree});
      const { id, cateType } = currentActivedTree;
      if(id){
        const resp = await getTreeTableList({
          id: Number(id),
          cateType: `${cateType}`,
          current,
          keyword,
          pageSize,
          startDate: date[0],
          endDate: date[1],
        });
        const { list = [],totalCount } = resp;
        // 每次刷新完数据将是否刷新置为false
        setReloadTable(false);
        return {
          data: list,
          success: true,
          total:totalCount
        };
      }
      
    }
    return {
      data: [],
      success: true,
    }
  };

  const addTreeHandle = async (formValue: API.AddTableListRequestType) => {
    const resp = await addTreeTableList(formValue);
    if (resp) {
      getTreeData();
    }
  };

  const editTreeHandle = async (formValue: API.EditTableListRequestType) => {
    const resp = await editTreeTableList(formValue);
    if (resp) {
      getTreeData();
    }
  };

  const delTreeHandle = async (ids: number[]) => {
    const resp = await delTreeTableList(ids);
    if (resp) {
      getTreeData();
    }
  };

  const delTableFlieListFunc = async (id: number)=>{
       const resp = await delTableFlieList(id);
       if(resp){
        setReloadTable(true);
       }
  }

  const getDrawerTableList = async (params: any,ids: string) => {
    const resp = await getAllAccreditionLevelFourList({...params,id:ids});
    if (resp) {
      return {
        data: resp.list,
        success: true,
        total:resp.totalCount
      };
    }
      return {
        data: [],
        success: true,
      };
    
  };

  const batchConnectAccreditation = async (data: API.BatchConnectAccreditation) => {
      // 批量关联条款
    const resp = await connectAccreditation(data);
    if (resp) {
      setReloadTable(true);
    }
  };

  const uploadFilehandle = async (files: any) => {
    if (currentActivedTree) {
      const { id } = currentActivedTree;
      const resp = await publicResourceUpload({ id: Number(id), files });
      if (resp) {
        setReloadTable(true);
      }
    } else {
      console.log('err', 'currentActivedTree', currentActivedTree, 'files', files);
    }
  };

  const batchDownloadResourcePost = async (ids: number[]) => {
      // 批量下载
    if (currentActivedTree) {
      const { cateType } = currentActivedTree;
      if (cateType != undefined) {
        const resp = await batchDownloadResource(ids, cateType);
        if (resp) {
          const blob = new Blob([resp], { type: 'application/zip' });
          const url = window.URL.createObjectURL(blob);
          const link = document.createElement('a'); // 创建a标签
          link.href = url;
          link.click();
          URL.revokeObjectURL(url); // 释放内存
        }
      }
    }
  };

  useEffect(() => {}, [currentActivedTree]);

  return {
    treeData,
    setTreeData,
    getTreeData,
    setCurrentActived,
    currentActived,
    delTreeHandle,
    reloadTable,
    uploadFilehandle,
    setCurrentActivedTree,
    getTreeTableListHandle,
    addTreeHandle,
    editTreeHandle,
    getDrawerTableList,
    batchConnectAccreditation,
    batchDownloadResourcePost,
    delTableFlieListFunc,
  };
};

export default publicData;
