//生产企业维护单
import React from 'react';
import { Card, Row,Radio, Col, Form, DatePicker, Select, Button, Checkbox, Table, Switch, message,TimePicker, Pagination, Input } from 'antd';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
import Reports from '../../raqSoft/reportsEmbed';
import './style/index.less';
import { BrowserRouter as Router,Route} from 'react-router-dom';
import Moment from 'moment';
import store from 'store';

const { Option } = Select;
const FormItem = Form.Item;
const formItemLayout = {    // label 和input宽度
    labelCol: { span:8 },
    wrapperCol: { span: 16 },
};
const formItemItem  = {
    labelCol: {span: 10},
    wrapperCol: {span: 14}
};

const InventoryProfitAndLossDetails = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                selectLocList:[], //科室列表数据
                selectList:[],//损益列表数据
                userInfo: {}, //表单数据详情
                pageSize: 10,
                page: 1,
                total:0,
                rowKey:'', //行选中id
                rowData: {}, //当前行数据
                language: JSON.parse(window.sessionStorage.userData),
                ChineseEnglishSize: [],
                params:{},
                reportName:"inventoryProfitAndLossDetails"

            }
        
            // 库存单的数据表头
            this.columns = [

                {
                    key: 1,
                    title: '单号',
                    dataIndex: 'cNO',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 2,
                    title: '日期',
                    dataIndex: 'cAdjDate',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 3,
                    title: '调整人员',
                    dataIndex: 'cAdjUser',
                    align: "center",
                    width: '20%'
                }
            ]
        }
        
        componentDidMount() {
            // 初始化数据
            this.getSelectListData();
            
            
        }

        componentWillUnmount() {
            // 组件销毁前将静止让setState修改state的状态，防止内存泄漏
            this.setState = (state,callback)=>{ return; }
        }

        //清空表格内容
        resetFields = () => {
            this.props.form.resetFields();
            this.setState({userInfo: {}, rowKey: '', rowData: ''});
        };
        handleReset=()=> {
            this.resetFields();
            
        }
        searchInformation=()=>{
            this.setState({page: 1},()=>{
                this.getSelectList()
            })
        };
        // 获取招标名称列表数据
        getSelectList=()=>{
            let locID = this.props.form.getFieldValue('Loc');
            let startDate = this.props.form.getFieldValue('startDate');
            let endDate = this.props.form.getFieldValue('endDate');
            let data = {
                params: [
                    {
                        locID : locID ? locID :undefined,
                        startDate : startDate ? startDate :undefined,
                        endDate : endDate ? endDate :undefined,
                    }
                ],
                pagination: [
                    {
                        pageSize:this.state.pageSize,
                        currentPage:this.state.page,
                        sort:"ID",
                        dir:"asc",
                    }
                ],
            };
            $http.post('urlS', this, {
                code: "02020295",
                data: data,
                success: function (res) {
                    if(res.errorCode==0 && res.result.rows.length > 0){
                        for(let i = 0; i < res.result.rows.length; i++){
                            res.result.rows[i].key = i;
                        }
                        this.setState({ selectList: res.result.rows, total: res.result.total});
                    }else {
                        
                        if (language.language =='EN'){
                            message.error("No related data was found");
                        }else{
                            message.error("没数据");
                        }
                      
                    }
                }
            });
        };
        //获取初始化页面的下拉列表数据
        getSelectListData=()=>{
            $http.post('urlS', this, {
                code: "02020131",
                data: {params:[],
                        pagination:[]},
                success: function (res) {
                    let id=this.state.language.locID;
                    let descripts=this.state.language.locDesc;
                    
                    this.setState({ selectLocList: res.loc,
                            userInfo:{id:id,descripts:descripts},
                        });
                        
                       
                        // userInfo:this.state.concat({ID:ID,LocDr:LocDr})
                   
                  
                   
                }
            });
        }
                // 设置行属性
        onClickRow = (record) => {
            return {
                // 单击行选中
                onClick: (e) => {
                    e.stopPropagation();
                    
                    if (this.state.rowKey === '') {
                        // 保存行数据以及ID
                        this.setState({rowKey: record.ID});
                        
                    } else {
                        if (this.state.rowKey !== record.ID) {
                            this.setState({rowKey: record.ID});
                          
                        } 
                    }
                   
                    this.searchInformation(record.ID);
                }
            }
        };
        // 根据表单获取列表数据
        searchInformation=(id)=>{
            let locID = this.props.form.getFieldValue('Loc');
            let startDate = this.props.form.getFieldValue('startDate');
            let endDate = this.props.form.getFieldValue('endDate');
            let takeID=id
            let params = {
                                locID : locID ? locID :undefined,
                                takeID : takeID ? takeID :undefined,
                                startDate : startDate ? Moment(startDate).format("YYYY-MM-DD") :undefined,
                                endDate : endDate ? Moment(endDate).format("YYYY-MM-DD") :undefined,
                                tableName: "盘点损益统计"
                    };
            this.setState({params:params})
           
 
        };


       

      

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

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

        // 显示分页总条数
        showTotal(total) {
            return `共 ${total} 条`;
        }
        //页面中英文翻译
        getDetailTableColumns = () => {
            let {language} = this.state;
            let data = {
                params: [{
                    reactCode: ['InventoryProfitAndLossDetails'],
                    compontName: 'InventoryProfitAndLossDetails',
                    type: 'C',
                }]
            }
            $http.post('urlS', this, {
                code: '01040073',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ChineseEnglishSize:  language.language=='EN' ? res.result.language.EN : res.result.language.CN})
                    }
                }
            })
        };
        render() {
            const { getFieldDecorator } = this.props.form;
            const {userInfo,ChineseEnglishSize } = this.state;
            return (
                <div className="InventoryProfitAndLossDetails">
                    <Row>
                    <Col span={6}>
                        <Row>
                            <Card
                                size="small"
                                title={<div className="card-title-left-icon"><div></div>损益单查询</div>}
                            >
                                <Col >
                                   
                                    <Row>
                                    <Col style={{textAlign: 'center'}} >
                                                <FormItem label="科室" {...formItemItem}>
                                                    {getFieldDecorator('Loc', {
                                                         initialValue: userInfo.id ? userInfo.id : "",
                                                        rules: [
                                                            {
                                                              type: 'text',
                                                            },
                                                            {
                                                              required: false,
                                                              message: 'Please input your Loc',
                                                            },
                                                          ],
                                                    })(
                                                       <Select  showSearch optionFilterProp="seatchprop" allowClear style={{width: '100%'}}>
                                                    {this.state.selectLocList.length>0 && this.state.selectLocList.map(item => {
                                                        return <Option value={item.id} key={item.id} title={item.descripts} seatchprop={item.descripts}>{item.descripts}</Option>
                                                    })}
                                                </Select>
                                                    )}
                                                </FormItem>
                                            </Col>
                                        </Row>
                                        <Row>
                                        <Col style={{textAlign: 'center'}} >
                                            <FormItem label="开始日期" {...formItemItem}>
                                                {getFieldDecorator('startDate', {
                                                     initialValue: Moment(new Date()),
                                                    rules:[{ required :false, message: 'Please select startDate!' }]
                                                })(
                                                    <DatePicker
                                                        format="YYYY-MM-DD"
                                                        style={{width:'100%'}}
                                                    >
                                                    </DatePicker>
                                                )}
                                            </FormItem>
                                          </Col>
                                    
                                        </Row>
                                       
                                        <Row>
                                        <Col style={{textAlign: 'center'}} >
                                            <FormItem label="结束日期" {...formItemItem}>
                                                {getFieldDecorator('endDate', {
                                                     initialValue: Moment(new Date()),
                                                  rules:[{ required :false, message: 'Please select startDate!' }]
                                                })(
                                                    <DatePicker
                                                        format="YYYY-MM-DD"
                                                        style={{width:'100%'}}
                                                    >
                                                    </DatePicker>
                                                )}
                                            </FormItem>
                                          </Col>
                                        </Row>
                                        <Row >
                                        <Col style={{textAlign: 'center'}} >
                                            <Button onClick={this.getSelectList}>查询</Button>
                                            <Button type="primary" onClick={this.handleReset} style={{marginLeft: '20px'}}>清屏</Button>
                                        </Col>
                                    </Row>
                                </Col>
                            </Card>
                        </Row>
                        <Row>
                        <Col  >
                            <Form >
                            <Card
                                    size="small"
                                >
                                    <div  style={{height: store.getState().tableHeight.y +94 + 'px'}}>
                                        <Table
                                            columns={this.state.columns?this.state.columns:this.columns}
                                            dataSource={this.state.selectList}
                                            pagination={false}
                                            bordered
                                            style={{height: store.getState().tableHeight.y +61 + 'px'}}
                                            scroll={{ y: store.getState().tableHeight.y +10 }}
                                            rowKey={record => record.key}
                                            onRow={this.onClickRow}
                                            rowClassName={this.setRowClassName}
                                        />
                                        <CommonPagination size='small' current={this.state.page} total={this.state.total} search= 'search'
                                                          onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.selectList}
                                                          loading={this.state.loading} onChange={this.onChange.bind(this)} />
                                    </div>
                                </Card>
                            </Form>
                            
                        </Col>
                        </Row>
                    </Col>
                    <Col  span={18}>
                            
                                
                    <div  style={{height: store.getState().tableHeight.y +200 + 'px'}}>
                                <Reports raqName={this.state.reportName} params={this.state.params} />
                                </div>
                            
                    </Col >
                    </Row>
                    
                </div>
            )
        }
    }
);
export default InventoryProfitAndLossDetails;