// 处方点评查询
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Modal, Table, Switch, message, Pagination, Input, Checkbox, Icon } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import store from 'store';
import '../style/index.less';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import CommonPagination from 'components/pagination/index';

const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 17 },
};
const formItemSwitch = {    // label 和input宽度
    labelCol: { span:12 },
    wrapperCol: { span: 12 },
};
const PrescriptionReviewCheck = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                data: [],
                SelectData: {},
                checkedDataDetails:[], //点评数据
                size: 'small',
                pageSize: 10,
                page: 1,
                total:0,
                show: false,
                modalColumns: [],
                visible: false,
                detailWidthTotal: '', // 明细表头长度
                stockID: '', //主表格id
                rowKey: '', //行key值
                ChineseEnglishSize: {} , //中英翻译
            };
            // 点评单据
            this.modalColumns = [
                {
                    key: 1,
                    title: this.state.userInfo.language == 'EN' ?  'NO':'单号',
                    dataIndex: 'reviewNO',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 2,
                    title: this.state.userInfo.language == 'EN' ?  'createDate':'日期',
                    dataIndex: 'createDate',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 3,
                    title: this.state.userInfo.language == 'EN' ?  'createTime':'时间',
                    dataIndex: 'createTime',
                    align: "center",
                    width: '150px'
                },
                {
                    key: 4,
                    title: this.state.userInfo.language == 'EN' ?  'Prepared by':'制单人',
                    dataIndex: 'createUser',
                    align: "center",
                    width: '150px',
                },
                {
                    key: 5,
                    title: this.state.userInfo.language == 'EN' ?  'type':'类型',
                    dataIndex: 'type',
                    align: "center",
                    width: '150px',
                },
                {
                    key: 6,
                    title: this.state.userInfo.language == 'EN' ?  'way':'方式',
                    dataIndex: 'way',
                    align: "center",
                    width: '150px',
                },
                // {
                //     key: 7,
                //     title: this.state.userInfo.language == 'EN' ?  'Production Date':'查询条件',
                //     dataIndex: 'produceDate',
                //     align: "center",
                //     width: '150px'
                // },
                // {
                //     key: 8,
                //     title: this.state.userInfo.language == 'EN' ?  'Quantity':'点评状态',
                //     dataIndex: 'qty',
                //     align: "center",
                //     width: '150px'
                // }
            ]
        }

        componentDidMount() {
            this.props.onRef(this);
            // 初始化数据
            this.getTacitlyApproveData();
            // this.getDetailTableColumns();
        }

        //清空表格内容
        handleReset = () => {
            this.setState({checkedDataDetails: [], total: 0});
            this.props.form.resetFields();
        };

        // 获取界面默认值
        getTacitlyApproveData() {
            let data = {
                params: [{
                    locID: ''
                }]
            };
            // 获取下拉数据
            $http.post('urlS', this, {
                code: "02020300",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({SelectData: res},()=>{
                            this.getCheckReviewsList();
                        })
                    }
                }
            })
        }

        //维护数据
         getDetailTableColumns = () => {
            let {userInfo} = this.state;
            let data = {
                params: [{
                    compontName: 'DrugsInboundAssistFormDetail',
                    type: 'C',
                }]
            }
            $http.post('urlS', this, {
                code: '01040073',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        let arr = res.result.C&&res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map((item, index) => {
                            return  {
                                ...item,
                                key: index + 1,
                                title: <div onDoubleClick={() => {this.columnRef.onOpenModel()}}>{userInfo.language== 'EN' ? item.enDescripts : item.title}</div>
                            }
                        }) : '';
                        let filterArr = arr.filter(function(item){ return item.display === 'Y'});
                        // this.setState({modalColumns: filterArr, detailWidthTotal: res.totalWidth})
                    }
                }
            });
        };

        // 获取点评单
        getCheckReviewsList=()=>{
            let {ChineseEnglishSize} =this.state;
            let leftList = document.querySelectorAll('.checkHeight  .ant-table-body');
            if(leftList.length>0){
                leftList[0].style.height = store.getState().tableHeight.y + 10 + 'px';
            }
            this.setState({loading:true});
            this.props.form.validateFields((err , val)=> {
                let obj = JSON.parse(JSON.stringify(val));
                if (err == null) {
                    let data = {
                        params: [{
                            startDate: obj.StartDate?Moment(obj.StartDate).format('YYYY-MM-DD'): '',
                            endDate: obj.EndDate?Moment(obj.EndDate).format('YYYY-MM-DD'): '',
                            way:obj.way ? obj.way : '',
                            doctor: obj.doctor ? obj.doctor : '',
                            state: obj.state ? obj.state : '',
                        }],
                        pagination: [
                            {
                                pageSize:this.state.pageSize,
                                currentPage:this.state.page,
                                sort:"ID",
                                dir:"asc",
                            }
                        ],
                    };
                    $http.post('urlS', this, {
                        code: "02020301",
                        data: data,
                        success: function (res) {
                            if(res.data.length > 0){
                                for(var i = 0; i < res.data.length; i++){
                                    res.data[i].key = i;
                                }
                                this.setState({ checkedDataDetails: res.data,total: res.total,loading:false });
                            }else {
                                this.setState({checkedDataDetails: []});
                                message.error(ChineseEnglishSize.WarehousingOrderMakingnoData ? ChineseEnglishSize.WarehousingOrderMakingnoData.descripts:"没数据")
                            }
                        }
                    });
                }
            });
        };

        // 页码改变的回调，参数是改变后的页码及每页条数
        onChange = (e) => {
            this.setState({page: e, pageSize: this.state.pageSize}, () => {
                // 重新调用查询方法
                this.getCheckReviewsList();
            });
        };

        onShowSizeChange = (current, size) => {
            this.setState({page: current, pageSize: size}, () => {
                // 重新调用查询方法
                this.onChange(current,size);
            })
        };


        //组件权限编辑器
        onColumnRef = (ref) => {
            this.columnRef = ref;
        };
        // 关闭
        handleCancel = (e) => {
            this.setState({
                visible: false,
                checkedDataDetails: [],
                total2: 0,
                rowKey: '',
                rowId: '',
                data: [],//主列表数据
                total: 0,
                page: 1,
                pageSize: 10,
            });
        };
        // 确定
        handleOk = (e) => {
            this.handleCancel()
        };
        // 打开弹框
        showModal = (val) => {
            this.setState({
                visible: true,
                ChineseEnglishSize: val,
            });
        };

        // 设置行属性
        onClickRow = (record) => {
            return {
                // 单击行选中
                onClick: (e) => {
                    e.stopPropagation();
                    if (this.state.rowKey === '') {
                        // 保存行数据以及ID
                        this.setState({rowKey: record.key, stockID: record.id},()=>{
                            // this.getRequestForm();
                        });
                    } else {
                        if (this.state.rowKey !== record.key) {
                            this.setState({rowKey: record.key, stockID: record.id},()=>{
                                // this.getRequestForm();
                            });
                        }
                        // else {
                        //     this.setState({rowKey: '', stockID: '', checkedDataDetails: []});
                        // }
                    }
                },
                onDoubleClick: () => {
                    this.props.handleThis.getCheckData(record.id);
                    this.handleCancel();
                }
            }
        };

        // 表格行的类名
        setRowClassName = (record) => {
            return record.key === this.state.rowKey ? 'clickRowStyle' : '';
        };

        //保存
        save=()=>{

            let data = {
                    params:[{
                        inOperID:this.state.stockID ? this.state.stockID : '',
                    }]
            };
            $http.post('urlS', this, {
                code: "02020299",
                data: data,
                success: function (res) {
                    if(res.errorCode == 0){
                        this.props.handleThis.getEchoData(res.result.inOperID);
                        this.handleCancel();
                        message.success(res.errorMessage)
                    }
                }
            });
        }
        render() {
            const { getFieldDecorator } = this.props.form;
            let userInfo = this.state.userInfo ? this.state.userInfo: {};

            //状态
            let stateSelect = [];
            this.state.SelectData.state && this.state.SelectData.state.map((item, i) => {
                stateSelect.push(<Option value={item.id} key={i} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
            });
            //点评方式
            let waysSelect = [];
            this.state.SelectData.ways && this.state.SelectData.ways.map((item, i) => {
                waysSelect.push(<Option value={item.id} key={i} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
            });
            let {ChineseEnglishSize,modalColumns,detailWidthTotal} =this.state;
            return (
                <Modal
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    width='75%'
                    footer={null}
                    className="PrescriptionReviewCheck"
                >
                    <Row>
                        <Card
                            size="small"
                            title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.WarehousingOrderMakingWarehousingForm ? ChineseEnglishSize.WarehousingOrderMakingWarehousingForm.descripts:'查找点评单'}</div>}
                        >
                            <Col >
                                <Form>
                                    <Col span={19}>
                                        <Row >
                                            <Col span={8}>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakingstartDate ? ChineseEnglishSize.WarehousingOrderMakingstartDate.descripts:'起初日期'}>
                                                    {getFieldDecorator('StartDate',{
                                                        initialValue:  Moment(Moment().format('L'), 'YYYY-MM-DD'),
                                                        rules:[{required: true, message: '起初日期不能为空'}]
                                                    })(
                                                        <DatePicker style={{width: '100%'}} />
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col span={8}>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakingendDate ? ChineseEnglishSize.WarehousingOrderMakingendDate.descripts:'截至日期'}>
                                                    {getFieldDecorator('EndDate',{
                                                        initialValue:  Moment(Moment().format('L'), 'YYYY-MM-DD'),
                                                        rules:[{required: true, message: '截至日期不能为空'}]
                                                    })(
                                                        <DatePicker style={{width: '100%'}} />
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            {/*<Col span={8}>*/}
                                                {/*<FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakinginOperNo ? ChineseEnglishSize.WarehousingOrderMakinginOperNo.descripts:'结果'}>*/}
                                                    {/*{getFieldDecorator('inOperNo',{*/}
                                                        {/*initialValue: undefined,*/}
                                                        {/*rules:[{ required : false, message: '结果不能为空'}]*/}
                                                    {/*})(*/}
                                                        {/*<Select showSearch allowClear>*/}
                                                            {/*{this.state.SelectData.vendor && this.state.SelectData.vendor.map(item => {*/}
                                                                {/*return <Option key={item.id} value={item.id} >{item.descripts}</Option>*/}
                                                            {/*})}*/}
                                                        {/*</Select>*/}
                                                    {/*) }*/}
                                                {/*</FormItem>*/}
                                            {/*</Col>*/}
                                            {/*<Col span={8}>*/}
                                                {/*<FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakingWarehouseStatus ? ChineseEnglishSize.WarehousingOrderMakingWarehouseStatus.descripts:'点评状态'}>*/}
                                                    {/*{getFieldDecorator('state',{*/}
                                                        {/*initialValue: undefined,*/}
                                                        {/*rules:[{ required : false, message: '点评状态不能为空'}]*/}
                                                    {/*})(*/}
                                                        {/*<Select allowClear>*/}
                                                            {/*{stateSelect}*/}
                                                        {/*</Select>*/}
                                                    {/*) }*/}
                                                {/*</FormItem>*/}
                                            {/*</Col>*/}
                                        </Row>
                                        <Row>
                                            <Col span={8}>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakingvendorID ? ChineseEnglishSize.WarehousingOrderMakingvendorID.descripts:'点评方式'}>
                                                    {getFieldDecorator('way',{
                                                        initialValue:  undefined,
                                                        rules:[{ required : false, message: '点评方式不能为空'}]
                                                    })(
                                                        <Select showSearch allowClear placeholder="请选择点评方式" >
                                                            {waysSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col span={8}>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WarehousingOrderMakingWarehouseStatus ? ChineseEnglishSize.WarehousingOrderMakingWarehouseStatus.descripts:'点评药师'}>
                                                    {getFieldDecorator('doctor',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '点评药师不能为空'}]
                                                    })(
                                                        <Select allowClear showSearch optionFilterProp="seatchprop">
                                                            {this.state.SelectData.doctor && this.state.SelectData.doctor.doctor && this.state.SelectData.doctor.doctor.map((item, i) => {
                                                                return <Option value={item.id} key={i}  seatchprop={item.descripts + item.descriptsSPCode}>{item.descripts}</Option>
                                                            })}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                        </Row>
                                    </Col>
                                    <Col span={5}>
                                        <Row style={{marginTop: '4px',marginLeft: '35px'}} className="buttonLeft">
                                            <Button onClick={this.getCheckReviewsList}>{ChineseEnglishSize.WarehousingOrderMakingsearch ? ChineseEnglishSize.WarehousingOrderMakingsearch.descripts:'查询'}</Button>
                                            <Button style={{marginLeft: '44px'}} onClick={this.save}>{ChineseEnglishSize.WarehousingOrderMakingsave ? ChineseEnglishSize.WarehousingOrderMakingsave.descripts:'确认'}</Button>
                                        </Row>
                                        <Row style={{marginTop: '18px',marginLeft: '35px'}} className="buttonLeft">
                                            <Button  onClick={this.handleReset}>{ChineseEnglishSize.WarehousingOrderMakingReset ? ChineseEnglishSize.WarehousingOrderMakingReset.descripts:'删除'}</Button>
                                            <Button style={{marginLeft: '44px'}} onClick={this.handleReset}>{ChineseEnglishSize.WarehousingOrderMakingReset ? ChineseEnglishSize.WarehousingOrderMakingReset.descripts:'提交'}</Button>
                                        </Row>

                                    </Col>
                                </Form>
                            </Col>
                        </Card>
                    </Row>
                    <Row>
                        <Col>
                            <Card
                                size="small"
                                title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.WarehousingOrderMakingstartDated ? ChineseEnglishSize.WarehousingOrderMakingstartDated.descripts:'点评单据'}</div>}
                                extra={<span style={{color: '#666'}} className='span' onClick={() => {
                                    this.columnRef.onOpenModel()}}><Icon type="setting" title={ChineseEnglishSize.WarehousingOrderMakingcolumnSettings ? ChineseEnglishSize.WarehousingOrderMakingcolumnSettings.descripts:'列权限设置'} /></span>}
                            >
                                <div style={{height: store.getState().tableHeight.y -200 + 'px'}}>
                                    <Table
                                        columns={modalColumns.length>0?modalColumns:this.modalColumns}
                                        dataSource={this.state.checkedDataDetails}
                                        pagination={false}
                                        bordered
                                        style={{height: (store.getState().tableHeight.y - 238)}}
                                        scroll={{x:detailWidthTotal?detailWidthTotal:1200, y: store.getState().tableHeight.y - 295}}
                                        onRow={this.onClickRow}
                                        rowClassName={this.setRowClassName}
                                        rowKey={record => record.key}
                                        className='checkHeight'
                                    />
                                    <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total} search= 'search'
                                                      onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.checkedDataDetails}
                                                      loading={this.state.detailsLoading} onChange={this.onChange.bind(this)} />
                                </div>
                            </Card>
                        </Col>
                    </Row>
                    <ColumnAuthoritysModel
                        clientWidth='800px'
                        compontName='DrugsInboundAssistFormDetail'
                        configType="C"
                        onRef={this.onColumnRef}
                        reloadData={this.getDetailTableColumns}
                    />
                </Modal>
            )
        }
    }
);
export default PrescriptionReviewCheck;
