//处方点评-处方预览
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Tabs, Table, Input, message, Pagination  } from 'antd';
import { $http } from 'containers/config/https';
import store from 'store';
import CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
const { Option } = Select;
const FormItem = Form.Item;
const Preview = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                detailsId: '',//统计单ID
                TableDataEditField:[], //统计单明细数据
                checkData: {},
                pageSize2: 20,
                page2: 1,
                detailTotal: 0,
                modalColumns:[],
                modalColumnsWidth:'', //表格宽度
                stockID: '', //行id
                SelectData: [], //下拉数据
            };
            // 统计单详细表头
            this.modalColumns = [

                {
                    key: 1,
                    title: '代码 ',
                    dataIndex: 'stockCode',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 2,
                    title: '名称',
                    dataIndex: 'stockDesc',
                    align: "center",
                    width: '10%'
                },
                {
                    key: 3,
                    title: '货位',
                    dataIndex: 'unitDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 4,
                    title: '批号',
                    dataIndex: 'batNo',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 5,
                    title: '有效期',
                    dataIndex: 'expDate',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 6,
                    title: '库存(包装单位)',
                    dataIndex: 'purQty',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 7,
                    title: '包装单位',
                    dataIndex: 'purUnitDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 8,
                    title: '库存(基本单位)',
                    dataIndex: 'baseQty',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 9,
                    title: '基本单位',
                    dataIndex: 'baseUnitDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 10,
                    title: '零售价',
                    dataIndex: 'sp',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 11,
                    title: '最新进价',
                    dataIndex: 'rp',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 12,
                    title: '售价金额',
                    dataIndex: 'spAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 13,
                    title: '进价金额',
                    dataIndex: 'rpAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 14,
                    title: '规格',
                    dataIndex: 'stockSpec',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 15,
                    title: '生产厂家',
                    dataIndex: 'manfDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 16,
                    title: '批次供应商',
                    dataIndex: 'vendorDesc',
                    align: "center",
                    width: '6%'
                }
            ]
        }

        componentDidMount(){
            this.props.onRef(this);
            // this.getDetailTableColumns();
            let leftList = document.querySelectorAll('.previewHeight  .ant-table-body');
            if(leftList.length>0){
                leftList[0].style.height = (store.getState().tableHeight.y)/2 + 'px';
            }
            let LogHeight = document.querySelectorAll('.BottomHeight  .ant-table-body');
            if(LogHeight.length>0){
                LogHeight[0].style.height = (store.getState().tableHeight.y)/2 + 'px';
            }
        }

        // 获取明细列表头数据
        getDetailTableColumns = () => {
            let {userInfo} = this.state;
            let data = {
                params: [{
                    compontName: 'TableDataFormList',
                    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, modalColumnsWidth: res.totalWidth})
                    }
                }
            });
        };

        componentWillReceiveProps(props){
            if(props.TableDataEditField){
                this.setState({
                    TableDataEditField: props.TableDataEditField.rows,
                    detailTotal: props.TableDataEditField.total
                })
            }

        }
        componentWillUnmount() {
            this.setState({  page: 1,});
            // 组件销毁前将静止让setState修改state的状态，防止内存泄漏
            this.setState = (state,callback)=>{ return; }
        }
        //清空表格内容
        handleReset = () => {
            this.setState({stockID: ''})  ;
        };
        // 页码改变的回调，参数是改变后的页码及每页条数 统计单详情
        onChange2 = (page, pageSize) => {
            this.setState({page2: page, pageSize2: this.state.pageSize2}, () => {
                // 重新调用查询方法
                // this.props.handelThis.callbackCheck(2,this.state.page2, this.state.pageSize2);
            });
        };

        onShowSizeChange2 = (current, size) => {
            this.setState({page2: current, pageSize2: size}, () => {
                // 重新调用查询方法
                this.onChange2(current,size);
            })
        };
        //组件权限编辑器
        onColumnRef = (ref) => {
            this.columnRef = ref;
        };
        // 表格行的类名
        setRowClassName = (record) => {
            return record.stockCode === this.state.stockID ? 'clickRowStyle' : '';
        };
        render() {
            const { getFieldDecorator } = this.props.form;
            let {modalColumns, modalColumnsWidth} = this.state;
            const formItemLayout = {    // label 和input宽度
                labelCol: { span:7 },
                wrapperCol: { span: 17 },
            };
            return (
                <div className="Preview">
                    <Row >
                        <Col span={3}>
                            <FormItem {...formItemLayout} label='处方号'>
                                {getFieldDecorator('No',{
                                    initialValue: undefined,
                                })(
                                    <Input/>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={3}>
                            <FormItem {...formItemLayout} label='登记号'>
                                {getFieldDecorator('loginNo',{
                                    initialValue: undefined,
                                })(
                                    <Input/>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={2}>
                            <FormItem {...formItemLayout} label='姓名'>
                                {getFieldDecorator('name',{
                                    initialValue: undefined,
                                })(
                                    <Input/>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={2}>
                            <FormItem {...formItemLayout} label='性别'>
                                {getFieldDecorator('gender',{
                                    initialValue: undefined,
                                })(
                                    <Select allowClear>

                                    </Select>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={2}>
                            <FormItem {...formItemLayout} label='年龄'>
                                {getFieldDecorator('age',{
                                    initialValue: undefined,
                                })(
                                    <Input/>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={2}>
                            <FormItem {...formItemLayout} label='费别'>
                                {getFieldDecorator('provider',{
                                    initialValue: undefined,
                                })(
                                    <Select allowClear>

                                    </Select>
                                ) }
                            </FormItem>
                        </Col>
                        <Col span={3}>
                            <FormItem {...formItemLayout} label='科室'>
                                {getFieldDecorator('provider',{
                                    initialValue: undefined,
                                    rules:[{ required :false, message: '科室不能为空'}]
                                })(
                                    <Select allowClear>
                                        {/*{this.state.SelectData.vendor && this.state.SelectData.vendor.map((item, i) => {*/}
                                        {/*return <Option value={item.id} key={i}>{item.descripts}</Option>*/}
                                        {/*})}*/}
                                    </Select>
                                ) }
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Card
                            size="small"
                            title={<div className="card-title-left-icon"><div></div>处方信息</div>}
                        >
                            <Col>
                                <Row>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='姓名'>
                                            {getFieldDecorator('UserName',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={2}>
                                        <FormItem {...formItemLayout} label='性别'>
                                            {getFieldDecorator('log',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={2}>
                                        <FormItem {...formItemLayout} label='年龄'>
                                            {getFieldDecorator('age',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={2}>
                                        <FormItem {...formItemLayout} label='身高'>
                                            {getFieldDecorator('height',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={2}>
                                        <FormItem {...formItemLayout} label='体重'>
                                            {getFieldDecorator('age',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='费别'>
                                            {getFieldDecorator('fer',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='科室'>
                                            {getFieldDecorator('Loc',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={2}>
                                        <FormItem {...formItemLayout} label='类别'>
                                            {getFieldDecorator('stkCat',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                </Row>
                                <Row>
                                    <Col span={10}>
                                        <Table
                                            columns={modalColumns.length>0?modalColumns:this.modalColumns}
                                            dataSource={this.state.TableDataEditField}
                                            pagination={false}
                                            bordered
                                            scroll={{x:modalColumnsWidth? modalColumnsWidth : 1500, y: (store.getState().tableHeight.y)/3 + 'px' }}
                                            rowKey={record => record.key}
                                            className='previewHeight'
                                            rowClassName={this.setRowClassName}
                                            onRow={(record) => {
                                                return {
                                                    // 单击行选中
                                                    onClick: (e) => {
                                                        e.stopPropagation();
                                                        // this.resetFields();
                                                        if (this.state.stockID === '') {
                                                            // 保存行数据以及ID
                                                            this.setState({stockID: record.stockCode});
                                                            this.props.handelThis.setStockID(record.stockCode);
                                                        } else {
                                                            if (this.state.stockCode !== record.stockCode) {
                                                                this.setState({stockID: record.stockCode});
                                                                this.props.handelThis.setStockID(record.stockCode);
                                                            } else {
                                                                this.setState({ stockID: ''});
                                                            }
                                                        }
                                                    }
                                                }
                                            }}
                                        />

                                        {/*<CommonPagination size='small' current={this.state.page2} total={this.state.detailTotal} search= 'search'*/}
                                                          {/*onShowSizeChange={this.onShowSizeChange2.bind(this)} complete={true} dataSource={this.state.TableDataEditField}*/}
                                                          {/*loading={this.state.loading} onChange={this.onChange2.bind(this)}  pageSize={true} />*/}
                                    </Col>
                                    <Col span={14}>
                                        <Table
                                            columns={modalColumns.length>0?modalColumns:this.modalColumns}
                                            dataSource={this.state.TableDataEditField}
                                            pagination={false}
                                            bordered
                                            scroll={{x:modalColumnsWidth? modalColumnsWidth : 1500, y: (store.getState().tableHeight.y)/3 + 'px' }}
                                            rowKey={record => record.key}
                                            className='BottomHeight'

                                        />

                                        {/*<CommonPagination size='small' current={this.state.page2} total={this.state.detailTotal} search= 'search'*/}
                                                          {/*onShowSizeChange={this.onShowSizeChange2.bind(this)} complete={true} dataSource={this.state.TableDataEditField}*/}
                                                          {/*loading={this.state.loading} onChange={this.onChange2.bind(this)}  pageSize={true} />*/}
                                    </Col>

                                </Row>
                                <Row>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='处方总额'>
                                            {getFieldDecorator('total',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='医生'>
                                            {getFieldDecorator('Dname',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='配药药师'>
                                            {getFieldDecorator('name12',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                    <Col span={3}>
                                        <FormItem {...formItemLayout} label='发药药师'>
                                            {getFieldDecorator('name75',{
                                                initialValue: undefined,
                                            })(
                                                <Input/>
                                            ) }
                                        </FormItem>
                                    </Col>
                                </Row>
                            </Col>
                        </Card>
                    </Row>
                    <ColumnAuthoritysModel
                        clientWidth='800px'
                        compontName='TableDataFormList'
                        configType="C"
                        onRef={this.onColumnRef}
                        reloadData={this.getDetailTableColumns}
                    />
                </div>
            )
        }
    }
);
export default Preview;
