/*
 * @Author: your name
 * @Date: 2021-09-14 10:22:09
 * @LastEditTime: 2021-12-27 17:44:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /MedicalWisdomCheckSys/src/pages/GradeHospitalAccreditation/articleManagement/index.tsx
 */


import React, { useEffect,useState } from 'react'
import {useModel} from 'umi';
import { Form,Tabs,Button } from 'antd'
import {
    ProFormText,
    ProFormDateRangePicker,
} from '@ant-design/pro-form';

import ArticleManagementCopy from '@/pages/GradeHospitalAccreditation/articleManagement/index';
import MccsDrawerForm from '@/components/MccsDrawerForm/index';
import './index.less';
import MccsScoreBanner from '@/components/MccsScoreBanner';
import MccsRecordList from '@/components/MccsRecordList/index';
import MccsClickableTabs from '@/components/MccsClickableTabs/index';

import 'moment/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';


type LedgerUploadPropsType = {

}

const { TabPane } = Tabs;

//条文管理
const LedgerUpload: React.FC<LedgerUploadPropsType> = (props) => {

    const columns = [
        // {
        //     key: 'articleType',
        //     hideInTable: true,
        //     renderFormItem: (item: any, { type }: any, form: any) => {
    
        //         if (type === 'form') {
        //             return null;
        //         }
        //         return (
                    
        //                 <ProFormSelect
        //                     width='sm'
        //                     options={[
        //                         { label: '全部', value: 'all' },
        //                         { label: '未解决', value: 'open' },
        //                         { label: '已解决', value: 'closed' },
        //                         { label: '解决中', value: 'processing' },
        //                     ]}
        //                     placeholder="台账状态"
        //                 />
                   
        //         )
        //     },
        // },
        {
            hideInTable: true,
            renderFormItem: (item: any, { type }: any, form: any) => {
    
                if (type === 'form') {
                    return null;
                }
                return (
                   
                        <ProFormDateRangePicker name='date'  placeholder={['开始时间','结束时间']} fieldProps={{locale:locale}} />
                  
                )
            },
        },
        {
            key: 'keyword',
            hideInTable: true,
            renderFormItem: (item: any, { type }: any, form: any) => {
    
                if (type === 'form') {
                    return null;
                }
                return (
                    <Form.Item name='keyword'>
                        <ProFormText placeholder='条文/条款' />
                    </Form.Item>
                )
            },
        },
        {
            title: '条款',
            key: 'numStr',
            dataIndex: 'numStr',
            hideInSearch: true,
    
        },
        {
            title: '条文',
            dataIndex: 'name',
            hideInSearch: true,
            width: '35%'
        },
        {
            title: '负责单位',
            dataIndex: 'responsibilityDepartmentName',
            hideInSearch: true,
        },
        {
            title: '负责人',
            dataIndex: 'responsibilityUserName',
            hideInSearch: true,
        },
        {
            title: '自评',
            hideInSearch: true,
            dataIndex: 'selfEvaluation',
        },
        // {
        //     title: '现场查核',
        //     hideInSearch: true,
        //     dataIndex: 'siteEvaluation',
        // },
        {
            title: '目标',
            dataIndex: 'targetEvaluation',
            hideInSearch: true,
        },
        {
            title: '系统评分',
            dataIndex: 'systemEvaluation',
            hideInSearch: true,
        },
        {
            title: '操作',
            width: 100,
            valueType: 'option',
            render: (_:any, record:any) =>{
                   
                   return [<a key="1" onClick={()=>openDrawerHandle(record)}>详情</a>, <a key="2" onClick={()=>del(record)}>删除</a>]
            },
        },
    ]

    const { articleManagement,ledgerUpload } = useModel('allModels',model=>{
            return {articleManagement:model.articleManagement,ledgerUpload:model.ledgerUpload};
    });

    const {
 
        currentActivedTree,
        ...restArticleManagementModelData
    } = articleManagement;
    
    const {...restLedgerUploadModelData} = ledgerUpload;

    const [tabKey,setTabKey] = useState<string|number>();  //antd Tab切换


    const openDrawerHandle = (record:any)=>{
        // console.log({record});
        restLedgerUploadModelData.setCurrentRecord(record)
        restLedgerUploadModelData.setDrawerVisible(true);
        restArticleManagementModelData.getTableData({id:record.id});
    }

    const del = (record:any)=>{

        const {id} = record;
        restLedgerUploadModelData.delRuleHandle(id);
        restArticleManagementModelData.getArticleTreeData();  //刷新树结构
        restArticleManagementModelData.setReloadRightContent(true);  //刷新表格数据
    }

    const onVisibleChangeHandle = (visible: boolean) => {
        restLedgerUploadModelData.setDrawerVisible(visible);
    }

    const upLoadFileHandle = (formInitData?:any) => {
        //因为modalForm开启了request+params获取表单默认值，
        //所以必须先设置默认值才能开启弹窗
        if(formInitData){
            const { fileUrl, recordTypeName, fileName, accountType,cateId } = formInitData;
            restArticleManagementModelData.setUploadFormInitValue({
                tab: { label: accountType, value: { label: recordTypeName, value:cateId } },
                files: [{
                    uid: new Date().getTime(),
                    name: fileName,
                    status: 'done',
                    url: fileUrl
                }]
            });
        }else {
            restArticleManagementModelData.setUploadFormInitValue({})
        }

        restArticleManagementModelData.getUploadLedgerType().then(data=>{
            //设置上传台账可选类型
            restArticleManagementModelData.setUploadLedgerTypeList(data);
          
        })
        restArticleManagementModelData.setModalVisible(true);
    }

    const actionNodeFunc = (data: any) => {
        //渲染记录列表操作按钮
        // console.log({ data });
        const reupload = () => {
            
            upLoadFileHandle(data);
        }

        return <a onClick={reupload}>重新上传</a>
    }

    useEffect(()=>{
        //监听上传台账弹窗变化，更新本地drawer里台账记录
        if(!restArticleManagementModelData.modalVisible&&restLedgerUploadModelData.currentRecord){
            restArticleManagementModelData.getTableData({id:restLedgerUploadModelData.currentRecord.id});
        }

    },[restArticleManagementModelData.modalVisible]);

    
    
    return (
        <React.Fragment>
            <ArticleManagementCopy 
                columnsFromProps={columns}
                isModeTwo={true}
            />

            
            <MccsDrawerForm 
               title={restLedgerUploadModelData.currentRecord?.name}
               visible = {restLedgerUploadModelData.drawerVisible}
               onVisibleChange={onVisibleChangeHandle}
            >
                <div className='leafContent'>
                
                                <div className='peopleGroup'>
                                    <span>条文组别：</span>
                                    <span>{restLedgerUploadModelData.currentRecord?.accountType}</span>
                                    <span>负责单位：</span>
                                    <span>{restLedgerUploadModelData.currentRecord?.responsibilityDepartmentName}</span>
                                    <span>负责人：</span>
                                    <span>{restLedgerUploadModelData.currentRecord?.responsibilityUserName}</span>
                                </div>
                                <MccsScoreBanner list={restLedgerUploadModelData.scoreList} />
                       
                                <div style={{ marginTop: 5 }}>
                                            <Tabs defaultActiveKey="1" centered size='large' onChange={(key) => {key == '2'&&restLedgerUploadModelData.getSelectableSelfScoreList();setTabKey(key);}}>
                                                <TabPane tab="台账记录" key="1">
                                                    <div className='articleSetBtn'>
                                                        <span onClick={()=>upLoadFileHandle()}>上传文件</span>
                                                    </div>
                                                    <MccsRecordList
                                                        title='台账记录'
                                                        list={restArticleManagementModelData.records}
                                                        actionNode={(data) => actionNodeFunc(data)}
                                                    />
                                                </TabPane>
                                                <TabPane tab="自评结果" key="2">
                                                    <div className='selfEvolutionTitle'>开始自评</div>
                                                    <div className='selfEvolutionArea'>
                                                        <MccsClickableTabs
                                                            classNames='tabWrap'
                                                            onChange={(value:{label:string,value:any})=>restLedgerUploadModelData.onSelfEvolutionTabChange(value)}
                                                            data={restLedgerUploadModelData.targetScores}
                                                            value={restLedgerUploadModelData.selectedSelfEvolution}
                                                        />
                                                        <Button onClick={()=>restLedgerUploadModelData.commitSelfEvolution()}>提交自评</Button>
                                                    </div>
                                                    <MccsRecordList
                                                        title='自评记录'
                                                        list={restLedgerUploadModelData.selfEvaluationRecords}
                                                    />
                                                </TabPane>
                                            </Tabs>
                                </div>
                </div>
            </MccsDrawerForm>
        </React.Fragment>
    )
}

export default LedgerUpload