// 库存项---收费项
import React from 'react';
import {Form, DatePicker, Input, Row, Col, Card, Select} from 'antd';
import Moment from 'moment';
import { $http } from 'containers/config/https';

const FormItem = Form.Item;
let {Option} = Select;
let timeout;

class AIITollItem extends React.Component {
    constructor() {
        super();
        this.state = {
            chargeData:[],
        }
    }

    componentDidMount() {
        this.props.onRef(this);
    }

    handleValidateFields = () => {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let obj = JSON.parse(JSON.stringify(values));
                obj.errors = true;
                obj.itemID = obj.itemID === this.props.userInfo.itemDesc ? this.props.userInfo.itemID : obj.itemID;
                obj.itemStDate = obj.itemStDate ? Moment(obj.itemStDate).format("YYYY-MM-DD") : "";
                obj.itemEndDate = obj.itemEndDate ? Moment(obj.itemEndDate).format("YYYY-MM-DD") : "";
                this.props.receiveSubcomponentsValue(obj, 'tollItem')
            } else {
                let obj = JSON.parse(JSON.stringify(values));
                obj.errors = false;
                this.props.receiveSubcomponentsValue(obj, 'tollItem')
            }
        })
    }

    // 重置数据
    resetFields = () => {
        this.props.form.resetFields();
    }

    //收费项
    handleChargeSearch = (value) => {
        this.fetchCharge(value, chargeData => this.setState({ chargeData }));
    }
    
    fetchCharge = (value, callback) => {
        value=value.replace( /^\s+/, "" ).replace( /\s+$/, "" );
        if (value===""){
            let data=[];
            callback(data);
            return;
        }
        if (timeout) {
            clearTimeout(timeout);
            timeout = null;
        }
        function fakeCharge() {
            let data = {
                params: [{
                    Code: '',
                    Descripts: '',
                    FindType: '',
                    TypeID: '',
                    alias: value,
                    include:"",
                    hospID:'',
                    include:"combo",
                }],
                pagination:[{
                    pageSize: 0,
                    currentPage: 0,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
            $http.post('urlS', this, {
                code: "05100010",
                data: data,
                success: function (res) {
                    if (res.errorCode === '0') {
                        let data=[];
                        var j=0
                        for(j = 0; j < res.result.Data.length; j++) {
                            data.push({
                                value: res.result.Data[j].ID,
                                text: res.result.Data[j].itemDesc,
                            });
                        }
                        callback(data);
                    }
                }
            });
        }
        timeout = setTimeout(fakeCharge, 300);
    }

    handleOnSelect = id => {
        this.loadChargeInfo(id)
    }

    //获取下拉框信息
    loadChargeInfo(id){
        let data = {
            params: [{
                chargeDr: id,
            }],
        }
        $http.post('urlS', this, {
            code: "02020071",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.result) {
                    this.props.form.setFieldsValue({
                        itemCode:res.result[0].chargeCode,
                        itemDesc:res.result[0].chargeDesc,
                        itemTCID:res.result[0].TCDr,
                        itemPCID:res.result[0].PCDr,
                        itemICID:res.result[0].ICDr,
                        itemOCID:res.result[0].OCDr,
                        itemECID:res.result[0].ECDr,
                        itemACID:res.result[0].ACDr,
                        itemMCID:res.result[0].MCDr,
                        itemStDate:res.result[0].chargeStDate ? Moment(res.result[0].chargeStDate) : null,
                        itemEndDate:res.result[0].chargeEndDate ? Moment(res.result[0].chargeEndDate) : null,
                    })
                }
            }
        })
    }
    
    render() {
        const { getFieldDecorator } = this.props.form;
        const formItemItem  = {
            labelCol: {span: 12},
            wrapperCol: {span: 12}
        };
        let userInfo = this.props.userInfo;
        let selectData = this.props.selectData;
        let ChineseEnglishSize = this.props.ChineseEnglishSize;
        // 关联收费项
        const chargeItemOptions = this.state.chargeData.map(d => <Option title={d.text} key={d.value}>{d.text}</Option>);
        // 物价分类
        let itemPCIDOption = selectData&&selectData.PC&&selectData.PC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 核算项目分类
        let itemECIDOption = selectData&&selectData.EC&&selectData.EC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 住院收据费用
        let itemICIDOption = selectData&&selectData.IC&&selectData.IC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 会计科目
        let itemACIDOption = selectData&&selectData.AC&&selectData.AC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 门诊收据费用
        let itemOCIDOption = selectData&&selectData.OC&&selectData.OC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 病案分类
        let itemMCIDOption = selectData&&selectData.MC&&selectData.MC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        // 收费分类
        let itemTCIDOption = selectData&&selectData.TC&&selectData.TC.map((item, index) => {
            return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        })
        return (
            <Card
                type="inner"
                title={ChineseEnglishSize.DrugMaintenancetollItem ? ChineseEnglishSize.DrugMaintenancetollItem.descripts:'收费项'}
                size="small"
            >
                <Form style={{padding: '0 12px'}}>
                    <Row>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemID ? ChineseEnglishSize.DrugMaintenanceitemID.descripts:'关联收费项'} {...formItemItem}>
                                {getFieldDecorator('itemID', {
                                    initialValue: userInfo.itemDesc ? userInfo.itemDesc : undefined,
                                    rules: [{ required: false, message: '收费项是必填项,请选择！' }]
                                })(
                                    <Select
                                        allowClear={true}
                                        showSearch
                                        style={{width: '100%'}}
                                        defaultActiveFirstOption={false}
                                        showArrow={false}
                                        filterOption={false}
                                        onSearch={this.handleChargeSearch}
                                        onSelect={this.handleOnSelect}
                                        notFoundContent={null}
                                    >
                                        {chargeItemOptions}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemPCID ? ChineseEnglishSize.DrugMaintenanceitemPCID.descripts:'物价分类'} {...formItemItem}>
                                {getFieldDecorator('itemPCID', {
                                    initialValue: userInfo.itemPCID ? userInfo.itemPCID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemPCID ? ChineseEnglishSize.DrugMaintenanceitemPCID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemPCID ? ChineseEnglishSize.DrugMaintenanceitemPCID.message:'请选择物价分类'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemPCIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemECID ? ChineseEnglishSize.DrugMaintenanceitemECID.descripts:'核算项目分类'} {...formItemItem}>
                                {getFieldDecorator('itemECID', {
                                    initialValue: userInfo.itemECID ? userInfo.itemECID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemECID ? ChineseEnglishSize.DrugMaintenanceitemECID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemECID ? ChineseEnglishSize.DrugMaintenanceitemECID.message:'请选择核算项目分类'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemECIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemCode ? ChineseEnglishSize.DrugMaintenanceitemCode.descripts:'收费项代码'} {...formItemItem}>
                                {getFieldDecorator('itemCode', {
                                    initialValue: userInfo.itemCode ? userInfo.itemCode : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemCode ? ChineseEnglishSize.DrugMaintenanceitemCode.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemCode ? ChineseEnglishSize.DrugMaintenanceitemCode.message:'请输入收费项代码'}]
                                })(
                                    <Input style={{width: '100%'}} />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemICID ? ChineseEnglishSize.DrugMaintenanceitemICID.descripts:'住院收据费用'} {...formItemItem}>
                                {getFieldDecorator('itemICID', {
                                    initialValue: userInfo.itemICID ? userInfo.itemICID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemICID ? ChineseEnglishSize.DrugMaintenanceitemICID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemICID ? ChineseEnglishSize.DrugMaintenanceitemICID.message:'请选择住院收据费用'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemICIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemACID ? ChineseEnglishSize.DrugMaintenanceitemACID.descripts:'会计科目'} {...formItemItem}>
                                {getFieldDecorator('itemACID', {
                                    initialValue: userInfo.itemACID ? userInfo.itemACID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemACID ? ChineseEnglishSize.DrugMaintenanceitemACID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemACID ? ChineseEnglishSize.DrugMaintenanceitemACID.message:'请选择会计科目'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemACIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemDesc ? ChineseEnglishSize.DrugMaintenanceitemDesc.descripts:'收费项描述'} {...formItemItem}>
                                {getFieldDecorator('itemDesc', {
                                    initialValue: userInfo.itemDesc ? userInfo.itemDesc: undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemDesc ? ChineseEnglishSize.DrugMaintenanceitemDesc.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemDesc ? ChineseEnglishSize.DrugMaintenanceitemDesc.message:'请输入收费项描述'}]
                                })(
                                    <Input style={{width: '100%'}} />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemOCID ? ChineseEnglishSize.DrugMaintenanceitemOCID.descripts:'门诊收据费用'} {...formItemItem}>
                                {getFieldDecorator('itemOCID', {
                                    initialValue: userInfo.itemOCID ? userInfo.itemOCID: undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemOCID ? ChineseEnglishSize.DrugMaintenanceitemOCID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemOCID ? ChineseEnglishSize.DrugMaintenanceitemOCID.message:'请选择门诊收据费用'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemOCIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemMCID ? ChineseEnglishSize.DrugMaintenanceitemMCID.descripts:'病案分类'} {...formItemItem}>
                                {getFieldDecorator('itemMCID', {
                                    initialValue: userInfo.itemMCID ? userInfo.itemMCID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemMCID ? ChineseEnglishSize.DrugMaintenanceitemMCID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemMCID ? ChineseEnglishSize.DrugMaintenanceitemMCID.message:'请输入病案分类'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemMCIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenanceitemTCID ? ChineseEnglishSize.DrugMaintenanceitemTCID.descripts:'收费分类'} {...formItemItem}>
                                {getFieldDecorator('itemTCID', {
                                    initialValue: userInfo.itemTCID ? userInfo.itemTCID : undefined,
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenanceitemTCID ? ChineseEnglishSize.DrugMaintenanceitemTCID.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenanceitemTCID ? ChineseEnglishSize.DrugMaintenanceitemTCID.message:'请输入收费分类'}]
                                })(
                                    <Select style={{width: '100%'}} showSearch optionFilterProp="seatchprop" allowClear>
                                        {itemTCIDOption}
                                    </Select>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.descripts:'生效日期'} {...formItemItem}>
                                {getFieldDecorator('itemStDate', {
                                    initialValue: userInfo.itemStDate ? Moment(userInfo.itemStDate) : Moment(),
                                    rules:[{ required : ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.required =='Y'? true:false: false, message: ChineseEnglishSize.DrugMaintenancestartDate ? ChineseEnglishSize.DrugMaintenancestartDate.message:'请选择生效日期'}]
                                })(
                                    <DatePicker
                                        format="YYYY-MM-DD" 
                                        style={{width:'100%'}}
                                    >
                                    </DatePicker>
                                )}
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem label={ChineseEnglishSize.DrugMaintenancestopDate ? ChineseEnglishSize.DrugMaintenancestopDate.descripts:'失效日期'} {...formItemItem}>
                                {getFieldDecorator('itemEndDate', {
                                    initialValue: userInfo.itemEndDate ? Moment(userInfo.itemEndDate) : undefined,
                                })(
                                    <DatePicker
                                        format="YYYY-MM-DD" 
                                        style={{width:'100%'}}
                                    >
                                    </DatePicker>
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </Card>
        )
    }
};
let AIITollItemForm = Form.create()(AIITollItem);
export default AIITollItemForm;