// 全院库存查询
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Checkbox, Table, Switch, message, Pagination, Input  } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import store from 'store';
import Reports from '../raqSoft/reportsEmbed';
import './style/index.less';
import PublicModalTable from './../common/PublicModalTable';
var serialNumber = 0; //库存单序列号

const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 14 },
};
const formItemSpan = {    // label 和input宽度
    labelCol: { span:5 },
    wrapperCol: { span: 19 },
};

const AllCourtyardInventory = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                data: [],
                queryCondition: {},
                SelectData: {},
                inOpLocDr:'',
                vendor: '',
                WaListData: [], //库存单数据
                pageSize: 10,
                page: 1,
                total:0,
                stateID: '',
                ModalRowData: {}, // 弹框双击选中行数据
                stockID: '',
                modalColumns:[],
                modalColumnsWidth:'',
                ChineseEnglishSize:{},
                params: {},
                raqName:''
            };
            // 库存单的数据表头
            this.columns = [

                {
                    key: 2,
                    title: '代码',
                    dataIndex: 'stockCode',
                    align: "center",
                    width: '9%'
                },
                {
                    key: 3,
                    title: '名称',
                    dataIndex: 'stockDesc',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 4,
                    title: '规格',
                    dataIndex: 'stockSpec',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 1,
                    title: '单位',
                    dataIndex: 'unitDesc',
                    align: "center",
                    width: '7%',
                },
                {
                    key: 5,
                    title: '库存量',
                    dataIndex: 'qty',
                    align: "center",
                    width: '9%'
                },
                {
                    key: 6,
                    title: '出数量',
                    dataIndex: 'outQty',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 7,
                    title: '入数量',
                    dataIndex: 'inQty',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 8,
                    title: '出金额',
                    dataIndex: 'outSpAmt',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 9,
                    title: '出进价金额',
                    dataIndex: 'outRpAmt',
                    align: "center",
                    width: '9%'
                },
                {
                    key: 10,
                    title: '入金额',
                    dataIndex: 'inSpAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 11,
                    title: '入进价金额',
                    dataIndex: 'inRpAmt',
                    align: "center",
                    width: '8%'
                },
                {
                    key: 12,
                    title: '生产厂家',
                    dataIndex: 'manfDesc',
                    align: "center",
                    width: '9%'
                },
                {
                    key: 13,
                    title: '货位',
                    dataIndex: 'expDate',
                    align: "center",
                    width: '6%'
                }
            ];
            this.modalColumns = [
                {
                    key: 1,
                    title: '库存项代码',
                    dataIndex: 'stockCode',
                    align: "center",
                    width: '22%'
                },
                {
                    key: 2,
                    title: '库存项描述',
                    dataIndex: 'stockDesc',
                    align: "center",
                    width: '24%',
                },
                {
                    key: 5,
                    title: '库存分类',
                    dataIndex: 'stkSubCatDesc',
                    align: "center",
                    width: '16%',
                },
                {
                    key: 3,
                    title: '包装规格',
                    dataIndex: 'stockSpec',
                    align: "center",
                    width: '12%',
                },
                {
                    key: 6,
                    title: '基本单位',
                    dataIndex: 'baseUnitDesc',
                    align: "center",
                    width: '12%',
                },
                {
                    key: 7,
                    title: '入库单位',
                    dataIndex: 'purUnitDesc',
                    align: "center",
                }
            ]
        }

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

        // 获取明系列表头数据
        // getDetailTableColumns = () => {
        //     let {userInfo} = this.state;
        //     let data = {
        //         params: [{
        //             reactCode: ['WholeHospitalInventory'],
        //             compontName: 'WholeHospitalInventory',
        //             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>{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,ChineseEnglishSize:userInfo.language=='EN' ? res.result.language.EN : res.result.language.CN})
        //             }
        //         }
        //     });
        // };

        componentWillUnmount() {
            // 组件销毁前将静止让setState修改state的状态，防止内存泄漏
            this.setState = (state,callback)=>{ return; }
        }

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

        // 获取界面默认值
        getTacitlyApproveData() {
            let data = {
                params: []
            };
            // 获取下拉数据
            $http.post('urlS', this, {
                code: "02020339",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({SelectData: res,raqName:res.raqName})
                    }
                }
            })
        }
        // 根据表单获取列表数据
        searchInformation = () => {
            this.props.form.validateFields((err, values) => {
                if (err) {
                    return;
                }
                const params = {
                    // locID: values.locID ? values.locID : undefined,
                    startDate: values.startDate ? Moment(values.startDate).format("YYYY-MM-DD") : undefined,
                    // endDate: values.endDate ? Moment(values.endDate).format("YYYY-MM-DD") : undefined,
                    type: values.type ? values.type : "",
                    stkCatID: values.stkCatID ? values.stkCatID : "",
                    stkSubCatID: values.stkSubCatID ? values.stkSubCatID : "",
                    // vendorID: values.vendorID ? values.vendorID : "",
                    stockID: this.state.stockID ? this.state.stockID : '',
                    // locDesc:this.locDesc?this.locDesc:undefined,
                    // typeDesc:this.typeDesc?this.typeDesc:undefined,
                    impTypeID:values.impType ? values.impType :'',
                    orderTypeID:values.orderType ? values.orderType :'',//医嘱子类
                    dosFormID:values.dosForm ? values.dosForm :'',
                    manfID:values.manf ? values.manf :'',
                    poisonID:values.poison ? values.poison :'',
                    incZeroBat:values.incZeroBat ? values.incZeroBat :'',
                    excNotFlag:values.excNotFlag ? values.excNotFlag :'',
                }
                //  console.log({ params: params,raqName:raqName });
                this.setState({ params: params});
            })
        };

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

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

        // 显示分页总条数
        showTotal(total) {
            return `共 ${total} 条`;
        }

        // 获取描述列表数据
        getModalListData(alias) {
            let data = {
                params: [
                    {
                        stockAlias: alias, // 库存项描述别名
                        frLocID: this.state.frLocDr ? this.state.frLocDr : undefined
                    }
                ]
            };
            $http.post('urlS', this, {
                code: "02020042",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        for(var i = 0; i < res.result.length; i++) {
                            res.result[i].key = i
                        }
                        this.setState({ modalData: res.result})
                    } else {
                        this.setState({ modalData: [] })
                    }
                }
            })
        }

        // 描述回车事件
        stockDescOnPressEnter = (e) => {
            let top = document.getElementById('stock-desc').getBoundingClientRect().top + 40 + 'px';
            let left = document.getElementById('stock-desc').getBoundingClientRect().left + 100 + 'px';
            this.setState({
                top: top,
                left: left
            });
            this.getModalListData(e.target.value);
            this.child.showModal();
        };

        // 当描述失去焦点的时候
        stastockDescOnBlure = (e) => {
            if (this.state.ModalRowData && (e.target.value !== this.state.ModalRowData.stockDesc)) {
                this.props.form.setFieldsValue({stockDesc: undefined});
            }
        };


        // 操作弹框
        onClickRowPublic = (record) => {
            return {
                onDoubleClick: () => {
                    this.confirmSelection(record);
                }
            }
        };
        // 双击选中行数据的回调
        confirmSelection = (record) => {
            // this.resetFields();
            this.setState({ModalRowData: record}, () => {
                this.setState({stockID: record.stockID});
                this.props.form.setFieldsValue({stockDesc: record.stockDesc});
                // 关闭弹窗
                this.child.handleCancel();
            })
        };


        onRef = (ref) => {
            this.child = ref;
        };

        render() {
            const { getFieldDecorator } = this.props.form;
            let {ChineseEnglishSize,userInfo,modalColumns} = this.state;
            //库存大类
            let stkCatSelect = [];
            this.state.SelectData.stkCat && this.state.SelectData.stkCat.map((item, i) => {
                stkCatSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //库存子类
            let stkSubCatSelect = [];
            this.state.SelectData.stkSubCat && this.state.SelectData.stkSubCat.map((item, i) => {
                stkSubCatSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //生产厂家
            let manfSelect = [];
            this.state.SelectData.manf && this.state.SelectData.manf.map((item, i) => {
                manfSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //剂型
            let formSelect = [];
            this.state.SelectData.form && this.state.SelectData.form.map((item, i) => {
                formSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //管制分类
            let poisonSelect = [];
            this.state.SelectData.poison && this.state.SelectData.poison.map((item, i) => {
                poisonSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //进口标志
            let importSelect = [];
            this.state.SelectData.import && this.state.SelectData.import.map((item, i) => {
                importSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //医嘱子类
            let orderTypeSelect = [];
            this.state.SelectData.orderType && this.state.SelectData.orderType.map((item, i) => {
                orderTypeSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            //类型
            let typeSelect = [];
            this.state.SelectData.type && this.state.SelectData.type.map((item, i) => {
                typeSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            let param = {
                width: '600px',
                top: this.state.top,
                left: this.state.left,
                columns: modalColumns.length>0?modalColumns:this.modalColumns,
                data: this.state.modalData,
                isOnRow: true,
                TableClassName: 'tab-placeholder',
            };
            return (
                <div className="allCourtyardInventory">
                    <Row >
                        <Col span={6}>
                            <Card
                                size="small"
                                title={ChineseEnglishSize.WholeHospitalInventorywholeHospitalInquiry ? ChineseEnglishSize.WholeHospitalInventorywholeHospitalInquiry.descripts:'全院库存查询'}
                            >
                                <div  style={{height: store.getState().tableHeight.y +115 + 'px'}}>
                                    <Form>
                                        <Row>
                                            <Col className="time">
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorystartDate ? ChineseEnglishSize.WholeHospitalInventorystartDate.descripts:'日期'}>
                                                    {getFieldDecorator('startDate',{
                                                        initialValue:  this.state.SelectData.params&&this.state.SelectData.params.startDate ? Moment(this.state.SelectData.params.startDate, 'YYYY-MM-DD') : Moment(Moment().format('L'), 'YYYY-MM-DD'),
                                                        rules:[{ required : ChineseEnglishSize.WholeHospitalInventorystartDate ? ChineseEnglishSize.WholeHospitalInventorystartDate.required =='Y'? true:false: false, message: ChineseEnglishSize.WholeHospitalInventorystartDate ? ChineseEnglishSize.WholeHospitalInventorystartDate.message:'日期不能为空'}]
                                                    })(
                                                        <DatePicker format="YYYY-MM-DD" />
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryStkCat ? ChineseEnglishSize.WholeHospitalInventoryStkCat.descripts:'库存大类'}>
                                                    {getFieldDecorator('stkCatID',{
                                                        initialValue:undefined,
                                                        rules:[{ required : false, message: '库存大类不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {stkCatSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorytype ? ChineseEnglishSize.WholeHospitalInventorytype.descripts:'类型'}>
                                                    {getFieldDecorator('type',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '类型不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {typeSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col  id="stock-desc">
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorydrugName ? ChineseEnglishSize.WholeHospitalInventorydrugName.descripts:'药品名称'}>
                                                    {getFieldDecorator('stockDesc',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '药品名称不能为空'}]
                                                    })(
                                                        <Input ref={this.myRef1} placeholder={ChineseEnglishSize.WholeHospitalInventorycarriageSearch ? ChineseEnglishSize.WholeHospitalInventorycarriageSearch.descripts:'回车检索药品相关信息'} onPressEnter={this.stockDescOnPressEnter} onBlur={this.stastockDescOnBlure} />
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryimFlag ? ChineseEnglishSize.WholeHospitalInventoryimFlag.descripts:'进口标志'}>
                                                    {getFieldDecorator('impType',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '进口标志不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {importSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryinventorySubclass ? ChineseEnglishSize.WholeHospitalInventoryinventorySubclass.descripts:'库存子类'}>
                                                    {getFieldDecorator('stkSubCatID',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '库存子类不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {stkSubCatSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorymedicalScorpion ? ChineseEnglishSize.WholeHospitalInventorymedicalScorpion.descripts:'医嘱子类'}>
                                                    {getFieldDecorator('orderType',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '医嘱子类不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {orderTypeSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryregulatoryClassification ? ChineseEnglishSize.WholeHospitalInventoryregulatoryClassification.descripts:'管制分类'}>
                                                    {getFieldDecorator('poison',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '管制分类不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {poisonSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorymanufacturer ? ChineseEnglishSize.WholeHospitalInventorymanufacturer.descripts:'生产厂家'}>
                                                    {getFieldDecorator('manf',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '生产厂家不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {manfSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventorydosageForm ? ChineseEnglishSize.WholeHospitalInventorydosageForm.descripts:'剂型'}>
                                                    {getFieldDecorator('dosForm',{
                                                        initialValue: undefined,
                                                        rules:[{ required : false, message: '剂型不能为空'}]
                                                    })(
                                                        <Select allowClear>
                                                            {formSelect}
                                                        </Select>
                                                    ) }
                                                </FormItem>
                                            </Col>
                                            <Row>
                                                <Col>
                                                    <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryincZeroBat ? ChineseEnglishSize.WholeHospitalInventoryincZeroBat.descripts:'仅在用品种'}>
                                                        {getFieldDecorator('incZeroBat',{
                                                            valuePropName: 'checked',
                                                            initialValue: false,
                                                            rules:[{required: false}]
                                                        })(
                                                            userInfo.language == 'EN' ?  <Switch checkedChildren="Yes" unCheckedChildren="No" /> : <Switch checkedChildren="是" unCheckedChildren="否" />
                                                        ) }
                                                    </FormItem>
                                                </Col>
                                            </Row>
                                            <Col>
                                                <FormItem {...formItemLayout} label={ChineseEnglishSize.WholeHospitalInventoryexcNotFlag ? ChineseEnglishSize.WholeHospitalInventoryexcNotFlag.descripts:'仅不可用品种'}>
                                                    {getFieldDecorator('excNotFlag',{
                                                        valuePropName: 'checked',
                                                        initialValue: false,
                                                        rules:[{required: false}]
                                                    })(
                                                        userInfo.language == 'EN' ?  <Switch checkedChildren="Yes" unCheckedChildren="No" /> : <Switch checkedChildren="是" unCheckedChildren="否" />
                                                    ) }
                                                </FormItem>
                                            </Col>
                                        </Row>
                                    </Form>
                                    <Row style={{margin: '5px 0 18px 80px'}} className="button">
                                        <Button style={{marginRight: '30px'}} onClick={this.searchInformation}>{ChineseEnglishSize.WholeHospitalInventorysearch ? ChineseEnglishSize.WholeHospitalInventorysearch.descripts:'查询'}</Button>
                                        <Button type="primary" onClick={this.handleReset}>{ChineseEnglishSize.WholeHospitalInventoryclean ? ChineseEnglishSize.WholeHospitalInventoryclean.descripts:'清屏'}</Button>
                                    </Row>
                                </div>

                            </Card>
                        </Col>
                        <Col span={18} >
                            <div style={{ height: store.getState().tableHeight.y + 200 + 'px' }}>
                                <Reports
                                    raqName={this.state.raqName}
                                    params={this.state.params}

                                />
                            </div>
                        </Col>
                    </Row>
                    <PublicModalTable onClickRowPublic={this.onClickRowPublic}  param={param} onRef={this.onRef} />
                </div>
            )
        }
    }
);
export default AllCourtyardInventory;