// 库存统计
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: 20,
                page2: 1,
                TableDataEditField: {},
                TableData: [],
                incZeroBus: '',
                excNoBus: '',
                checkboxStateI: false,
                checkboxStateE: false,
                number: 1,
                ChineseEnglishSize:{},
            }
            // 统计单的数据表头
            this.columns = [
                {
                    key: 1,
                    title: '代码',
                    dataIndex: 'transNO',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 2,
                    title: '名称',
                    dataIndex: 'stkReqNO',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 3,
                    title: '货位',
                    dataIndex: 'frLocDesc',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 4,
                    title: '库存(包装单位)',
                    dataIndex: 'toLocDesc',
                    align: "center",
                    width: '10%'
                },
                {
                    key: 5,
                    title: '包装单位',
                    dataIndex: 'stateDesc',
                    align: "center",
                    width: '5%'
                },
                {
                    key: 6,
                    title: '库存(基本单位)',
                    dataIndex: 'createDate',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 7,
                    title: '基本单位',
                    dataIndex: 'createUserName',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 8,
                    title: '零售价',
                    dataIndex: 'stkCatDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 9,
                    title: '最新进价',
                    dataIndex: 'auditUserName',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 10,
                    title: '售价金额',
                    dataIndex: 'auditDate',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 11,
                    title: '进价金额',
                    dataIndex: 'rpAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 12,
                    title: '规格',
                    dataIndex: 'spAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 13,
                    title: '生产厂家',
                    dataIndex: 'difRS',
                    align: "center",
                    width: '7%'
                },
            ]
            // 统计单详细表头
            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.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: "02020191",
                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 ?  '02020193' : '02020192',
                    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.InventoryStatisticsinventoryStatistic ? ChineseEnglishSize.InventoryStatisticsinventoryStatistic.descripts:'库存统计'}
                        >
                            <Col span={18}>
                                <Form>
                                    <Row >
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsloc ? ChineseEnglishSize.InventoryStatisticsloc.descripts:'科室'}>
                                                {getFieldDecorator('Loc',{
                                                    initialValue: this.state.userInfo.locID ? this.state.userInfo.locID : undefined,
                                                    rules:[{ required : ChineseEnglishSize.InventoryStatisticsloc ? ChineseEnglishSize.InventoryStatisticsloc.required =='Y'? true:false: false, message: ChineseEnglishSize.InventoryStatisticsloc ? ChineseEnglishSize.InventoryStatisticsloc.message:'科室不能为空'}]
                                                })(
                                                    <Select allowClear>
                                                        {inOpLocDrSelect}
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={8}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.InventoryStatisticsStkCat ? ChineseEnglishSize.InventoryStatisticsStkCat.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.InventoryStatisticsstkSubCat ? ChineseEnglishSize.InventoryStatisticsstkSubCat.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.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>
                                        <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.InventoryStatisticsincludeLeft ? ChineseEnglishSize.InventoryStatisticsincludeLeft.descripts:'包括'}</span>
                                                           <Input style={{width : '95px',  marginLeft: '10px'}} type="number" value={this.state.incZeroBus} disabled={!this.state.checkboxStateI} onChange={this.addConditionI}  />
                                                           <span>{ChineseEnglishSize.InventoryStatisticsincludeRight ? ChineseEnglishSize.InventoryStatisticsincludeRight.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.InventoryStatisticsexcludeLeft ? ChineseEnglishSize.InventoryStatisticsexcludeLeft.descripts:'排除'}</span>
                                                           <Input style={{width : '95px',  marginLeft: '10px'}} type="number" value={this.state.excNoBus} disabled={!this.state.checkboxStateE} onChange={this.addConditionE}  />
                                                            <span>{ChineseEnglishSize.InventoryStatisticsexcledeRight ? ChineseEnglishSize.InventoryStatisticsexcledeRight.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.InventoryStatisticssearch ? ChineseEnglishSize.InventoryStatisticssearch.descripts:'查询'}</Button>
                                    <Button type="primary" onClick={this.handleReset}>{ChineseEnglishSize.InventoryStatisticsclean ? ChineseEnglishSize.InventoryStatisticsclean.descripts:'清屏'}</Button>
                                </Row>
                            </Col>
                        </Card>
                    </Row>
                    <Row>
                        <Tabs  type="card" onChange={this.callback}>
                            <TabPane tab={ChineseEnglishSize.InventoryStatisticsitemDetail ? ChineseEnglishSize.InventoryStatisticsitemDetail.descripts:'项目明细'}  key="1">
                                <TableDataEditField  handelThis={this} onRef={this.onRef}  parm={parm} TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>
                            <TabPane tab={ChineseEnglishSize.InventoryStatisticsbatchDetail ? ChineseEnglishSize.InventoryStatisticsbatchDetail.descripts:'批次明细'} key="2">
                                <TableDataForm  handelThis={this} onRef={this.onRefTable}  parm={parm} TableDataEditField={ this.state.TableDataEditField} />
                            </TabPane>

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