/*
 * Creator:     ML
 * CreatDate:   2020-04-07
 * descripts：  居民健康管理签约居民分析
 * */
import React from 'react';
import moment from 'moment';
import { dayFormat } from 'tools/index';
import { Form, Col, Select, Icon, Tooltip, Button, Row, Cascader,Radio } from 'antd';
import RangeDatePicker from 'components/rangePicker/index';
import { $http } from 'containers/config/https';
import AnalysisCharts from './AnalysisCharts.jsx';//图表
//组件const
const Option = Select.Option;
const FormItem = Form.Item;
const padding = {
    padding: "0px"
}
const formItemLayout = { // label 和input宽度
    labelCol: { span: 3 },
    wrapperCol: { span: 20 }
}
let formItem815 = {
    labelCol: { span: 8 },
    wrapperCol: { span: 16 }
};

let formItem717 = {
    labelCol: { span: 7 },
    wrapperCol: { span: 17 }
};
let formItem618 = {
    labelCol: { span: 6 },
    wrapperCol: { span: 18 }
};
const formItem614 = {
    labelCol: { span: 6 },
    wrapperCol: { span: 14 }
};
const AnalysisOfContractedResidents = Form.create()(
    class AnalysisOfContractedResidents extends React.Component {
        constructor(props) {
            super(props);
            let userData = sessionStorage.getItem("userData") ? JSON.parse(sessionStorage.getItem("userData")) : {};
            var date = new Date();
            this.state = {
                userData,
                diseaseArr: [],
                diseaseSubArr: [],
                typeArr: [],
                StartDate:moment().startOf('year'),  // //开始日期
                EndDate:  moment().endOf('year').endOf('year'),  // //结束日期
                advicelogMessage: "",
                advicelogVisible: false,
                requid: this.props.requireds,
                requireds: true,
                typeID:"02",
                doctorOptionID:""
            };
        }

        componentWillReceiveProps(nextProps) {
        }
        componentDidMount = () => {
            this.props.onAnalysisOfContractedResidents(this)
           this.loadInit()
        }
        loadInit=()=>{
            this.loadDiseaseType()
            // this.loadDiseaseArr()
            this.findData()
        }
        loadDiseaseArr = () => {
            let datas = {}
            datas = {
                params: [{
                    hospID: sessionStorage.getItem("userData") ? JSON.parse(sessionStorage.getItem("userData")).hospID : " ",
                    locID: sessionStorage.getItem("userData") ? JSON.parse(sessionStorage.getItem("userData")).locID : " ",
                }],
            }
            $http.post('urlS', this, {
                code: '03070033',
                data: datas,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            diseaseArr: res.result.data
                        })
                    }
                }
            })
        }
        loadDiseaseType() {
            var params = {
                params: [{}]
            }
            $http.post('urlDeault', this, {
                code: "03070018",
                data: params,
                success: function (res) {
                    if (res.errorCode == '0') {
                        this.setState({
                            diseaseType: res.result.fatherArr,
                        })
                    }
                }
            });
        }
        //级联选择
        onChangeDisease(e, selectedOptions) {
            console.log("selectedOptions",selectedOptions)
            if (selectedOptions.length > 1) {
                this.setState({
                    diseaseCode: selectedOptions[1].code
                });
            } else if (selectedOptions.length === 1) {
                this.setState({
                    diseaseCode: selectedOptions[0].code
                });
            }
            else {
                this.setState({
                    diseaseCode: ''
                });
            }
        }

        //开始日期
        startChange(date) {
            this.setState({
                StartDate: date,
            })
        }
        //结束日期
        endChange(date) {
            this.setState({
                EndDate: date,
            })
        }
        onRefPeople=(ref)=>{
            this.childPeople=(ref)
        }
        // handleObj = (s, data) => {
        //     let obj = {
        //         0: () => this.setState({ doctorData: data },()=>{this.childPeople.getData(data)}),
        //         1: () => this.setState({ sexData: data },()=>{this.childPeople.getData(data)}),
        //         2: () => this.setState({ BMIData: data },()=>{this.childPeople.getData(data)}),
        //         3: () => this.setState({ ageData: data },()=>{this.childPeople.getData(data)}),
        //     }
        //     if (obj[s]) {
        //         obj[s]()
        //     }
        // }

        getData(code,data){
            return new Promise((resolve,reject)=>{
                $http.post('urlS', this, {
                    code: code,
                    data: data,
                    success: function (res) {
                        if (+res.errorCode === 0) {
                            resolve(res.result);
                        }
                    },
                })
            })
        }
        //查询图表
        findData = () => {
            let data = {}
            data = {
                params: [{
                    hospID:this.state.userData&&this.state.userData?this.state.userData.hospID:"",
                    docID: this.props.form.getFieldValue("doctorID") || "",
                    assessItemCode: this.state.diseaseCode || "",
                    type:this.props.form.getFieldValue("typeID") || "",
                    dateFrom: this.state.typeID=="02"?moment(this.state.StartDate).format("YYYY-MM-DD"):undefined,
                    dateTo:  this.state.typeID=="02"?moment(this.state.EndDate).format("YYYY-MM-DD"):undefined,
                }],
            }
            Promise.all([this.getData("03070095",data),this.getData("03070096",data)]).then(res=>{
                // console.log("===1",res);
                this.childPeople.getData(
                    res[0]&& res[0],res[0]&&res[0].patCount,
                    res[1]&&res[1],res[1]&&res[1].patCount,
                    res[1]&&res[1],res[1]&&res[1].patCount,
                    res[1]&&res[1],res[1]&&res[1].patCount,
                    )
                    this.setState({
                        diseaseArr:res[0]&&res[0].data || []
                    })
            }).catch(res=>{
                console.log("res",res)
            })

        }
        handleChange=(e)=>{
            if (e.target) {
                this.setState({
                    typeID:e.target.value
                },()=>{
                    this.findData()
                })
            }
        }
           //开始日期
           startChange(date) {
            this.setState({
                StartDate: date,
            })
        }
        //结束日期
        endChange(date) {
            this.setState({
                EndDate: date,
            })
        }
        render() {
            const { getFieldDecorator } = this.props.form;
            const { collArr, diseaseArr, diseaseSubArr, diseaseType, doctorData, statusArr, columns, widthTotal } = this.state;
            var date = new Date();
            let searchColConfig = {
                xs: { span: 24 },
                sm: { span: 12 },
                md: { span: 10 },
                lg: { span: this.props && this.props.modal ? 8 : 6 }
            }
            //疾病类型
            var diseaseTypeOption = []
            diseaseType && diseaseType.map((item, index) => {
                diseaseTypeOption.push(<Option key={index} value={item.id} name={item.descriptsSPCode}
                    code={item.descriptsSPCode}>{item.descripts}</Option>)
            })
            //责任医生
            var doctorOption = []
            diseaseArr && diseaseArr.map((item, index) => {
                doctorOption.push(<Option key={index} value={item.docID} onClick={()=>this.setState({
                    doctorOptionID:item.docID
                })} 
                name={item.docDesc}
                code={item.docDesc}>{item.docDesc}</Option>)
            })

            return (
                <div className="viewSelectDom">
                    <Row>
                        <Form>
                        <Col span={4}>
                                <FormItem {...formItem717} label={<span>
                                    <span style={{ marginLeft: 5 }}>居民类型</span></span>}>
                                    {getFieldDecorator('typeID', {
                                        initialValue: "02",
                                    })(
                                        <Radio.Group onChange={(e) => this.handleChange(e)} value={this.state.typeID}>
                                        <Radio value={"02"} >体检居民</Radio>
                                        <Radio value={"01"} >签约居民</Radio>
                                    </Radio.Group>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={4}>
                                <FormItem {...formItem618} label={<span>
                                    <span style={{ marginLeft: 5 }}>责任医生</span></span>}>
                                    {getFieldDecorator('doctorID', {
                                        initialValue: this.state.type,
                                    })(
                                        <Select style={{ width: '189px' }}
                                            allowClear
                                            showSearch
                                            filterOption={(input, option) =>
                                                option.props.name.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                                || option.props.code.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                        >
                                            {doctorOption}</Select>
                                    )}
                                </FormItem>
                            </Col>
                         
                            <Col {...searchColConfig} style={{ padding: "4px 0",display:(this.state.typeID=="02"?"block":"none") }}>
                                <RangeDatePicker isChange={true} label={ '体检日期：'} format='YYYY-MM-DD' width={344}
                                    startDate={this.state.StartDate} endDate={this.state.EndDate}
                                    startChange={this.startChange.bind(this)} endChange={this.endChange.bind(this)} />
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItem614} label={<span><span>疾病类型</span></span>}>
                                    {getFieldDecorator('a1001', {
                                        // initialValue: this.state.,
                                    })(
                                        <Cascader style={{ width: '106%' }}
                                            options={this.state.diseaseType}
                                            onChange={this.onChangeDisease.bind(this)}
                                            changeOnSelect
                                        />


                                    )}
                                </FormItem>
                            </Col>
                            <Col span={2} style={{ paddingLeft: "0" }}>
                                <FormItem  {...formItemLayout}>
                                    {getFieldDecorator('findData', {
                                        initialValue: undefined,
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <div style={{ display: "flex", marginTop: "4px" }}>
                                            <Button style={{ margin: "0 18px 0 18px" }} onClick={this.findData} type="primary">查询</Button>
                                        </div>
                                    )}
                                </FormItem>
                            </Col>
                        </Form>
                    </Row>
                    <Row>
                        <AnalysisCharts findData={this.findData} doctorData={this.state.doctorData} onRefPeople={this.onRefPeople} handThis={this}></AnalysisCharts>
                    </Row>
                </div>
            );
        }
    })
export default AnalysisOfContractedResidents
