// 招标名称维护
import React from 'react';
import { Card, Row, Col, Form, DatePicker, Select, Button, Checkbox, Table, Switch, message, Pagination, Input  } from 'antd';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
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 BidNameMaintenance = Form.create()(
    class extends React.Component {
        constructor() {
            super();
            this.state = {
                userInfo: {}, //表单数据详情
                BidNameListData: [], //招标名称列表数据
                columns:[],
                detailWidthTotal:'',
                pageSize: 10,
                page: 1,
                total:0,
                rowKey: '', //行选中id
                rowData: {}, //当前行数据
                language: JSON.parse(window.sessionStorage.userData),
                ChineseEnglishSize: [],
            }
            // 库存单的数据表头
            this.columns = [

                {
                    key: 1,
                    title: '代码',
                    dataIndex: 'cCode',
                    align: "center",
                    width: '20%'
                },
                {
                    key: 2,
                    title: '描述',
                    dataIndex: 'cDesc',
                    align: "center",
                    width: '40%'
                },
                {
                    key: 3,
                    title: '状态',
                    dataIndex: 'cState',
                    align: "center",
                    width: '20%'
                }
            ]
        }

        componentDidMount() {
            // 初始化数据
            this.getBidNameList();
            this.getDetailTableColumns();
        }

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

        //清空表格内容
        resetFields = () => {
            this.props.form.resetFields();
            this.setState({userInfo: {}, rowKey: '', rowData: ''});
        };
        handleReset=()=> {
            this.resetFields();
            this.setState({BidNameListData: [],total: 0});
        }
        //导入数据
        importData=()=>{
            
        }
        searchInformation=()=>{
            this.setState({page: 1},()=>{
                this.getBidNameList()
            })
        };
        // 获取招标名称列表数据
        getBidNameList=()=>{
            let cCode = this.props.form.getFieldValue('cCode');
            let cDesc = this.props.form.getFieldValue('cDesc');
            let cState = this.props.form.getFieldValue('cState');
            this.setState({loading: true})
            let data = {
                params: [
                    {
                      cCode : cCode ? cCode :undefined,
                      cDesc : cDesc ? cDesc :undefined,
                      cState : cState ? cState :undefined,
                    }
                ],
                pagination: [
                    {
                        pageSize:this.state.pageSize,
                        currentPage:this.state.page,
                        sort:"ID",
                        dir:"asc",
                    }
                ],
            };
            $http.post('urlS', this, {
                code: "02020280",
                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({ BidNameListData: res.result.rows, total: res.result.total, loading: false});
                    }else {
                        this.setState({ BidNameListData: [],total:0,loading: false});
                        if (language.language =='EN'){
                            message.error("No related data was found");
                        }else{
                            message.error("没数据");
                        }
                        this.props.form.resetFields();
                    }
                }
            });
        };

        //保存
        saveData=()=>{
            this.props.form.validateFields((error, value)=> {
                let obj = JSON.parse(JSON.stringify(value));
                if(error == null){
                    let data = {
                        params: [
                            {
                                cID:this.state.userInfo.cID,	//盘点组ID
                                cCode:obj.code ,	//代码
                                cDesc:obj.descripts ,	//描述
                                cENDesc: obj.ENDescripts,
                                cSeqNo:obj.seqNo ? obj.seqNo :'',//序号
                                cStartDate:Moment(obj.startDate).format('YYYY-MM-DD'),	//生效日期
                                cEndDate:obj.endDate ? Moment(obj.endDate).format('YYYY-MM-DD') : '',	//失效日期
                            }
                        ]
                    };
                    $http.post('urlS', this, {
                        code: "02020285",
                        data: data,
                        success: function (res) {
                            if(res.errorCode == 0) {
                                message.success(res.errorMessage);
                                this.getBidNameList();
                            }
                        }
                    });
                }
            })
        }
        // 设置行属性
        onClickRow = (record) => {
            return {
                // 单击行选中
                onClick: (e) => {
                    e.stopPropagation();
                    this.resetFields();
                    if (this.state.rowKey === '') {
                        // 保存行数据以及ID
                        this.setState({rowKey: record.ID});
                        this.BidNameData(record.ID)
                    } else {
                        if (this.state.rowKey !== record.ID) {
                            this.setState({rowKey: record.ID});
                            this.BidNameData(record.ID)
                        } else {
                            this.setState({rowKey: '', userInfo: {}});
                            this.resetFields();
                        }
                    }
                }
            }
        };

        // 表格行的类名
        setRowClassName = (record) => {
            return record.ID === this.state.rowKey ? 'clickRowStyle' : '';
        };

        BidNameData=(id)=>{

            let data = {
                params: [
                    {
                        cID: id
                    }
                ]
            };
            $http.post('urlS', this, {
                code: "02020284",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                     
                     this.setState({userInfo: res.result})
                    }
                }
            })

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

        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: ['BidNameMaintenance'],
                    compontName: 'BidNameMaintenance',
                    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 >{language.language =='EN' ? item.enDescripts : item.title}</div>,
                            }
                        }) : '';
                        
                        this.setState({columns: arr,ChineseEnglishSize: language.language =='EN' ? res.result.language.EN : res.result.language.CN})
                        
                        // 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="BidNameMaintenance">
                    <Col span={12}>
                        <Row>
                            <Card
                                size="small"
                                title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.BidNameMaintenancebidTitle ? ChineseEnglishSize.BidNameMaintenancebidTitle.descripts:'招标名称列表'}</div>}
                            >
                                <Col span={24}>
                                    <Row >
                                        <Col span={6}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.BidNameMaintenancefcode ? ChineseEnglishSize.BidNameMaintenancefcode.descripts:'代码'}>
                                                {getFieldDecorator('cCode',{
                                                    initialValue: undefined
                                                })(
                                                    <Input />
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={6}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.BidNameMaintenancefdes ? ChineseEnglishSize.BidNameMaintenancefdes.descripts:'描述'}>
                                                {getFieldDecorator('cDesc',{
                                                    initialValue: undefined
                                                })(
                                                    <Input />
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col span={6}>
                                            <FormItem {...formItemLayout} label={ChineseEnglishSize.BidNameMaintenancefstate ? ChineseEnglishSize.BidNameMaintenancefstate.descripts:'状态'}>
                                                {getFieldDecorator('cState',{
                                                    initialValue: undefined
                                                })(
                                                    <Select allowClear style={{width: '100%'}}>
                                                        <Option value="1" key="1" >{ChineseEnglishSize.BidNameMaintenancefEffective ? ChineseEnglishSize.BidNameMaintenancefEffective.descripts:'有效'}</Option>
                                                        <Option value="0" key="0" >{ChineseEnglishSize.BidNameMaintenancefInvalid ? ChineseEnglishSize.BidNameMaintenancefInvalid.descripts:'失效'}</Option>
                                                    </Select>
                                                ) }
                                            </FormItem>
                                        </Col>
                                        <Col style={{textAlign: 'center'}} span={6}>
                                            <Button onClick={this.searchInformation}>{ChineseEnglishSize.BidNameMaintenanceQuery ? ChineseEnglishSize.BidNameMaintenanceQuery.descripts:'查询'}</Button>
                                            <Button type="primary" onClick={this.importData} style={{marginLeft: '20px'}}>{ChineseEnglishSize.BidNameMaintenanceclean ? ChineseEnglishSize.BidNameMaintenanceclean.descripts:'清屏'}</Button>
                                        </Col>
                                    </Row>
                                </Col>
                            </Card>
                        </Row>
                        <Row>
                            <Col>
                                <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.BidNameListData}
                                            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.BidNameListData}
                                                          loading={this.state.loading} onChange={this.onChange.bind(this)} />
                                    </div>
                                </Card>
                            </Col>
                        </Row>
                    </Col>
                    <Col span={12}>
                        <Col >
                            
                            <Form>
                                <Card
                                    title={<div className="card-title-left-icon"><div></div>{ChineseEnglishSize.BidNameMaintenanceinfo ? ChineseEnglishSize.BidNameMaintenanceinfo.descripts:'招标名称信息'}</div>}
                                    size="small"
                                >
                                    <div  style={{height: store.getState().tableHeight.y +129 + 'px'}}>
                                    <Col >
                                        <Row>
                                            <Col span={11}>
                                                <FormItem label={ChineseEnglishSize.BidNameMaintenancebidCode ? ChineseEnglishSize.BidNameMaintenancebidCode.descripts:'招标名称代码：'} {...formItemItem}>
                                                    {getFieldDecorator('code', {
                                                        initialValue: userInfo.cCode ? userInfo.cCode : undefined,
                                                        rules:[{ required : ChineseEnglishSize.BidNameMaintenancecode ? ChineseEnglishSize.BidNameMaintenancecode.required =='Y'? true:false: false, message: ChineseEnglishSize.BidNameMaintenanceinputBidNameCode ? ChineseEnglishSize.BidNameMaintenanceinputBidNameCode.message:'请输入招标名称代码'}]
                                                    })(
                                                        <Input style={{width: '100%'}} />
                                                    )}
                                                </FormItem>
                                            </Col>
                                            <Col span={11}>
                                                <FormItem label={ChineseEnglishSize.BidNameMaintenancebidSepNo ? ChineseEnglishSize.BidNameMaintenancebidSepNo.descripts:'招标名称序号：'} {...formItemItem}>
                                                    {getFieldDecorator('seqNo', {
                                                        initialValue: userInfo.cSeqNo ? userInfo.cSeqNo : undefined,
                                                    })(
                                                        <Input style={{width: '100%'}} type="number" />
                                                    )}
                                                </FormItem>
                                            </Col>
                                        </Row>
                                        <Row>
                                            <Col span={11}>
                                                <FormItem label={ChineseEnglishSize.BidNameMaintenancebidDes ? ChineseEnglishSize.BidNameMaintenancebidDes.descripts:'招标名称描述：'} {...formItemItem}>
                                                    {getFieldDecorator('descripts', {
                                                        initialValue: userInfo.cDesc ? userInfo.cDesc : undefined,
                                                        rules:[{ required : ChineseEnglishSize.BidNameMaintenancedesc ? ChineseEnglishSize.BidNameMaintenancedesc.required =='Y'? true:false: false, message: ChineseEnglishSize.BidNameMaintenanceinputBidNameDES ? ChineseEnglishSize.BidNameMaintenanceinputBidNameDES.message:'请输入招标名称描述'}] 
                                                    })(
                                                        <Input style={{width: '100%'}} />
                                                    )}
                                                </FormItem>
                                            </Col>
                                            <Col span={11}>
                                            <FormItem label={ChineseEnglishSize.BidNameMaintenanceenglishDes ? ChineseEnglishSize.BidNameMaintenanceenglishDes.descripts:'英文描述：'} {...formItemItem}>
                                                {getFieldDecorator('ENDescripts', {
                                                    initialValue:  userInfo.cENDesc ? userInfo.cENDesc : undefined,
                                                })(
                                                    <Input style={{width: '100%'}} />
                                                )}
                                            </FormItem>
                                            </Col>
                                        </Row>
                                        <Row>
                                            <Col span={11}>
                                            <FormItem label={ChineseEnglishSize.BidNameMaintenancestartDate ? ChineseEnglishSize.BidNameMaintenancestartDate.descripts:'生效日期：'} {...formItemItem}>
                                                {getFieldDecorator('startDate', {
                                                    initialValue:  userInfo.cStartDate ? Moment(userInfo.cStartDate) : undefined,
                                                    rules:[{ required : ChineseEnglishSize.BidNameMaintenancestartDate ? ChineseEnglishSize.BidNameMaintenancestartDate.required =='Y'? true:false: false, message: ChineseEnglishSize.BidNameMaintenancestartDate ? ChineseEnglishSize.BidNameMaintenancestartDate.message:'请选择生效日期'}]
                                                })(
                                                    <DatePicker
                                                        format="YYYY-MM-DD"
                                                        style={{width:'100%'}}
                                                    >
                                                    </DatePicker>
                                                )}
                                            </FormItem>
                                            </Col>
                                            <Col span={11}>
                                            <FormItem label={ChineseEnglishSize.BidNameMaintenanceendDate ? ChineseEnglishSize.BidNameMaintenanceendDate.descripts:'失效日期：'} {...formItemItem}>
                                                {getFieldDecorator('endDate', {
                                                    initialValue:  userInfo.cEndDate ? Moment(userInfo.cEndDate) : undefined,
                                                })(
                                                    <DatePicker
                                                        format="YYYY-MM-DD"
                                                        style={{width:'100%'}}
                                                    >
                                                    </DatePicker>
                                                )}
                                            </FormItem>
                                            </Col>
                                        </Row>
                                    </Col>
                                    </div>
                                </Card>
                            </Form>
                            
                        </Col>
                        <Col>
                            <Row style={{marginTop: '10px',textAlign: 'right'}} >
                                <Button type="primary" style={{marginRight: '30px'}} onClick={this.saveData}>{ChineseEnglishSize.BidNameMaintenancesave ? ChineseEnglishSize.BidNameMaintenancesave.descripts:'保存'}</Button>
                                <Button onClick={this.resetFields}>{ChineseEnglishSize.BidNameMaintenancereset ? ChineseEnglishSize.BidNameMaintenancereset.descripts:'重置'}</Button>
                            </Row>
                        </Col>
                    </Col>
                </div>
            )
        }
    }
);
export default BidNameMaintenance;