// 物资库存统计
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";
var serialNumber = 0; //统计单序列号
const TabPane = Tabs.TabPane;
const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:7 },
    wrapperCol: { span: 17 },
};

const InventoryStatistics = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                data: [],
                SelectData: {},
                inOpLocDr:'',
                pageSize2: 10,
                page2: 1,
                TableDataEditField: {},
                TableData: [],
                incZeroBus: '',
                excNoBus: '',
                checkboxStateI: false,
                checkboxStateE: false,
                number: 1,
                ChineseEnglishSize:{},
            }
        }

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

        componentWillUnmount() {
            // 组件销毁前将静止让setState修改state的状态，防止内存泄漏
            this.setState = (state,callback)=>{ return; }
        }
        
        //获取form中英翻译
        getFormChineseEnglisth=(v)=>{
            this.setState({ChineseEnglishSize:v})
        }

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

        //点击切换表格数据
        callback=(e, page,pageSize)=> {
            this.setState({number: e},()=>{
                let locID = this.props.form.getFieldValue('Loc');
                let endDate = this.props.form.getFieldValue('StartDate');
                let stkCatID = this.props.form.getFieldValue('stkCat');
                let stkSubCatID = this.props.form.getFieldValue('stkSubCat');
                let data = {
                    params: [{
                        locID: locID ? locID : undefined,
                        endDate: endDate ? Moment(endDate).format('YYYY-MM-DD') : undefined,
                        stkCatID: stkCatID ? stkCatID : undefined,
                        stkSubCatID: stkSubCatID ? stkSubCatID : undefined,
                        excNoBus: this.state.excNoBus ? this.state.excNoBus : undefined,
                        incZeroBus: this.state.incZeroBus ? this.state.incZeroBus : undefined,

                    }],
                    pagination: [
                        {
                            pageSize:pageSize ? pageSize :this.state.pageSize2,
                            currentPage:page ? page : this.state.page2,
                            sort:"ID",
                            dir:"asc",
                        }
                    ]
                };
                $http.post('urlS', this, {
                    code: this.state.number == 2 ?  '02060122' : '02060121',
                    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 });
                        }
                    }
                })
            })
        };

        //控制条件输入框
        checkboxShowI=(event)=>{
            this.setState({checkboxStateI: event.target.checked})
        };
        addConditionI=(e)=> {
            this.setState({incZeroBus: e.target.value})  ;
        };
        //控制条件输入框
        checkboxShowE=(event)=>{
            this.setState({checkboxStateE: event.target.checked})
        };
        addConditionE=(e)=> {
            this.setState({excNoBus: e.target.value})  ;
        };

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

        onRefTable = (ref) => {
            this.childTable = ref;
        };
        //清空表格内容
        handleReset = () => {
            this.props.form.resetFields();
            this.setState({incZeroBus: '',excNoBus: '',  checkboxStateI: false, checkboxStateE: false,TableDataEditField: {}})  ;
        };
        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 parm ={
                 locID :this.props.form.getFieldValue('Loc'),
                 endDate : this.props.form.getFieldValue('StartDate'),
                 stkCatID : this.props.form.getFieldValue('stkCat'),
                 stkSubCatID : this.props.form.getFieldValue('stkSubCat')
            }
            return (
                <div className="inventoryStatistics">
                    <Row>
                        <Card
                            size="small"
                            title={ChineseEnglishSize.MaterialInventoryStatisticsinventoryStatistic ? ChineseEnglishSize.MaterialInventoryStatisticsinventoryStatistic.descripts:'库存统计'}
                        >
                            <Col span={18}>
                                <Form>
                                    <Row >
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.MaterialInventoryStatisticsloc ? ChineseEnglishSize.MaterialInventoryStatisticsloc.descripts:'科室'}>
                                                {getFieldDecorator('Loc',{
                                                    initialValue: this.state.userInfo.locID ? this.state.userInfo.locID : undefined,
                                                    rules:[{ required : ChineseEnglishSize.MaterialInventoryStatisticsloc ? ChineseEnglishSize.MaterialInventoryStatisticsloc.required =='Y'? true:false: false, message: ChineseEnglishSize.MaterialInventoryStatisticsloc ? ChineseEnglishSize.MaterialInventoryStatisticsloc.message:'科室不能为空'}] 
                                                })(
                                                    <Select allowClear>
                                                        {inOpLocDrSelect}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.MaterialInventoryStatisticsStkCat ? ChineseEnglishSize.MaterialInventoryStatisticsStkCat.descripts:'库存大类'}>
                                                {getFieldDecorator('stkCat',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '库存大类不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {this.state.SelectData.stkCat && this.state.SelectData.stkCat.map((item, i) => {
                                                            return <Option value={item.id} key={i}>{item.descripts}</Option>
                                                        })}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.MaterialInventoryStatisticsstkSubCat ? ChineseEnglishSize.MaterialInventoryStatisticsstkSubCat.descripts:'库存子类'}>
                                                {getFieldDecorator('stkSubCat',{
                                                    initialValue: undefined,
                                                    rules:[{ required : false, message: '库存子类不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {this.state.SelectData.stkSubCat && this.state.SelectData.stkSubCat.map((item, i) => {
                                                            return <Option value={item.id} key={i}>{item.descripts}</Option>
                                                        })}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>

                                    </Row>
                                    <Row>
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.MaterialInventoryStatisticsStartDate ? ChineseEnglishSize.MaterialInventoryStatisticsStartDate.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>
                                        <Col span={8} style={{ textAlign: 'center'}}>
                                            <FormItem >
                                                {getFieldDecorator('incZeroBus',{
                                                    valuePropName: 'checked',
                                                    initialValue: false,
                                                    rules:[{required: false}]
                                                })(
                                                    <span>
                                                          {/*<input type="checkbox" onClick={this.checkboxShowI} style={{ verticalAlign: 'sub'}}  />*/}
                                                           <Checkbox checked={this.state.checkboxStateI}  onChange={this.checkboxShowI} />
                                                          <span>{ChineseEnglishSize.MaterialInventoryStatisticsincludeLeft ? ChineseEnglishSize.MaterialInventoryStatisticsincludeLeft.descripts:'包括'}</span>
                                                           <Input style={{width : '95px',  marginLeft: '10px'}} type="number" value={this.state.incZeroBus} disabled={!this.state.checkboxStateI} onChange={this.addConditionI}  />
                                                           <span>{ChineseEnglishSize.MaterialInventoryStatisticsincludeRight ? ChineseEnglishSize.MaterialInventoryStatisticsincludeRight.descripts:'个月有进出零库存'}</span>
                                                      </span>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={8} style={{ textAlign: 'center'}}>
                                            <FormItem >
                                                {getFieldDecorator('excNoBus',{
                                                    valuePropName: 'checked',
                                                    initialValue: false,
                                                    rules:[{required: false}]
                                                })(
                                                    <span>
                                                          {/*<input type="checkbox" onClick={this.checkboxShowE} style={{ verticalAlign: 'sub'}}  />*/}
                                                          <Checkbox checked={this.state.checkboxStateE} onChange={this.checkboxShowE} />
                                                           <span>{ChineseEnglishSize.MaterialInventoryStatisticsexcludeLeft ? ChineseEnglishSize.MaterialInventoryStatisticsexcludeLeft.descripts:'排除'}</span>
                                                           <Input style={{width : '95px',  marginLeft: '10px'}} type="number" value={this.state.excNoBus} disabled={!this.state.checkboxStateE} onChange={this.addConditionE}  />
                                                            <span>{ChineseEnglishSize.MaterialInventoryStatisticsexcledeRight ? ChineseEnglishSize.MaterialInventoryStatisticsexcledeRight.descripts:'个月无进出非零库存'}</span>
                                                      </span>
                                                ) }
                                            </FormItem>
                                        </Col>
                                    </Row>
                                </Form>
                            </Col>
                            <Col span={6}>
                                <Row style={{margin: '5px 0 18px 45px'}} className="button">
                                    <Button style={{marginRight: '30px'}} onClick={this.callback}>{ChineseEnglishSize.MaterialInventoryStatisticssearch ? ChineseEnglishSize.MaterialInventoryStatisticssearch.descripts:'查询'}</Button>
                                    <Button type="primary" onClick={this.handleReset}>{ChineseEnglishSize.MaterialInventoryStatisticsclean ? ChineseEnglishSize.MaterialInventoryStatisticsclean.descripts:'清屏'}</Button>
                                </Row>
                            </Col>
                        </Card>
                    </Row>
                    <Row>
                        <Tabs  type="card" onChange={this.callback}>
                            <TabPane tab={ChineseEnglishSize.MaterialInventoryStatisticsitemDetail ? ChineseEnglishSize.MaterialInventoryStatisticsitemDetail.descripts:'项目明细'} key="1">
                                <TableDataEditField  handelThis={this} onRef={this.onRef}  parm={parm} TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>
                            <TabPane tab={ChineseEnglishSize.MaterialInventoryStatisticsbatchDetail ? ChineseEnglishSize.MaterialInventoryStatisticsbatchDetail.descripts:'批次明细'} key="2">
                                <TableDataForm  handelThis={this} onRef={this.onRefTable}  parm={parm} TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>

                        </Tabs>
                    </Row>
                </div>
            )
        }
    }
);
export default InventoryStatistics;