import React from 'react';
import { Modal, Row, Col, Button, Table, Select , DatePicker, Pagination, message, Menu, Dropdown, Icon, Card, Switch } from 'antd';
import { $http } from 'containers/config/https';
import Moment from 'moment';
import store from "../../../../store";

const { Option } = Select;

export default class InventoryInformation extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            tableData: [],
            startDate: undefined, // 开始日期
            endDate: undefined, // 截止日期
            status: undefined, // 单据状态
            rowId: '', // 选中行id
            batchRowId:'',//批号行id
            total: 0, // 总条数
            page: 1, // 页数
            page2: 1, // 页数
            pageSize: 10, // 每页的条数
            stateCode: {},
            BatchData: [],
            BatchId: '',
        };
        this.columns = [
            { title: '加锁', dataIndex: 'lockFlag', key: '16', width: '150px', align: "center",
                render: (text, record) => (
                    <span> {record.lockFlag  ? <Icon type="check" /> : <Icon type="close" />}</span>
                )
            },
            { title: '代码', dataIndex: 'stockCode', key: '1', width: '150px', align: "center"},
            { title: '药品名称', dataIndex: 'stockDesc', key: '2', width: '150px', align: "center"},
            { title: '规格', dataIndex: 'stockSpec', key: '3', width: '150px', align: "center"},
            { title: '剂型', dataIndex: 'stockForm', key: '4', width: '150px', align: "center"},
            { title: '货位码', dataIndex: 'stkLocBin', key: '5', width: '150px', align: "center"},
            { title: '基本单位库存', dataIndex: 'locBQty', key: '6', width: '150px', align: "center"},
            { title: '入库单位库存', dataIndex: 'locPQty', key: '8', width: '150px', align: "center"},
            { title: '占用数', dataIndex: 'dirQty', key: '9', width: '150px', align: "center"},
            { title: '进价', dataIndex: 'rp', key: '11', width: '150px', align: "center"},
            { title: '进价金额', dataIndex: 'rpAmt', key: '12', width: '150px', align: "center"},
            { title: '售价', dataIndex: 'sp', key: '13', width: '150px', align: "center"},
            { title: '售价金额', dataIndex: 'spAmt', key: '14', width: '150px', align: "center"},
        ]

        this.detailColumns = [
            { title: '医嘱激活', dataIndex: 'ordActive', key: '1', width: '150px', align: "center",
                render: (text, record) => (
                    <span> {record.ordActive === 1 ? <Icon type="check" /> : <Icon type="close" />}</span>
                )
            },
            { title: '库存激活', dataIndex: 'stkActive', key: '2', width: '150px', align: "center",
                render: (text, record) => (
                    <span> {record.stkActive === 1  ? <Icon type="check" /> : <Icon type="close" />}</span>
                )
            },
            { title: '批号', dataIndex: 'batExp', key: '3', width: '150px', align: "center" },
            { title: '有效期', dataIndex: 'expDate', key: '4', width: '150px', align: "center" },
            { title: '生产日期', dataIndex: 'produceDate', key: '5', width: '150px', align: "center" },
            { title: '库存数', dataIndex: 'batQty', key: '6', width: '150px', align: "center" },
            { title: '占用数', dataIndex: 'dirQty', key: '7', width: '150px', align: "center" },
            { title: '进价(基本单位)', dataIndex: 'baseRp', key: '9', width: '150px', align: "center" },
            { title: '售价(基本单位)', dataIndex: 'baseSp', key: '10', width: '150px', align: "center" },
            { title: '进价(售价单位)', dataIndex: 'purRp', key: '11', width: '150px', align: "center" },
            { title: '售价(售价单位)', dataIndex: 'purSp', key: '12', width: '150px', align: "center" },
            { title: '生产企业', dataIndex: 'manfDesc', key: '13', width: '200px', align: "center" },
            { title: '供应商', dataIndex: 'vendorDesc', key: '14', width: '200px', align: "center" },
        ];
    }

    componentDidMount() {
        // 获取列表数据
        this.props.onRef(this);
    }

    //props变化是即使更新props
    componentWillReceiveProps(props){
        this.setState({ stateCode: props.SelectData, BatchData: props.BatchData, page2: props.page})
    }


    // 打开弹框
    showModal = () => {
        this.setState({
            visible: true
        });
    };

    // 确定
    handleOk = (e) => {
        if (this.state.batchRowId) {
            this.props.BatchDetail(this.state.batchRowId);
            this.handleCancel()
        } else {
            message.error("请选择一条需要操作的数据");
        }
    };

    // 关闭
    handleCancel = (e) => {
        this.setState({
            visible: false,
            status: undefined,
            tableData:[],
            total:0,
            batchRowId:'',
            rowId:0
        });
    };

    // 取消选中
    handleUncheck() {
        this.setState({rowId: '',batchRowId: ''});
    }

    // 设置库存属性
    onClickRow = (record) => {
        return {
            // 单击行选中
            onClick: () => {
                if (this.state.rowId === '') {
                    // 保存行数据以及ID
                    this.setState({rowId: record.stkLocID});
                    this.getBatchData(record.stkLocID);
                } else {
                    if (this.state.rowId !== record.stkLocID) {
                        this.setState({rowId: record.stkLocID});
                        this.getBatchData(record.stkLocID);
                    }else {
                        this.setState({rowId: '', tableData: []});
                    }
                }
            }
        }
    };

    // 设置批次属性
    onClickBatch = (record) => {
        return {
            // 单击行选中
            onClick: () => {
                if (this.state.batchRowId === '') {
                    // 保存行数据以及ID
                    this.setState({batchRowId: record.stkLocBatID});
                } else {
                    if (this.state.batchRowId !== record.stkLocBatID) {
                        this.setState({batchRowId: record.stkLocBatID});
                    }else {
                        this.setState({batchRowId: ''});
                    }
                }
                // this.props.BatchDetail(record.stkLocBatID);
            },
            //双击
            onDoubleClick: () => {
                // 保存行数据以及ID
                this.props.BatchDetail(record.stkLocBatID);
                this.props.onClickRowPublic(record);
            }
        }
    };

    // 获取批次信息大家都在发
    getBatchData(id) {
        let data = {
            params: [
                {
                    stkLocID: id, // 库存项Id
                }
            ],
            pagination: [
                {
                    pageSize:this.state.pageSize,
                    currentPage:this.state.page,
                }
            ],
        };
        $http.post('urlS', this, {
            code: "02020104",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    for(var i = 0; i < res.result.rows.length; i++){
                        res.result.rows[i].key = i;
                    }
                    this.setState({ tableData: res.result.rows, total: res.result.total})
                } else {
                    this.setState({ tableData: [], total: 0 })
                }
            }
        })
    }

    // 表格行的类名
    setRowClassName = (record) => {
        return record.stkLocID === this.state.rowId ? 'clickRowStyle' : '';
    };
    // 表格行的类名
    setBatchRowClassName = (record) => {
        return record.stkLocBatID === this.state.batchRowId ? 'clickRowStyle' : '';
    };

    // 页码改变的回调，参数是改变后的页码及每页条
    onChange = (page, pageSize) => {
        this.setState({page: page, pageSize: pageSize}, () => {
            let {rowId} = this.state;
            this.getBatchData(rowId)
        })
    };

    // 条数改变的回调
    onShowSizeChange = (current, size) => {
        this.setState({page: 1, pageSize: size}, () => {
            let {rowId} = this.state;
            this.getBatchData(rowId);
        })
    };

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

    render() {
        return (
            <Modal
                visible={this.state.visible}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                okText="确定"
                width={1000}
                wrapClassName='inventoryInformation'
            >
                <div>
                    <Card
                        size="small"
                        title="库存信息">
                        <div style={{height: '260px'}} className="inventory">
                            <Table
                                columns={this.columns}
                                dataSource={this.state.BatchData.rows}
                                pagination={false}
                                style={{overflow: 'auto', height: '215px'}}
                                onRow={this.onClickRow}
                                rowClassName={this.setRowClassName}
                                scroll={{ x: 1950, y: store.getState().tableHeight.y / 2 - 125 }}
                            />
                            <Pagination
                                showTotal={() => this.showTotal(this.state.BatchData.total)}
                                size="small"
                                showQuickJumper
                                showSizeChanger
                                defaultCurrent={1}
                                current={this.state.page2}
                                total={this.state.BatchData.total ? this.state.BatchData.total : 0}
                                onChange={(c,d) => this.props.onStrike(c,d)}
                                onShowSizeChange={(a , b) => this.props.onShowSizePage(a , b)}
                                style={{textAlign: "right", marginTop: '13px'}}
                            />
                        </div>

                    </Card>
                    <Card
                        size="small"
                        title="批次信息">

                        <div style={{height: '245px'}} className="batch">
                            <Table

                                columns={this.detailColumns}
                                dataSource={this.state.tableData}
                                pagination={false}
                                style={{ height: '215px'}}
                                onRow={this.onClickBatch}
                                rowClassName={this.setBatchRowClassName}
                                scroll={{ x: 2050, y: store.getState().tableHeight.y / 2 - 26 }}
                            />
                            <Pagination
                                showTotal={() => this.showTotal(this.state.total)}
                                size="small"
                                showQuickJumper
                                showSizeChanger
                                defaultCurrent={1}
                                current={this.state.page}
                                total={this.state.total}
                                onChange={this.onChange}
                                onShowSizeChange={this.onShowSizeChange}
                                style={{textAlign: "right", marginTop: '13px'}}
                            />
                        </div>
                    </Card>
                </div>
            </Modal>
        )
    }
}