/*
 * @Author: your name
 * @Date: 2022-04-28 16:12:05
 * @LastEditTime: 2022-04-28 16:13:10
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /MedicalWisdomCheckSys/src/pages/GradeHospitalAccreditation/articleManagement/components/articleDetailModule/drawer/fragment/setArticle.tsx
 */

import MccsDrawerForm from '@/components/MccsDrawerForm/index';
import {ProFormInstance } from '@ant-design/pro-form';
import MccsClickableTabs from '@/components/MccsClickableTabs/index';
import { useEffect, useRef, useState } from 'react'

import { Form, Row, Col } from 'antd'
import { useModel } from 'umi';
import MultiResponsibilitySelecter from './multi';

//条文设置/表格批量设置drawer弹窗

const SetArticle = () => {

    const { articleManagement } = useModel('allModels');

    const {
        isLeaf,
        treeData,
        getTableData,
        currentActivedTree,
        setCurrentActivedTree,
        scoreList,
        getDepartmentRequest,

        ...restModelData
    } = articleManagement;

    const drawerFormRef = useRef<ProFormInstance>();

    const [ifsetTargetScore, setifsetTargetScore] = useState(true); //设置目标分数是否必填
    const [rows,setrows] = useState(0);
    const [respDepartmentList,setrespDepartmentList] = useState<{label:string;value:any}[]|[]>([]);  //可选单位


    const onVisibleChangeHandle = (visible: boolean) => {
        //drawer展示控制
        if (visible) {
            if (restModelData.leafData) {
                // getDepartmentUsers(restModelData.leafData?.reviewArticle.responsibilityDepartmentId);
            }
        }
        !visible && restModelData.setArticleSettingFormInit(undefined);//清空默认值
        restModelData.setDrawerVisible(visible);
    }

    const setTitle = () => {

        if (restModelData.drawerFormType == 'ARTICLE_SET') {
            return '条文设置'
        }
        if (restModelData.drawerFormType == 'BATCHSETTING') {
            return '批量设置'
        }
        return ''
    }

    const onFinishHandle = (values: any) => {

        if (restModelData.drawerFormType == 'ARTICLE_SET') {
            restModelData.setArticle(values)
        }
        if (restModelData.drawerFormType == 'BATCHSETTING') {
            restModelData.batchSetting(values)
        }
    }

    const setRespDepartment =async () => {
          const data = await getDepartmentRequest();
          setrespDepartmentList(data);
    }


    useEffect(() => {
        if (articleManagement.articleSettingFormInit && articleManagement.articleSettingFormInit.targetScores) {
            setifsetTargetScore(true);
        } else {
            setifsetTargetScore(false);
        }
    }, [articleManagement.articleSettingFormInit?.targetScores]);



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



    return (
        <MccsDrawerForm
            title={setTitle()}
            width={500}
            formRef={drawerFormRef}
            visible={restModelData.drawerVisible}
            initialValues={restModelData.drawerFormType == 'ARTICLE_SET' ? { 
                ...articleManagement.articleSettingFormInit,
                main_DepartmentAndManager:[{
                    responsibilityDepartment:articleManagement.articleSettingFormInit?.responsibilityDepartment,
                    responsibilityUser:articleManagement.articleSettingFormInit?.responsibilityUser
                }]
            } : undefined}
            onFinish={(values: any) => onFinishHandle({...values,targetScores:ifsetTargetScore?values.targetScores:{label:'',value:''}})}
            onVisibleChange={onVisibleChangeHandle}
        >
            <div>
                <div className='formItemTitle require'>负责人及单位</div>
                <Row >
                    <Col span={24}>
                           <Form.Item name='main_DepartmentAndManager'>
                               <MultiResponsibilitySelecter options={respDepartmentList}  rows={1}  />
                           </Form.Item>
                    </Col>
                </Row>
                

                <div className='formItemTitle'>合作负责人及单位<span onClick={() => setrows(rows+1)}>添加</span></div>
                <Row >
                    <Col span={24}>
                           <Form.Item name='Co_DepartmentAndManager'>
                               <MultiResponsibilitySelecter options={respDepartmentList} editable={true}  rows={rows}  />
                           </Form.Item>
                    </Col>
                </Row>

                <div className={ifsetTargetScore ? 'formItemTitle require' : 'formItemTitle'}>目标分数{ifsetTargetScore && <span onClick={() => setifsetTargetScore(false)}>暂不设置</span>}</div>
                <Row >
                    <Col span={24}>
                        <Form.Item name='targetScores' rules={[
                            {
                                required: true,
                                message: '请选择目标分数!',
                            },
                        ]}>
                            <MccsClickableTabs
                                clear={!ifsetTargetScore}
                                data={restModelData.targetScores}
                                tabClickHandle={(val)=>{setifsetTargetScore(true)}}
                            />
                        </Form.Item>
                    </Col>
                </Row>
                {
                    true && (
                        <>
                            <div className='formItemTitle require'>条文组别</div>
                            <Row>
                                <Col span={24}>
                                    <Form.Item name='articleType' rules={[
                                        {
                                            required: true,
                                            message: '请选择条文组别!',
                                        },
                                    ]}>
                                        <MccsClickableTabs
                                            data={restModelData.ruleTypes}
                                        />
                                    </Form.Item>
                                </Col>
                            </Row>
                        </>
                    )
                }
            </div>
        </MccsDrawerForm>
    )
}

export default SetArticle;
