import React from 'react';
import { Icon, Button, Input, Spin, Table, message, Radio, Select, Modal } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';

const StandardDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    handleOk() {
        this.setState({
            loading: true,
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/developStandard",
            data: {
                id: this.props.data.id,
                uid:this.props.uid,
                standardName: this.state.standardName,
                standardNumber: this.state.standardNumber,
                standardLevel: this.state.standardLevel,
                participateWay: this.state.participateWay
            },
            success: function (data) {
                if (data.data) {
                    this.setState({
                        id: data.data.id,
                        standardName: data.data.standardName,
                        standardNumber: data.data.standardNumber,
                        standardLevel: data.data.standardLevel,
                        participateWay: data.data.participateWay
                    });
                } else if (data.error.length && data.error[0].message) {
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功！');
                this.setState({
                    loading: false,
                    visible: false
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.props.closeDesc(false,true);
        }.bind(this));
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc;
        if (nextProps.data) {
            this.state.standardName = nextProps.data.standardName;
            this.state.standardNumber = nextProps.data.standardNumber;
            this.state.standardLevel = nextProps.data.standardLevel;
            this.state.participateWay = nextProps.data.participateWay;
        };
    },
    render() {
        if (this.props.data) {
            return (
                <div className="admin-desc">
                    <Modal title="企业参与国家标准或行业标准制定情况明细" visible={this.state.visible} onCancel={this.handleCancel}
                        width='400px' footer={[
                            <Button key="submit" type="primary" size="large" loading={this.state.loading} onClick={this.handleOk}>确认</Button>,
                            <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>
                        ]}
                        className="admin-desc-content">
                        <div>
                            <p>标准名称:</p>
                            <Input defaultValue={this.props.data.standardName} value={this.state.standardName}
                                onChange={(e) => { this.setState({ standardName: e.target.value }); }} />
                        </div>
                        <div>
                            <p>标准编号:</p>
                            <Input value={this.state.standardNumber}
                                onChange={(e) => { this.setState({ standardNumber: e.target.value }); }} />
                        </div>
                        <div>
                            <p>标准级别:</p>
                            <Radio.Group value={this.state.standardLevel} onChange={(e) => { this.setState({ standardLevel: e.target.value }) }}>
                                <Radio value="0">国家</Radio>
                                <Radio value="1">行业</Radio>
                            </Radio.Group>
                        </div>
                        <div>
                            <p>参与方式:</p>
                            <Radio.Group value={this.state.participateWay} onChange={(e) => { this.setState({ participateWay: e.target.value }) }}>
                                <Radio value="0">主持</Radio>
                                <Radio value="1">参与</Radio>
                            </Radio.Group>
                        </div>
                    </Modal>
                </div>
            );
        } else {
            return <div></div>
        }
    },
});

const Standard = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/standard",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                uid:this.props.data.uid,
                standardName: this.state.standardName,
                standardNumber: this.state.standardNumber,
                standardLevel: this.state.standardLevel,
                participateWay: this.state.participateWay
            },
            success: function (data) {
                if (data.error.length || !data.data || !data.data.list) {
                    message.warning(data.error[0].message);
                    return;
                }
                for (let i = 0; i < data.data.list.length; i++) {
                    let thisdata = data.data.list[i];
                    this.state.data.push({
                        key: i + 1,
                        id: thisdata.id,
                        uid: thisdata.uid,
                        standardName: thisdata.standardName,
                        standardNumber: thisdata.standardNumber,
                        standardLevel: String(thisdata.standardLevel),
                        participateWay: String(thisdata.participateWay),
                        createTime: thisdata.createTime,
                        createTimeFormattedDate: thisdata.createTimeFormattedDate
                    });
                };
                this.state.pagination.current = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                this.setState({
                    dataSource: this.state.data,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            selectedRowKeys: [],
            selectedRows: [],
            loading: false,
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            columns: [
                {
                    title: '编号',
                    dataIndex: 'key',
                    key: 'key'
                }, {
                    title: '标准名称',
                    dataIndex: 'standardName',
                    key: 'standardName'
                }, {
                    title: '标准编号',
                    dataIndex: 'standardNumber',
                    key: 'standardNumber'
                }, {
                    title: '标准级别',
                    dataIndex: 'standardLevel',
                    key: 'standardLevel',
                    render: text => {
                        switch (String(text)) {
                            case "0":
                                return "国家";
                            case "1":
                                return "行业";
                        }
                    }
                }, {
                    title: '参与方式',
                    dataIndex: 'participateWay',
                    key: 'participateWay',
                    render: text => {
                        switch (String(text)) {
                            case "0":
                                return "主持";
                            case "1":
                                return "参与";
                        }
                    }
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    delectRow() {
        let deletedIds = [];
        for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
            let rowItem = this.state.selectedRows[idx];
            if (rowItem.id) {
                deletedIds.push(rowItem.id)
            }
        }
        this.setState({
            selectedRowKeys: [],
            loading: deletedIds.length > 0
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/deleteStandard",
            data: {
                ids: deletedIds
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功！');
                this.setState({
                    loading: false,
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.loadData();
        }.bind(this));
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.standardName = undefined;
        this.state.standardNumber = undefined;
        this.state.standardLevel = undefined;
        this.state.participateWay = undefined;
        this.loadData();
    },
    render() {
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows,
                    selectedRowKeys: selectedRowKeys
                });
            }
        }
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            <div className="user-content" >
                <div className="content-title">
                    <span>标准制定情况汇总</span>
                </div>
                <div className="user-search">
                    <Input placeholder="标准名称" value={this.state.standardName}
                        onChange={(e) => { this.setState({ standardName: e.target.value }); }} />
                    <Input placeholder="标准编号" value={this.state.standardNumber}
                        onChange={(e) => { this.setState({ standardNumber: e.target.value }); }} />
                    <Select placeholder="标准级别" style={{ width: 200 }}
                        value={this.state.standardLevel}
                        onChange={(e) => { this.setState({ standardLevel: e }) }}>
                        <Select.Option value="0" >国家</Select.Option>
                        <Select.Option value="1" >行业</Select.Option>
                    </Select>
                    <Select placeholder="参与方式" style={{ width: 200 }}
                        value={this.state.participateWay}
                        onChange={(e) => { this.setState({ participateWay: e }) }}>
                        <Select.Option value="0" >主持</Select.Option>
                        <Select.Option value="1" >参与</Select.Option>
                    </Select>
                    <Button type="primary" onClick={this.search}>搜索</Button>
                    <Button onClick={this.reset}>重置</Button>
                    <p>
                        <Button style={{ background: "#ea0862", border: "none", color: "#fff" }}
                            onClick={this.tableRowClick}>添加<Icon type="plus" /></Button>
                        <Button style={{ background: "#3fcf9e", border: "none", color: "#fff" }}
                            disabled={!hasSelected}
                            onClick={this.delectRow}>删除<Icon type="minus" /></Button>
                    </p>
                </div>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table columns={this.state.columns}
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                            rowSelection={rowSelection}
                            onRowClick={this.tableRowClick} />
                    </Spin>
                </div>
                <StandardDesc uid={this.props.data.uid} data={this.state.RowData} showDesc={this.state.showDesc} closeDesc={this.closeDesc} />
            </div >
        );
    }
});

export default Standard;