import React,{Component} from "react";
import {Button, message, Spin, Table, Tabs} from "antd";
import {ChooseList} from "../../order/orderNew/chooseList";
import $ from "jquery/src/ajax";

import PatentDetails from './patentDetails';

const { TabPane } = Tabs;

class List extends Component{
    constructor(props) {
        super(props);
        this.state={
            loading:false,
            columns:[
                {
                    title: "序号",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "专利名称",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "专利类型",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "申请日",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "权利人",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "发明人",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "收费截止日",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "年费状态",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "营销员是否提醒",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "年费/滞纳金/恢复",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "操作",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "客户名称",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "项目名称",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "项目编号",
                    dataIndex: "companyName",
                    key: "companyName",
                },
            ],
            dataSource:[],
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadData(page,this.state.searchInfor);
                }.bind(this),
                showTotal: function (total) {
                    return "共" + total + "条数据";
                },
            },
            detailsVisible:true,
            detailsInfor:{}
        }
        this.changeList = this.changeList.bind(this);
        this.loadData = this.loadData.bind(this);
        this.download = this.download.bind(this);
    }

    componentDidMount() {
        this.loadData();
    }

    changeList(arr) {
        const newArr = [];
        this.state.columns.forEach(item => {
            arr.forEach(val => {
                if (val === item.title) {
                    newArr.push(item);
                }
            });
        });
        this.setState({
            changeList: newArr
        });
    }

    loadData(pageNo) {
        this.setState({
            loading: true,
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/company/selectPaymentList",
            data: {
                pageNo:pageNo || 1,
                pageSize:10,
            },
            success: function (data) {
                if(data.error.length === 0){
                    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({
                        page: data.data.pageNo,
                        dataSource: data.data.list,
                        pagination: this.state.pagination,
                    });
                }else{
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).always(
            function () {
                this.setState({
                    loading: false,
                });
            }.bind(this)
        );
    }

    download() {
        window.location.href =
            globalConfig.context +
            "/api/user/channel/downloadTemplate?" +
            "sign=user_channel";
    }

    render() {
        return (
            <div className="user-content">
                <div className="content-title">
                    <span>专利列表</span>
                </div>
                <Tabs defaultActiveKey="1" className="test">
                    <TabPane tab="搜索" key="1">
                        <div style={{paddingTop:'10px'}}>

                        </div>
                    </TabPane>
                    <TabPane tab="操作" key="2">
                        <Button
                            type="primary"
                            onClick={this.exportExec}
                            style={{ margin: "11px 0px 10px 10px" }}
                        >
                            标识为已通知
                        </Button>
                        <Button
                            type="primary"
                            onClick={this.exportExec}
                            style={{ margin: "11px 0px 10px 10px" }}
                        >
                            批量导入
                        </Button>
                        <Button
                            type="primary"
                            onClick={(e) => {
                                e.stopPropagation();
                                this.download();
                            }}
                            style={{ margin: "11px 0px 10px 10px" }}
                        >
                            下载专利模板
                        </Button>
                    </TabPane>
                    <TabPane tab="更改表格显示数据" key="3">
                        <div style={{ marginLeft: 10 }}>
                            <ChooseList
                                columns={this.state.columns}
                                changeFn={this.changeList}
                                changeList={this.state.changeList}
                                top={55}
                                margin={11}
                            />
                        </div>
                    </TabPane>
                </Tabs>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table
                            columns={
                                this.state.changeList
                                    ? this.state.changeList
                                    : this.state.columns
                            }
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                            style={{
                                cursor: 'pointer',
                            }}
                            onRowClick={(record)=>{
                                this.setState({
                                    detailsVisible:true,
                                    detailsInfor:record
                                })
                            }}
                            rowSelection={{
                                selectedRowKeys:this.state.selectedRowKeys,
                                onChange: (selectedRowKeys)=>{
                                    this.setState({ selectedRowKeys });
                                },
                            }}
                            scroll={{ x: "max-content", y: 0 }}
                            bordered
                            size="small"
                        />
                    </Spin>
                </div>
                {this.state.detailsVisible ? <PatentDetails
                    visible={this.state.detailsVisible}
                    detailsInfor={this.state.detailsInfor}
                    onCancel={()=>{
                        this.setState({
                            detailsVisible:false,
                            detailsInfor:{}
                        })
                    }}
                /> : null}
            </div>
        )
    }
}


export default List;
