import {Component} from 'react';
import { Icon, Button, Input, Select, Spin, Table, Switch, message, DatePicker, Modal, Form } from 'antd';

class Tags extends Component {
    constructor(props) {
        super(props);
        this.state = {
            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: 'name',
                    key: 'name',
                },  {
                    title: '创建人',
                    dataIndex: 'createName',
                    key: 'createName',
                    render:(text)=>{
                        return '管理员'
                    }
                },
                {
                    title: '创建时间',
                    dataIndex: 'createTime',
                    key: 'createTime'
                }
            ],
            dataSource: []
          }
          this.handleCancel=this.handleCancel.bind(this)
    }
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            selectedRowKeys:[],
            page:pageNo,
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/jtBusiness/tag/list",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                name: this.state.name, //标签名称
            },
            success: function (data) {
                let theArr = [];
                if (!data.data || !data.data.list) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    for (let i = 0; i < data.data.list.length; i++) {
                        let thisdata = data.data.list[i];
                        theArr.push({
                            key: i,
                            id: thisdata.id,
                            name: thisdata.name,
                            createTime:thisdata.createTime?(new Date(thisdata.createTime)).toLocaleString():'',
                        });
                    };
                };
                this.state.pagination.current = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                if(data.data&&data.data.list&&!data.data.list.length){
					this.state.pagination.current=0
					this.state.pagination.total=0
				};
                this.setState({
                    dataSource: theArr,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    }
    handOk(){
        this.setState({
            visible:false
        })
        this.loadData(this.state.page)
    }
    handleCancel(){
        this.setState({
            visible:false
        })
    }
    search(){
        this.loadData();
    }
    reset(){
        this.state.name='';
        this.loadData();
    }
    addClick(){
        this.state.tagName='';
        this.setState({
            visible:true
        })
    }
    del(){
        this.setState({
            loading:true
        })
        let deletedIds =[];
        let rowItem = this.state.selectedRowKeys[0];
        let data = this.state.dataSource ||[];
        if (data.length) {
            deletedIds.push(data[rowItem].id);
        }
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/jtBusiness/tag/delete",
            data: {
              id:deletedIds[0]
            },
            success: function (data) {
                let theArr = [];
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                    return
                };
                message.success('删除成功!');  
                this.loadData(this.state.page);
            }.bind(this)
        }).always(function(){
            this.setState({
                loading:false
            })
        }.bind(this))
    }
    //新建
    handleSubmit(e){
        e.preventDefault();
        if(!this.state.tagName){
            message.warning('请填写标签名称!');
            return;
        }
        this.setState({
            loading:true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/jtBusiness/tag/apply",
            data: {
              name:this.state.tagName
            },
            success: function (data) {
                let theArr = [];
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                    return;
                };
                message.success('新建成功!');
                this.loadData(this.state.page);
                this.setState({
                    visible:false
                })
            }.bind(this)
        }).always(
            function() {
                this.setState({
                    loading: false
                });
            }.bind(this)
        );
    }
    componentWillMount(){
        this.loadData();
    }
    render() { 
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows.slice(-1),
                    selectedRowKeys: selectedRowKeys.slice(-1)
                });
            }
        };
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
		};
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            <div className="user-content" >
            <div className="content-title">
                <span style={{fontSize:20}}>我的标签库</span>
                <div className="patent-addNew">
                    <Button type="primary" onClick={this.addClick.bind(this)}>新增标签<Icon type="plus" /></Button>
                </div>
            </div>
            <div className="user-search">
                <Input placeholder="标签名称"
                    value={this.state.name}
                    onChange={(e) => { this.setState({ name: e.target.value }); }} />
                <Button type="primary" onClick={this.search.bind(this)}>搜索</Button>
                <Button onClick={this.reset.bind(this)}>重置</Button>
                <Button type="danger" onClick={this.del.bind(this)} disabled={!hasSelected}>删除</Button>
            </div>
            <div className="patent-table">
                <Spin spinning={this.state.loading}>
                    <Table columns={this.state.columns}
                        dataSource={this.state.dataSource}
                        rowSelection={rowSelection}
                        pagination={this.state.pagination}
                         />
                </Spin>
            </div>
            <Modal
					maskClosable={false}
					visible={this.state.visible}
					onOk={this.handOk.bind(this)}
					onCancel={this.handleCancel.bind(this)}
					width="400px"
					title="新建标签"
					footer=""
				>
                <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e)}} id="demand-form">
                        <Form.Item  {...formItemLayout} label={<span><strong style={{color:'#f00'}}> * </strong>标签</span>}>
                            <Input value={this.state.tagName} onChange={(e)=>{this.setState({tagName:e.target.value})}} placeholder="标签名称"/>
						</Form.Item>
                        <div className="clearfix">
						<Form.Item wrapperCol={{ span: 12, offset: 6 }}>
						    <Button className="set-submit" type="primary" htmlType="submit">
								保存
							</Button>
							<Button className="set-submit" type="ghost" onClick={this.handleCancel.bind(this)}>
								取消
							</Button>
						</Form.Item>
					</div>
                </Form>
             </Modal>
        </div >
         );
    }
}
 
export default Tags;