/**
 * 未配送
 */

import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Modal, Tabs,
    DatePicker, Button, Switch, Icon, Divider, message, Popconfirm, Tooltip,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ReadCard from 'components/readcard/ReadCard';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import CommonPagination from 'components/pagination/index';
import RangeDatePicker from 'components/rangePicker/index';
import { PAGE_NUM_MORE_STATUS } from 'tools/statusConstants';
import SendExpress from '../component/SendExpress';
import ShowPrescriptionModal from './ShowPrescriptionModal';
import '../courierStyle/index.less';
import moment from 'moment';
import store from 'store';

const { RangePicker } = DatePicker;
const Search = Input.Search;
const Option = Select.Option;
const { TabPane } = Tabs;

class ShowNoDistributionTabPane extends React.Component {

    constructor(props) {
        super(props);
        var date = new Date()
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            size: 'small',
            userData: userData,
            activeKey: '1',
            presc: [],
            cmpresc: [],
            specNos: [],
            checkNos: [],
            curStatus: false
        }
    }

    componentWillReceiveProps(nextProp) {
        if (!Util.isEmpty(nextProp.distributionData)) {
            this.setState({
                distributionData: nextProp.distributionData,
                record: nextProp.record,
            })
        }else{
            this.setState({
                distributionData: [],
                record: '',
            })
        }
    }

    componentDidMount() {
    }

    loadExpressMessage() {
        var { distributionData } = this.state;
        let distributionDataOption = []
        distributionData && distributionData.map((item, index) => {
            distributionDataOption.push(
                <Row><Card key={index} title={<div>
                    <Checkbox onChange={this.expressCheckBox.bind(this, item, index)} checked={item.checked} />
                    <span style={{ marginLeft: 5 }}>{item.admDate}</span>
                    <span>{item.admTime}</span>&nbsp;&nbsp;&nbsp;
                    <span>{item.locDesc}</span>
                </div>} extra={<span>共未配送处方：{item.data.length}</span>}>
                    {this.loadSunData(item.data, item.admID, index)}
                </Card></Row>
            )
        })
        return distributionDataOption
    }

    expressCheckBox(data, index, e) {
        var { presc, distributionData, cmpresc, specNos, checkNos } = this.state;
        var newPrescName = [], newCMPrescName = [], newCheckNoName = [], newSpecNoName = []
        this.state.distributionData[index]['checked'] = e.target.checked
        var source = data.data;
        if (e.target.checked) {
            //选中，先判断是否存在子数据，在进行选中处理
            if (!Util.isEmpty(source)) {
                for (var i = 0; i < source.length; i++) {
                    if (source[i].flag == 'Y') {
                        source[i]['checked'] = e.target.checked
                    }
                    if (source[i].checked == true && source[i].type == 'Presc') {
                        newPrescName.push(source[i].prescNo)
                    } else if (source[i].checked == true && source[i].type == 'CMPresc') {
                        newCMPrescName.push(source[i].prescNo)
                    } else if (source[i].checked == true && source[i].type == 'CheckNo') {
                        newCheckNoName.push(source[i].prescNo)
                    } else if (source[i].checked == true && source[i].type == 'SpecNo') {
                        newSpecNoName.push(source[i].prescNo)
                    }
                    for (var j = 0; j < newPrescName.length; j++) {
                        if (newPrescName[j] == source[i].prescNo) {
                            this.state.presc.push(newPrescName[j])
                        }
                    }
                    for (var j = 0; j < newCMPrescName.length; j++) {
                        if (newCMPrescName[j] == source[i].prescNo) {
                            this.state.cmpresc.push(newCMPrescName[j])
                        }
                    }
                    for (var j = 0; j < newCheckNoName.length; j++) {
                        if (newCheckNoName[j] == source[i].prescNo) {
                            this.state.checkNos.push(newCheckNoName[j])
                        }
                    }
                    for (var j = 0; j < newSpecNoName.length; j++) {
                        if (newSpecNoName[j] == source[i].prescNo) {
                            this.state.specNos.push(newSpecNoName[j])
                        }
                    }
                }
            }
        } else {
            var prescName = [], cmprescName = [], checkNosName = [], specNosName = []
            if (!Util.isEmpty(source)) {
                for (var i = 0; i < source.length; i++) {
                    if (source[i].flag == 'Y') {
                        source[i]['checked'] = e.target.checked
                    }
                    for (var j = 0; j < presc.length; j++) {
                        if (source[i].type == 'Presc' && source[i].prescNo !== presc[j] && source[i].checked == false) {
                            prescName.push(presc[j])
                        }
                    }
                    for (var j = 0; j < cmpresc.length; j++) {
                        if (source[i].type == 'CMPresc' && source[i].prescNo !== cmpresc[j] && source[i].checked == false) {
                            cmprescName.push(cmpresc[j])
                        }
                    }
                    for (var j = 0; j < specNos.length; j++) {
                        if (source[i].type == 'SpecNo' && source[i].prescNo !== specNos[j] && source[i].checked == false) {
                            specNosName.push(specNos[j])
                        }
                    }
                    for (var j = 0; j < checkNos.length; j++) {
                        if (source[i].type == 'CheckNo' && source[i].prescNo !== checkNos[j] && source[i].checked == false) {
                            checkNosName.push(checkNos[j])
                        }
                    }
                }
                this.setState({
                    presc: prescName,
                    cmpresc: cmprescName,
                    checkNos: checkNosName,
                    specNos: specNosName
                })
            }
        }
        let newPresc = [...new Set(this.state.presc)]
        let newCmpresc = [...new Set(this.state.cmpresc)]
        let newSpecNosc = [...new Set(this.state.specNos)]
        let newCheckNos = [...new Set(this.state.checkNos)]
        this.setState({ newPresc, newCmpresc, newSpecNosc, newCheckNos, admID: data.admID });
    }

    loadSunData(data, admID, code) {
        var sunDataOption = []
        data && data.map((item, index) => {
            sunDataOption.push(<Col span={5} style={{ height: '160px', border: '1px solid #e8e8e8', padding: '0 5px', margin: '3px 5px' }}
                key={index}>
                <div>
                    <div style={{ height: 30, lineHeight: '30px', borderBottom: '1px solid #e8e8e8' }}>
                        <div style={{ float: 'left' }}>{/**disabled={item.flag == 'Y' ? false : true} */}
                            <Checkbox onChange={this.onChangeBox.bind(this, data, item, admID, code, index)} checked={item.checked}
                                disabled={item.flag == 'Y' ? false : true} />
                            <span style={{ marginLeft: 5 }}>{item.title}</span>&nbsp;&nbsp;
                            <span>{item.prescNo}</span>
                        </div>

                        <div style={{ float: 'right' }}>{item.status}</div>
                    </div>

                    <div style={{ padding: 5, height: '105px', overflow: 'auto' }}>{this.loadSunBody(item.data)}</div>
                    <div style={{ textAlign: 'center', color: '#1890ff', cursor: 'pointer' }}
                        onClick={this.showPrescriptionModal.bind(this, item)}>处方详情</div>
                </div>
            </Col>)
        })
        return sunDataOption
    }

    loadSunBody(data) {
        var sunBodyOption = []
        data && data.map((item, index) => {
            sunBodyOption.push(<div key={index} style={{
                height: 30, lineHeight: '30px',
                width: '100%', display: 'flex'
            }}>
                <div style={{ width: '80%' }} className='sunbody-esplise'>{item.ordDesc}</div>
                <div style={{ width: '19.8%',textAlign:'right' }}>{item.ordQty}</div>
            </div>)
        })
        return sunBodyOption
    }

    onChangeBox(data, source, admID, code, index, e) {
        var { presc, distributionData, cmpresc, specNos, checkNos } = this.state;
        var newPrescName = [], newCMPrescName = [], newCheckNoName = [], newSpecNoName = []
        data[index]['checked'] = e.target.checked

        if (e.target.checked) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].checked == true && data[i].type == 'Presc') {
                    newPrescName.push(data[i].prescNo)
                } else if (data[i].checked == true && data[i].type == 'CMPresc') {
                    newCMPrescName.push(data[i].prescNo)
                } else if (data[i].checked == true && data[i].type == 'CheckNo') {
                    newCheckNoName.push(data[i].prescNo)
                } else if (data[i].checked == true && data[i].type == 'SpecNo') {
                    newSpecNoName.push(data[i].prescNo)
                }
            }
            for (var i = 0; i < newPrescName.length; i++) {
                if (newPrescName[i] == source.prescNo) {
                    this.state.presc.push(newPrescName[i])
                }
            }
            for (var i = 0; i < newCMPrescName.length; i++) {
                if (newCMPrescName[i] == source.prescNo) {
                    this.state.cmpresc.push(newCMPrescName[i])
                }
            }
            for (var i = 0; i < newCheckNoName.length; i++) {
                if (newCheckNoName[i] == source.prescNo) {
                    this.state.checkNos.push(newCheckNoName[i])
                }
            }
            for (var i = 0; i < newSpecNoName.length; i++) {
                if (newSpecNoName[i] == source.prescNo) {
                    this.state.specNos.push(newSpecNoName[i])
                }
            }
        } else {
            var prescName = [], cmprescName = [], checkNosName = [], specNosName = []
            // for (var i = 0; i < data.length; i++) {
            for (var j = 0; j < presc.length; j++) {
                if (source.type == 'Presc' && source.prescNo !== presc[j] && source.checked == false) {
                    prescName.push(presc[j])
                }
            }
            for (var j = 0; j < cmpresc.length; j++) {
                if (source.type == 'CMPresc' && source.prescNo !== cmpresc[j] && source.checked == false) {
                    cmprescName.push(cmpresc[j])
                }
            }
            for (var j = 0; j < specNos.length; j++) {
                if (source.type == 'SpecNo' && source.prescNo !== specNos[j] && source.checked == false) {
                    specNosName.push(specNos[j])
                }
            }
            for (var j = 0; j < checkNos.length; j++) {
                if (source.type == 'CheckNo' && source.prescNo !== checkNos[j] && source.checked == false) {
                    checkNosName.push(checkNos[j])
                }
            }
            // }
            this.setState({
                presc: prescName,
                cmpresc: cmprescName,
                checkNos: checkNosName,
                specNos: specNosName
            })

        }

        let newPresc = [...new Set(this.state.presc)]
        let newCmpresc = [...new Set(this.state.cmpresc)]
        let newSpecNosc = [...new Set(this.state.specNos)]
        let newCheckNos = [...new Set(this.state.checkNos)]
        this.setState({ newPresc, newCmpresc, newSpecNosc, newCheckNos, admID: admID });
    }

    //全选
    changeSelectAll() {
        var { presc, distributionData, cmpresc, specNos, checkNos, curStatus } = this.state;
        var newPrescName = [], newCMPrescName = [], newCheckNoName = [], newSpecNoName = []
        if (curStatus == false) {
            this.state.curStatus = true;
            if (!Util.isEmpty(distributionData)) {
                for (var i = 0; i < distributionData.length; i++) {
                    distributionData[i]['checked'] = true
                    this.state.admID = distributionData[i].admID
                    var source = distributionData[i].data
                    for (var j = 0; j < source.length; j++) {
                        if (source[j].flag == 'Y') {
                            source[j]['checked'] = true
                        }
                        if (source[j].checked == true && source[j].type == 'Presc') {
                            newPrescName.push(source[j].prescNo)
                        } else if (source[j].checked == true && source[j].type == 'CMPresc') {
                            newCMPrescName.push(source[j].prescNo)
                        } else if (source[j].checked == true && source[j].type == 'CheckNo') {
                            newCheckNoName.push(source[j].prescNo)
                        } else if (source[j].checked == true && source[j].type == 'SpecNo') {
                            newSpecNoName.push(source[j].prescNo)
                        }
                    }
                }
            }
            // this.setState({
            //     presc: newPrescName,
            //     cmpresc: newCMPrescName,
            //     checkNos: newCheckNoName,
            //     specNos: newSpecNoName
            // })
            let newPresc = [...new Set(newPrescName)]
            let newCmpresc = [...new Set(newCMPrescName)]
            let newSpecNosc = [...new Set(newSpecNoName)]
            let newCheckNos = [...new Set(newCheckNoName)]
            this.setState({ newPresc, newCmpresc, newSpecNosc, newCheckNos });
        } else {
            var prescName = [], cmprescName = [], checkNosName = [], specNosName = []
            this.state.curStatus = false
            if (!Util.isEmpty(distributionData)) {
                for (var i = 0; i < distributionData.length; i++) {
                    this.state.admID = undefined
                    distributionData[i]['checked'] = false
                    var source = distributionData[i].data
                    for (var j = 0; j < source.length; j++) {
                        if (source[j].flag == 'Y') {
                            source[j]['checked'] = false
                        }
                        if (source[j].checked == false && source[j].type == 'Presc') {
                            prescName.push([])
                        } else if (source[j].checked == false && source[j].type == 'CMPresc') {
                            cmprescName.push([])
                        } else if (source[j].checked == false && source[j].type == 'CheckNo') {
                            checkNosName.push()
                        } else if (source[j].checked == false && source[j].type == 'SpecNo') {
                            specNosName.push([])
                        }
                    }
                }
            }
            // this.setState({
            //     presc: prescName,
            //     cmpresc: cmprescName,
            //     checkNos: checkNosName,
            //     specNos: specNosName
            // })
            let newPresc = [...new Set(prescName)]
            let newCmpresc = [...new Set(cmprescName)]
            let newSpecNosc = [...new Set(specNosName)]
            let newCheckNos = [...new Set(checkNosName)]
            this.setState({ newPresc, newCmpresc, newSpecNosc, newCheckNos });
        }
    }

    //寄快递
    handleSendExpress = () => {
        // console.dir(this.state.newCmpresc)
        // console.dir(this.state.newSpecNosc)
        // console.dir(this.state.newCheckNos)
        // console.dir(this.state.newPresc)
        this.expressRef.showModal()
    }

    onRef = (ref) => {
        this.expressRef = ref
    }

    handleTableInfo = () => {
        this.clearInput()
        var { handleTableInfo } = this.props;
        handleTableInfo && handleTableInfo()
    }

    clearInput() {
        this.setState({
            presc: [],
            cmpresc: [],
            specNos: [],
            checkNos: [],
            newCmpresc: [],
            newSpecNosc: [],
            newCheckNos: [],
            newPresc: [],
            distributionData: []
        })
    }

    showPrescriptionModal(data) {
        var params = {
            params: [{
                admID: data.admID,
                prescNo: data.prescNo,
                type: data.type,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03030049",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        prescriptData: res.result,
                        isShowPrescriptionModal: true
                    })
                }
            }
        });
    }

    hideModal(name) {
        this.setState({
            [name]: false
        })
    }

    render() {
        var { distributionData } = this.state;

        return <div style={{ height: '76vh' }} className='no-disbtn-body'>
            <SendExpress
                newPresc={this.state.newPresc}
                admID={this.state.admID}
                newCmpresc={this.state.newCmpresc}
                newSpecNosc={this.state.newSpecNosc}
                newCheckNos={this.state.newCheckNos}
                titleData={this.state.record}
                handleTableInfo={this.handleTableInfo}
                onRef={this.onRef}
            >
            </SendExpress>
            {/**处方详情 */}
            <ShowPrescriptionModal isShow={this.state.isShowPrescriptionModal}
                prescriptData={this.state.prescriptData}
                onHide={this.hideModal.bind(this, 'isShowPrescriptionModal')}>
            </ShowPrescriptionModal>
            <div style={{ height: '80vh', overflow: 'auto' }}>
                <div style={{ height: 40, lineHeight: '40px', borderBottom: '1px solid #e8e8e8' }}>
                    <div style={{ float: 'left' }}>共计未配送处方：{this.state.distributionData && this.state.distributionData.length}</div>
                    <div style={{ float: 'right' }}>
                        <Button onClick={this.changeSelectAll.bind(this)}>全选/反选</Button>
                        <Button type='primary' style={{ marginLeft: 5 }} onClick={this.handleSendExpress}
                            disabled={!Util.isEmpty(this.state.admID) ? false : true}>物流配送</Button>
                    </div>
                </div>
                {this.loadExpressMessage()}
            </div>
        </div>
    }
}

export default Form.create()(ShowNoDistributionTabPane);