// 物资批次明细
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 Moment from "../InventoryStatistics";
import CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';

const TableDataForm = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                detailsId: '',//统计单ID
                TableDataEditField:[], //统计单明细数据
                checkData: {},
                pageSize2: 10,
                page2: 1,
                detailTotal: 0,
                modalColumns:[],
                modalColumnsWith:''
            };
            // 统计单详细表头
            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();
        }

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

        // 页码改变的回调，参数是改变后的页码及每页条数 统计单详情
        onChange2 = (page, pageSize) => {
            this.setState({page2: page, pageSize2: this.state.pageSize2}, () => {
                // 重新调用查询方法
                this.props.handelThis.callback(2,this.state.page2, this.state.pageSize2);
            });
        };

        onShowSizeChange2 = (current, size) => {
            this.setState({page2: current, pageSize2: size}, () => {
                // 重新调用查询方法
                this.onChange2(current,size);
            })
        };
        // 显示分页总条数
        showTotal(total) {
            return `共 ${total} 条`;
        }
        //组件权限编辑器
        onColumnRef = (ref) => {
            this.columnRef = ref;
        };
        render() {
            let {modalColumns} = this.state;
            return (
                <div className="TableDataForm">
                    <Row>
                        <Col>
                                <div>
                                    <Table
                                        columns={modalColumns.length>0?modalColumns:this.modalColumns}
                                        dataSource={this.state.TableDataEditField}
                                        pagination={false}
                                        bordered
                                        scroll={{ y: store.getState().tableHeight.y - 50 + 'px' }}
                                        style={{height: store.getState().tableHeight.y - 15 + 'px'}}
                                        rowKey={record => record.key}
                                    />

                                    <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)} />
                                </div>
                        </Col>
                    </Row>
                    <ColumnAuthoritysModel
                        clientWidth='800px'
                        compontName='MaterialInventoryStatisticsBat'
                        configType="C"
                        onRef={this.onColumnRef}
                        reloadData={this.getDetailTableColumns}
                    />
                </div>
            )
        }
    }
);
export default TableDataForm;