// 项目明细
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 CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import store from 'store';
import Moment from "../InventoryStatistics";

const TableDataEditField = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: JSON.parse(window.sessionStorage.userData),
                checkData:{}, //查询条件数据
                pageSize: 20,
                page: 1,
                total:0,
                TableData: [], //表格数据
                columns:[],
                columnsWidth:''
            }
            // 统计单的数据表头
            this.columns = [
                {
                    key: 1,
                    title: '代码',
                    dataIndex: 'stockCode',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 2,
                    title: '名称',
                    dataIndex: 'stockDesc',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 3,
                    title: '货位',
                    dataIndex: 'frLocDesc',
                    align: "center",
                    width: '11%'
                },
                {
                    key: 4,
                    title: '库存(包装单位)',
                    dataIndex: 'purQty',
                    align: "center",
                    width: '10%'
                },
                {
                    key: 5,
                    title: '包装单位',
                    dataIndex: 'purUnitDesc',
                    align: "center",
                    width: '5%'
                },
                {
                    key: 6,
                    title: '库存(基本单位)',
                    dataIndex: 'baseQty',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 7,
                    title: '基本单位',
                    dataIndex: 'baseUnitDesc',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 8,
                    title: '零售价',
                    dataIndex: 'sp',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 9,
                    title: '最新进价',
                    dataIndex: 'rp',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 10,
                    title: '售价金额',
                    dataIndex: 'spAmt',
                    align: "center",
                    width: '7%'
                },
                {
                    key: 11,
                    title: '进价金额',
                    dataIndex: 'rpAmt',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 12,
                    title: '规格',
                    dataIndex: 'stockSpec',
                    align: "center",
                    width: '6%'
                },
                {
                    key: 13,
                    title: '生产厂家',
                    dataIndex: 'manfDesc',
                    align: "center",
                    width: '7%'
                },
            ]
        }
        componentDidMount(){
            this.props.onRef(this);
            this.getTableColumns();
            //获取表格数据
            // this.getTableData();
        }

        // 获取明系列表头数据
        getTableColumns = () => {
            let {userInfo} = this.state;
            let data = {
                params: [{
                    reactCode: ['InventoryStatistics'],
                    compontName: 'ItemDetails',
                    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: 'ItemDetails'},()=>{
                                    this.columnRef.onOpenModel();
                                })}}>{userInfo.language== 'EN' ? item.enDescripts : item.title}</div>
                            }
                        }) : '';
                        let filterArr = arr.filter(function(item){ return item.display === 'Y'});
                        this.setState({columns: filterArr, columnsWidth: res.totalWidth,detailWidthTotal: res.totalWidth},()=>{
                            this.props.handelThis.getFormChineseEnglisth(userInfo.language=='EN' ? res.result.language.EN : res.result.language.CN)
                        })
                    }
                }
            });
        };

        componentWillReceiveProps(props){
            if(props.TableDataEditField){
                this.setState({ TableData: props.TableDataEditField.rows,total: props.TableDataEditField.total})
            }
        }

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

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

        onColumnRef = (ref) => {
            this.columnRef = ref;
        }

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

        render() {
            let {columns, columnsWidth} = this.state;
            return (
                <div className="TableDataEditField">
                    <Row>
                        <Col>
                            <div>
                                <Table
                                    columns={columns.length>0?columns:this.columns}
                                    dataSource={this.state.TableData}
                                    pagination={false}
                                    bordered
                                    scroll={{x:columnsWidth? columnsWidth : 1700, y:  store.getState().tableHeight.y -43 + 'px' }}
                                    style={{height:  store.getState().tableHeight.y + 'px'}}
                                    rowKey={record => record.key}

                                />
                                <CommonPagination size='small' current={this.state.page} total={this.state.total} search= 'search'
                                                  onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.TableData}
                                                  loading={this.state.loading} onChange={this.onChange.bind(this)} pageSize={true} />
                            </div>
                        </Col>
                    </Row>
                    <ColumnAuthoritysModel
                        clientWidth='800px'
                        compontName="ItemDetails"
                        configType="C"
                        onRef={this.onColumnRef}
                        reloadData={this.getTableColumns}
                    />
                </div>
            )
        }
    }
);
export default TableDataEditField;