import React,{Component} from "react";
import {AutoComplete, Button, DatePicker, Input, message, Select, Spin, Table, Tabs} from "antd";
import {
    ShowModal,
} from "@/tools";
import {ChooseList} from "../../manageCenter/order/orderNew/chooseList";
import $ from "jquery/src/ajax";
import moment from "moment";

const {TabPane} = Tabs;
const { RangePicker } = DatePicker;

class TabelContent extends Component{
    constructor(props) {
        super(props);
        this.state={
            changeList:[],
            dataSource:[],
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function(page) {
                    this.loadData(page);
                }.bind(this),
                showTotal: function(total) {
                    return "共" + total + "条数据";
                }
            },
            searchValues:{},
            contactsOption:[]
        }
        this.loadData= this.loadData.bind(this);
        this.exportExec= this.exportExec.bind(this);
        this.selectSuperId= this.selectSuperId.bind(this);
    }

    loadData(pageNo) {
        this.setState({
            loading:true
        })
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + this.props.tabelApi,
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
            },
            success: function(data) {
                ShowModal(this);
                this.setState({
                    loading:false
                })
                if (data.error && 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({
                        dataSource: this.props.dataProcessing ? this.props.dataProcessing(data) : data.data.list,
                        pagination: this.state.pagination,
                        pageNo:data.data.pageNo
                    });
                }else{
                    message.warning(data.error[0].message);
                }
            }.bind(this)
        }).always(
            function() {
                this.setState({
                    loading: false
                });
            }.bind(this)
        );
    }

    exportExec(){
        let data = {
            clockStart:this.state.releaseDate[0],
            clockEnd:this.state.releaseDate[1],
            createStart:this.state.createReleaseDate[0],
            createEnd:this.state.createReleaseDate[1],
            depId:this.state.superId,
            aid:this.state.theTypes
        };
        for(let i of Object.keys(data)){
            if(!data[i]){
                delete data[i]
            }
        }
        window.location.href =
            globalConfig.context +
            this.props.exportApi + "?" +
            $.param(data);
    }

    //获取上级组织
    selectSuperId() {
        this.setState({
            departmentLoading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/organization/selectSuperId",
            data:{},
            success: function (data) {
                let theArr = [];
                if (data.error && data.error.length === 0) {
                    for(let i=0;i<data.data.length;i++){
                        let theData = data.data[i];
                        theArr.push(
                            {
                                value:theData.id,
                                label:theData.name
                            }
                        );
                    };
                    this.setState({
                        contactsOption: theArr,
                    });
                }else{
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).always(function () {
            this.setState({
                departmentLoading: false
            });
        }.bind(this));
    }

    componentDidMount() {
        this.loadData();
        if(this.props.searchList){
            let arr = this.props.searchList.filter(v=>v.type === 'departmentSelect')
            let arr1 = this.props.searchList.filter(v=>v.defaultValue)
            if(arr.length > 0){
                this.selectSuperId();
            }
            if(arr1.length > 0){
                for(let i of arr1){
                    this.state.searchValues[i.dataKey] = i.defaultValue
                }
            }
        }
    }

    render() {
        const { searchValues } = this.state;
        return (
            <div>
                <Tabs defaultActiveKey="1" className="test">
                    <TabPane tab="搜索" key="1">
                        <div className="user-search" style={{
                            marginTop:'10px',
                            marginLeft:'10px',
                            marginRight:'10px',
                            display:'flex',
                            alignItems:'center'
                        }}>
                            {
                                (this.props.searchList || []).map((v,k)=>(
                                    v.type === 'text' ?
                                        <span>
                                            <Input
                                                key={k}
                                                placeholder={v.placeholder}
                                                value={v.value ? v.value : (v.defaultValue || undefined)}
                                                style={v.style ? v.style : { width: 150, marginRight: 10}}
                                                onChange={(e) => {
                                                    searchValues[v.dataKey] = e.target.value
                                                    this.setState({
                                                        searchValues:  searchValues
                                                    });
                                                }}
                                            />
                                        </span> :
                                        v.type === 'select' || v.type === 'departmentSelect' ?
                                            <span style={{paddingBottom:'9px'}}>
                                                <Spin spinning={this.state.departmentLoading}>
                                                    {
                                                        <Select
                                                            key={k}
                                                            placeholder={v.placeholder}
                                                            style={v.style ? v.style : { width:200,marginRight: 10}}
                                                            value={searchValues[v.dataKey] ? searchValues[v.dataKey] : (v.defaultValue || undefined)}
                                                            onChange={(e) => {
                                                                searchValues[v.dataKey] = String(e);
                                                                this.setState({
                                                                    searchValues:  searchValues
                                                                });
                                                            }}>
                                                            {
                                                                v.selectOptionList ?
                                                                    v.selectOptionList:
                                                                    (
                                                                        v.type === 'departmentSelect' ?
                                                                            this.state.contactsOption :
                                                                            (v.selectList || [])
                                                                    ).map((v)=>(
                                                                        <Select.Option
                                                                            key={v.value}
                                                                            value={v.value}
                                                                        >
                                                                            {v.label}
                                                                        </Select.Option>
                                                                    ))
                                                            }
                                                        </Select>
                                                    }
                                                </Spin>
                                            </span>: v.type === 'times' ?
                                                <span style={{display:'flex'}}>
                                                    <div style={{marginRight:'10px',marginBottom:'10px'}}>{v.title}</div>
                                                    <span>
                                                        <RangePicker
                                                            style={{marginRight:'10px',marginBottom:'10px'}}
                                                            value={[
                                                                searchValues[v.dataKey[0]]
                                                                    ? moment(searchValues[v.dataKey[0]])
                                                                    : null,
                                                                searchValues[v.dataKey[1]]
                                                                    ? moment(searchValues[v.dataKey[1]])
                                                                    : null,
                                                            ]}
                                                            onChange={(data, dataString) => {
                                                                searchValues[v.dataKey[0]] = String(dataString[0]);
                                                                searchValues[v.dataKey[1]] = String(dataString[1]);
                                                                this.setState({
                                                                    searchValues:  searchValues
                                                                });
                                                            }}
                                                        />
                                                    </span>
                                                </span>:
                                            null
                                ))
                            }
                            <Button type="primary" onClick={()=>{
                                this.loadData();
                            }} style={{marginRight:'10px',marginBottom:'10px'}}>搜索</Button>
                            <Button onClick={this.resetAll} style={{marginRight:'10px',marginBottom:'10px'}}>重置</Button>
                        </div>
                    </TabPane>
                    <TabPane tab="更改表格显示数据" key="2">
                        <div style={{ marginLeft: 10 }}>
                            <ChooseList
                                columns={this.props.columns}
                                changeFn={this.changeList}
                                changeList={this.state.changeList}
                                top={55}
                                margin={11}
                            />
                        </div>
                    </TabPane>
                    {this.props.exportApi && <TabPane tab="导出" key="3">
                        <div style={{ float: "left" }}>
                            <Button onClick={this.exportExec} style={{ margin: 10 }}>导出excel</Button>
                        </div>
                    </TabPane>}
                </Tabs>
                <div className="patent-table">
                    <Spin spinning={this.state.loading}>
                        <Table
                            bordered
                            size="middle"
                            scroll={{ x: 800, y: 0 }}
                            columns={
                                this.state.changeList.length > 0
                                    ? this.state.changeList
                                    : this.props.columns
                            }
                            dataSource={this.state.dataSource}
                            pagination={this.state.pagination}
                        />
                    </Spin>
                </div>
            </div>
        )
    }
}

export default TabelContent;
