/*
 * @Author: your name
 * @Date: 2021-09-27 10:41:52
 * @LastEditTime: 2021-09-29 18:07:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /MedicalWisdomCheckSys/src/pages/PlatformMana/generalSetting/index.tsx
 */


import React, { useState } from 'react'
import MccsPageContainer from '@/components/MccsPageContainer/index';
import MccsEditableTable from '@/components/MccsEditableTable/index';
import { Checkbox } from 'antd';
import { getAccreditationTypeData, addTableData, editTableData, getScoreLevelData,getLedgerType,delTableData } from './serve';

import './index.less';


type GeneralSettingType = {

}

const GeneralSetting: React.FC<GeneralSettingType> = () => {

    const [currentType, setCurrentType] = useState<number>();
    const [reloadTableOne, setReloadTableOne] = useState(false);
    const [reloadTableTwo, setReloadTableTwo] = useState(false);
    const [reloadTableThree, setReloadTableThree] = useState(false);

    const accreditationColumns = [
        {
            title: '序号',
            key: 'id',
            dataIndex: 'id',
            editable: false
        },
        {
            title: '名称',
            key: 'generalName',
            dataIndex: 'generalName',
        },
        {
            title: '组别类型',
            key: 'categoryType',
            dataIndex: 'categoryType',
        },
        {
            title: '操作',
            valueType: 'option',
            width: 200,
            render: (text: any, record: any, _: any, action: any) => [
                <a
                    key="editable"
                    onClick={() => {
                        action?.startEditable?.(record.id);
                    }}
                >
                    编辑
                </a>,
                <a
                    key="delete"
                    onClick={() => {
                        delData(record.id,1)
                    }}
                >
                    删除
                </a>,
            ],
        },
    ];
    const scoreLevelColumns = [
        {
            title: '序号',
            key: 'id',
            dataIndex: 'id',
            editable: false
        },
        {
            title: '等级分数',
            key: 'levelNumber',
            dataIndex: 'levelNumber',
        },
        {
            title: '要求',
            key: 'requirements',
            dataIndex: 'requirements',
        },
        {
            title: '备注',
            key: 'configNote',
            dataIndex: 'configNote',
            ellipsis:true,
            width:'40%'
        },
        {
            title: '是否启用',
            key: 'configStatus',
            dataIndex: 'configStatus',
            valueType: 'select',
            width:'10%',
            valueEnum: {
                1: {
                    text: '是',
                    value:1
                },
                0: {
                    text: '否',
                    value:0
                },
            },

        },
        {
            title: '操作',
            valueType: 'option',
            width: 200,
            render: (text: any, record: any, _: any, action: any) => [
                <a
                    key="editable"
                    onClick={() => {
                        action?.startEditable?.(record.id);
                    }}
                >
                    编辑
                </a>,
                <a
                    key="delete"
                    onClick={() => {
                        delData(record.id,2)
                    }}
                >
                    删除
                </a>,
            ],
        },
    ];

    const uploadLedgerColumns = [
        {
            title: '序号',
            key: 'id',
            dataIndex: 'id',
            editable: false
        },
        {
            title: '等级分数',
            key: 'levelNumber',
            dataIndex: 'levelNumber',
        },
        {
            title: '类型名称',
            key: 'generalName',
            dataIndex: 'generalName',
        },
        {
            title: '备注',
            key: 'configNote',
            dataIndex: 'configNote',
            width:'40%',
            ellipsis:true
        },
        {
            title: '是否启用',
            key: 'configStatus',
            dataIndex: 'configStatus',
            valueType: 'select',
            width:'15%',
            valueEnum: {
                1: {
                    text: '是',
                    value:1
                },
                0: {
                    text: '否',
                    value:0
                },
            },

        },
        {
            title: '操作',
            valueType: 'option',
            width: 200,
            render: (text: any, record: any, _: any, action: any) => [
                <a
                    key="editable"
                    onClick={() => {
                        action?.startEditable?.(record.id);
                    }}
                >
                    编辑
                </a>,
                <a
                    key="delete"
                    onClick={() => {
                        delData(record.id,3)
                    }}
                >
                    删除
                </a>,
            ],
        },
    ];

    const getTableDataRequest = async () => {

        const respData = await getAccreditationTypeData();
        if (respData) {
            setReloadTableOne(false);
            return {
                data: respData,
                success: true
            }
        }
    }

    const getLevelScoreTableDataRequest = async () => {
        //获取等级分数
        const respData = await getScoreLevelData();
        if (respData) {
            const _respData = respData.map((t:any)=>({...t,configStatus:`${t.configStatus}`})); //Columns的key只可以是string
            setReloadTableTwo(false);
            return {
                data: _respData,
                success: true
            }
        }
    }

    const getLedgerTableDataRequest = async () => {
        //获取台账类型数据
        const respData = await getLedgerType();
        if (respData) {
            const _respData = respData.map((t:any)=>({...t,configStatus:`${t.configStatus}`}));//Columns的key只可以是string
            setReloadTableThree(false);
            return {
                data: _respData,
                success: true
            }
        }
    }



    type dataType = {
        id: string | number,
        categoryType?: string,
        configNote?: string,
        configStatus?: number,
        generalName?: string,
        levelNumber?: string,
        requirements?: string
    }

    const addData = async (data: dataType, typeNum: number) => {
        const { categoryType,configStatus,requirements,levelNumber,configNote, generalName} = data;
        if (typeNum == 1) {
            //条文组别类型新增
            const postData: API.AddtableDataPostType = {
                configType: typeNum,
                categoryType,
                generalName
            }
            const resp = await addTableData(postData);
            if (resp) {
                setReloadTableOne(true);
            }
        }
        if (typeNum == 2) {
            //条文组别类型新增
            const postData: API.AddtableDataPostType = {
                configType: typeNum,
                configStatus:Number(configStatus),
                levelNumber,
                configNote,
                requirements
            }
            const resp = await addTableData(postData);
            if (resp) {
                setReloadTableTwo(true);
            }
        }
        if (typeNum == 3) {
            //条文组别类型新增
            const postData: API.AddtableDataPostType = {
                configType: typeNum,
                configStatus:Number(configStatus),
                generalName,
                levelNumber,
                configNote,
            }
            const resp = await addTableData(postData);
            if (resp) {
                setReloadTableThree(true);
            }
        }

    }

    const delData = async (id:number,typeNum:number)=>{
          const resp = await delTableData(id);
          if(resp){
            typeNum==1&&setReloadTableOne(true);
            typeNum==2&&setReloadTableTwo(true);
            typeNum==3&&setReloadTableThree(true);
          }
    }

    const editDataHandle = async (data: dataType, typeNum: number) => {
        const { categoryType,configStatus,requirements,levelNumber,configNote, generalName, id } = data;
        if (typeNum == 1) {
            //条文组别类型新增
            const postData: API.EdittableDataPostType = {
                configType: typeNum,
                categoryType,
                generalName,
                id: Number(id)
            }
            const resp = await editTableData(postData);

            if (resp) {
                setReloadTableOne(true);
            }
        }
        if (typeNum == 2) {
            //条文组别类型新增
            const postData: API.EdittableDataPostType = {
                configType: typeNum,
                configStatus:Number(configStatus),
                levelNumber,
                configNote,
                id: Number(id),
                requirements
            }
            const resp = await editTableData(postData);
            if (resp) {
                setReloadTableTwo(true);
            }
        }
        if (typeNum == 3) {
            //条文组别类型新增
            const postData: API.EdittableDataPostType = {
                configType: typeNum,
                configStatus:Number(configStatus),
                generalName,
                levelNumber,
                configNote,
                id: Number(id)
            }
            const resp = await editTableData(postData);
            if (resp) {
                setReloadTableThree(true);
            }
        }
    }

    return (
        <MccsPageContainer>
            <div className='block'>
                <div className='blockTitle'>评审细则类型设置</div>
                <MccsEditableTable
                    columns={accreditationColumns}
                    request={getTableDataRequest}
                    addHandle={(data) => addData(data, 1)}
                    editHandle={data => editDataHandle(data, 1)}
                    reload={reloadTableOne}
                />
            </div>
            <div className='block'>
                <div className='blockTitle'>分数等级设置</div>
                <MccsEditableTable
                    columns={scoreLevelColumns}
                    request={getLevelScoreTableDataRequest}
                    addHandle={(data) => addData(data, 2)}
                    editHandle={data => editDataHandle(data, 2)}
                    reload={reloadTableTwo}
                />
            </div>
            <div className='block'>
                <div className='blockTitle'>台账类型设置</div>
                <MccsEditableTable
                    columns={uploadLedgerColumns}
                    request={getLedgerTableDataRequest}
                    addHandle={(data) => addData(data, 3)}
                    editHandle={data => editDataHandle(data, 3)}
                    reload={reloadTableThree}
                />
            </div>
        </MccsPageContainer>
    )
}

export default GeneralSetting
