/*
 * @Author: your name
 * @Date: 2021-09-06 17:42:57
 * @LastEditTime: 2021-12-30 17:02:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /MedicalWisdomCheckSys/src/pages/GradeHospitalAccreditation/accreditationDetail/model.ts
 */

// accreditationDetail

import { useState } from 'react';
import { getTreeList, delDetailRule, addDetailRule,editDetailRule,getDetailRuleList } from './service';
import {getScoreLevelData} from '@/pages/PlatformMana/generalSetting/serve';


const accreditationDetail = () => {
  const [treeData, setTreeData] = useState<MccsFileTree.childTree[]>([]);
  const [detailRuleList,setDetailRuleList] = useState<API.DetailRuleList[]>([]);

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

    setTreeData(list);
  };

  const addDetailRuleHandle = async (data: API.AddDetailRule) => {
    await addDetailRule(data);
    getTreeData();
  };

  const delDetailRuleHandle = async (id: string | number) => {
    await delDetailRule(id);
    getTreeData();
  };

  const editDetailRulehandle = async (data: API.EditDetailRule)=>{
     const resp = await editDetailRule(data);
     if(resp){
      getTreeData(); // 更新树
      getDetailRuleListHandle(data.id); // 重新获取
     }
  }

  const getDetailRuleListHandle = async (id: number|string)=>{
     const {leafDetails=[]} = await getDetailRuleList(id);
     setDetailRuleList(leafDetails);
  }

  const getScoreSelectableList = async ()=>{
     // 获取表单下拉评分数据
     return await getScoreLevelData();
  }


  return { 
    treeData, setTreeData, getTreeData, delDetailRuleHandle, addDetailRuleHandle,
    editDetailRulehandle ,detailRuleList,getDetailRuleListHandle,setDetailRuleList,getScoreSelectableList
  };
};

export default accreditationDetail;
