// 盘点损益明细与汇总
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Tabs, Table, Input, message, Checkbox   } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import store from 'store';
import './style/index.less';
import TableDataForm from "./component/TableDataForm";
import TableDataEditField from "./component/TableDataEditField";
import {printProfitAndLossData} from '../commonPrint/stkPrint';
const TabPane = Tabs.TabPane;
const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 17 },
};

const InventoryDetailsAndSummary = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                data: [],
                SelectData: {},
                inOpLocDr:'',
                pageSize2: 20,
                page2: 1,
                TableDataEditField: {},
                TableData: [],
                number: 1,
                ChineseEnglishSize:{},
                stockID: '',
            }
        }

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

        //获取form中英翻译
        getFormChineseEnglisth=(v)=>{
            this.setState({ChineseEnglishSize:v})
        }

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


        // 获取界面默认值
        getTacitlyApproveData() {
            let data = {
                params: []
            };
            // 获取下拉数据
            $http.post('urlS', this, {
                code: "02020309",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({SelectData: res})
                    }
                }
            })
        }
        callbackCheck=(e)=>{
            this.setState({number: e },()=>{
                this.callback();
            })
        }
        //点击切换表格数据
        callback=(e, page,pageSize)=> {
            this.props.form.validateFields((errors, values) => {
                // let obj = JSON.parse(JSON.stringify(values));
                if (errors == null) {
                    let locID = this.props.form.getFieldValue('Loc');
                    let startDate = this.props.form.getFieldValue('StartDate');
                    let endDate = this.props.form.getFieldValue('endDate');
                    let stkCatID = this.props.form.getFieldValue('stkCat');
                    let type = this.props.form.getFieldValue('type');
                    let stockDesc = this.props.form.getFieldValue('stockDesc');
                    console.log('this.state.number',this.state.number)
                    let data = {
                        params:[{
                            stkLocID:locID ? locID : '',
                            drugName: stockDesc ? stockDesc : '',
                            stkCat:stkCatID?stkCatID:'',
                            searchType: this.state.number == 2 ? 'D' : 'B',
                            type:type ? type : 0,
                            startDate:startDate?Moment(startDate).format("YYYY-MM-DD"):Moment(),
                            endDate:endDate?Moment(endDate).format("YYYY-MM-DD"):Moment(),
                        }],
                        pagination: [
                            {
                                pageSize:pageSize ? pageSize :this.state.pageSize2,
                                currentPage:page ? page : this.state.page2,
                                sort:"ID",
                                dir:"asc",
                            }
                        ]
                    };
                    $http.post('urlS', this, {
                        code: '02020307',
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                for(let i = 0; i < res.rows.length; i++){
                                    res.rows[i].key = i;
                                }
                                this.setState({TableDataEditField:res });
                            }
                        }
                    })
                }
            })

        };
        setStockID=(val)=>{
            this.setState({stockID: val})
        }
        //准备打印参数
        getPrintParams=()=>{
            let locID = this.props.form.getFieldValue('Loc');
            let startDate = this.props.form.getFieldValue('StartDate');
            let endDate = this.props.form.getFieldValue('endDate');
            let stkCatID = this.props.form.getFieldValue('stkCat');
            let type = this.props.form.getFieldValue('type');
            var params=[{
                stkLocID:locID ? locID : '',
                stkCat:stkCatID?stkCatID:'',
                type:type ? type : 0,
                startDate:startDate?Moment(startDate).format("YYYY-MM-DD"):Moment(),
                endDate:endDate?Moment(endDate).format("YYYY-MM-DD"):Moment(),
                searchType: this.state.number == 2 ? 'D' : 'B',
            }];
            if (this.state.number == 2){
                if (!this.state.stockID) {
                    message.error("请先选择药品信息");
                    return;
                }
                params[0].stockCode = this.state.stockID;
            };
            console.log('444444',params)
            printProfitAndLossData(params);
        };

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

        onRefTable = (ref) => {
            this.childTable = ref;
        };
        //清空表格内容
        handleReset = () => {
            this.props.form.resetFields();
            this.childTable.handleReset();
            this.setState({TableDataEditField: {}, stockID: ''})  ;
        };
        render() {
            const { getFieldDecorator } = this.props.form;
            let {ChineseEnglishSize} = this.state;
            let userInfo = this.state.userInfo ? this.state.userInfo: {};
            //统计科室列表
            let inOpLocDrSelect = [];
            this.state.SelectData.loc && this.state.SelectData.loc.map((item, i) => {
                inOpLocDrSelect.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 stkCatSelect = [];
            this.state.SelectData.stkCat && this.state.SelectData.stkCat.map((item, i) => {
                stkCatSelect.push(<Option value={item.id} key={i}>{item.descripts}</Option>)
            });
            return (
                <div className="InventoryDetailsAndSummary">
                    <Col span={4}  style={{height: store.getState().tableHeight.y+242 + 'px'}}>
                        <Card
                            size="small"
                            title={ChineseEnglishSize.InventoryStatisticsinventoryStatistic1 ? ChineseEnglishSize.InventoryStatisticsinventoryStatistic.descripts:'盘点损益'}
                            style={{height: '100%'}}
                        >
                            <Row>
                                <Form>
                                    <Row >
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsloc ? ChineseEnglishSize.InventoryStatisticsloc.descripts:'科室'}>
                                                {getFieldDecorator('Loc',{
                                                    initialValue: this.state.userInfo.locID ? this.state.userInfo.locID : undefined,
                                                    rules:[{ required :true, message: ChineseEnglishSize.InventoryStatisticsloc ? ChineseEnglishSize.InventoryStatisticsloc.message:'科室不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {inOpLocDrSelect}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row >
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsloc1 ? ChineseEnglishSize.InventoryStatisticsloc.descripts:'药品名称'}>
                                                {getFieldDecorator('stockDesc',{
                                                    initialValue:  undefined,
                                                })(
                                                    <Input />
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsStkCat ? ChineseEnglishSize.InventoryStatisticsStkCat.descripts:'库存大类'}>
                                                {getFieldDecorator('stkCat',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '库存大类不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {stkCatSelect}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsStkCat1 ? ChineseEnglishSize.InventoryStatisticsStkCat.descripts:'盘点条件'}>
                                                {getFieldDecorator('type',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '状态不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {typeSelect}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsStartDate ? ChineseEnglishSize.InventoryStatisticsStartDate.descripts:'起初日期'}>
                                                {getFieldDecorator('StartDate',{
                                                    initialValue: this.state.SelectData.params ? Moment(this.state.SelectData.params.startDate, 'YYYY-MM-DD') : Moment(),
                                                    rules:[{required: false}]
                                                })(
                                                    <DatePicker style={{width: '100%'}} />
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                    <Row>
                                        <Col span={22}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsStartDate ? ChineseEnglishSize.InventoryStatisticsStartDate.descripts:'截止日期'}>
                                                {getFieldDecorator('endDate',{
                                                    initialValue: this.state.SelectData.params ? Moment(this.state.SelectData.params.endtDate, 'YYYY-MM-DD') : Moment(),
                                                    rules:[{required: false}]
                                                })(
                                                    <DatePicker style={{width: '100%'}} />
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                </Form>
                            </Row>
                            <Row style={{margin: '25px 0 0px 10%'}} className="button">
                                <Button onClick={this.callback}>{ChineseEnglishSize.InventoryStatisticssearch ? ChineseEnglishSize.InventoryStatisticssearch.descripts:'查询'}</Button>
                                <Button style={{margin: '0 30px'}} type="primary" onClick={this.handleReset}>{ChineseEnglishSize.InventoryStatisticsclean ? ChineseEnglishSize.InventoryStatisticsclean.descripts:'清屏'}</Button>
                                <Button  onClick={this.getPrintParams}>{ChineseEnglishSize.InventoryStatisticssearch1 ? ChineseEnglishSize.InventoryStatisticssearch.descripts:'打印'}</Button>
                                {/*<Button type="primary" onClick={this.handleReset}>{ChineseEnglishSize.InventoryStatisticsclean ? ChineseEnglishSize.InventoryStatisticsclean.descripts:'清屏'}</Button>*/}
                            </Row>
                        </Card>
                    </Col>
                    <Col span={20}>
                        <Tabs  type="card" onChange={this.callbackCheck}>
                            <TabPane tab={ChineseEnglishSize.InventoryStatisticsitemDetail1 ? ChineseEnglishSize.InventoryStatisticsitemDetail.descripts:'盘点明细'}  key="1">
                                <TableDataEditField  handelThis={this} onRef={this.onRef} TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>
                            <TabPane tab={ChineseEnglishSize.InventoryStatisticsbatchDetail1 ? ChineseEnglishSize.InventoryStatisticsbatchDetail.descripts:'盘点汇总'} key="2">
                                <TableDataForm  handelThis={this} onRef={this.onRefTable}  TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>

                        </Tabs>
                    </Col>
                </div>
            )
        }
    }
);
export default InventoryDetailsAndSummary;