import React,{Component} from "react";
import {Button, Form, Input, message, Popconfirm, Select, Spin, Table, Tabs, Upload} from "antd";
import {ChooseList} from "../../order/orderNew/chooseList";
import $ from "jquery/src/ajax";
import { patentTypeList} from '@/dataDic.js';

import {getPatentType} from "@/tools.js";

import './patent.less';

import PatentDetails from './patentDetails';

const { TabPane } = Tabs;

class List extends Component{
    constructor(props) {
        super(props);
        this.state={
            loading:false,
            columns:[
                {
                    title: "序号",
                    dataIndex: "id",
                    key: "id",
                },
                {
                    title: "专利名称",
                    dataIndex: "name",
                    key: "name",
                },
                {
                    title: "专利类型",
                    dataIndex: "type",
                    key: "type",
                    render: (text) => {
                        return (
                            getPatentType(text)
                        )
                    }
                },
                {
                    title: "申请日",
                    dataIndex: "applyDates",
                    key: "applyDates",
                },
                {
                    title: "权利人",
                    dataIndex: "holders",
                    key: "holders",
                },
                {
                    title: "发明人",
                    dataIndex: "inventor",
                    key: "inventor",
                },
                {
                    title: "收费截止日",
                    dataIndex: "",
                    key: "",
                },
                {
                    title: "年费状态",
                    dataIndex: "annualFeeStatus",
                    key: "annualFeeStatus",
                    render: (text)=>{
                        return (
                            text === 0 ? '无信息' :
                                text === 1 ? '代缴费' : ''
                        )
                    }
                },
                {
                    title: "营销员是否提醒",
                    dataIndex: "salesmanRemind",
                    key: "salesmanRemind",
                    render: (text)=>{
                        return(
                            text === 0 ? '未提醒' :
                                text === 1 ? '已提醒' : ''
                        )
                    }
                },
                {
                    title: "年费/滞纳金/恢复",
                    dataIndex: "delayingAmount",
                    key: "delayingAmount",
                    render:(text,record)=>{
                        return(
                            (record.patentAmount || '无') + '/' + (text || '无') + '/' + (record.recoveryAmount || '无')
                        )
                    }
                },
                {
                    title: "客户名称",
                    dataIndex: "companyName",
                    key: "companyName",
                },
                {
                    title: "项目名称",
                    dataIndex: "projectName",
                    key: "projectName",
                },
                {
                    title: "项目编号",
                    dataIndex: "tid",
                    key: "tid",
                },
                {
                    title: "营销员",
                    dataIndex: "salesmanName",
                    key: "salesmanName",
                },
                {
                    title: "咨询师",
                    dataIndex: "techName",
                    key: "techName",
                },
                {
                    title: "派单部门",
                    dataIndex: "depName",
                    key: "depName",
                },
                {
                    title: "操作",
                    dataIndex: "id",
                    key: "caozuo",
                    render: (text,record) => {
                        return (
                            <div>
                                <Button type="primary" style={{marginRight:"15px"}}>发邮件</Button>
                                <Button type="primary">发通知</Button>
                            </div>
                        )
                    }
                },
            ],
            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:false,
            detailsInfor:{},
            departmentArr:[],
            selectedRowKeys:[],

            name:undefined,
            type:undefined,
            depName:undefined,
            annualFeeStatus:undefined,
            userName:undefined,
            salesmanRemind:undefined,
            aname:undefined,

            upLoad: {
                customRequest: (options) => {
                    this.setState({
                        upLoadFileLoading: true,
                    })
                    let params = new FormData();
                    params.append("file", options.file);
                    $.ajax({
                        method: "post",
                        url: globalConfig.context + "/api/admin/patentNew/import",
                        async: true,
                        cache: false,
                        contentType: false,
                        processData: false,
                        data:params
                    }).done(
                        function (data) {
                            this.setState({
                                upLoadFileLoading: false,
                            })
                            if (data.error.length === 0) {
                                message.success("导入成功！");
                                this.loadData();
                            } else {
                                message.warning(data.error[0].message);
                                this.loadData();
                            }
                        }.bind(this)
                    ).always(
                        function () {
                            this.loadData();
                            this.setState({
                                upLoadFileLoading: false,
                            });
                        }.bind(this)
                    );
                },
                name: "file",
                action: globalConfig.context + "/api/admin/patentNew/import",
            }
        }
        this.changeList = this.changeList.bind(this);
        this.loadData = this.loadData.bind(this);
        this.download = this.download.bind(this);
        this.departmentList = this.departmentList.bind(this);
        this.exportExec = this.exportExec.bind(this);
        this.onReset = this.onReset.bind(this);
    }

    componentDidMount() {
        this.loadData();
        this.departmentList();
    }

    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,
            selectedRowKeys:[],
        });
        let data = {
            name:this.state.name,
            type:this.state.type,
            depName:this.state.depName,
            annualFeeStatus:this.state.annualFeeStatus,
            userName:this.state.userName,
            salesmanRemind:this.state.salesmanRemind,
            aname:this.state.aname,
            pageNo:pageNo || 1,
            pageSize:10,
        }
        for(let i in data){
            if(data[i] === undefined){delete data[i]}
        }
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/patentNew/selectPatentNew",
            data,
            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";
    }

    downloadFile() {
        window.location.href =
            globalConfig.context +
            "/api/admin/patentNewexportTemplate?" +
            "sign=new_patent_template";
    }

    //部门
    departmentList() {
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/organization/selectSuperId",
            data: {},
            success: function(data) {
                if(data.error.length === 0){
                    let theArr=[];
                    data.data.map(function(item,index){
                        theArr.push({
                            key:index,
                            name:item.name,
                            id:item.id,
                        })
                    })
                    this.setState({
                        departmentArr:theArr,
                    })
                }else{
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).always(function() {
        }.bind(this));
    }

    exportExec(){
        let loading = message.loading('加载中...');
        let data = {
            name:this.state.name,
            type:this.state.type,
            depName:this.state.depName,
            annualFeeStatus:this.state.annualFeeStatus,
            userName:this.state.userName,
            salesmanRemind:this.state.salesmanRemind,
            aname:this.state.aname,
        }
        for(let i in data){
            if(data[i] === undefined){delete data[i]}
        }
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/patentNew/export",
            data,
            success: function(data) {
                if(data.error.length === 0){
                    loading();
                    this.download(data.data.data);
                }else{
                    loading();
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).always(function() {
        }.bind(this));
    }

    download(fileName){
        window.location.href = globalConfig.context + "/open/download?fileName=" + fileName
    }

    onReset(){
        this.setState({
            name:undefined,
            type:undefined,
            depName:undefined,
            annualFeeStatus:undefined,
            userName:undefined,
            salesmanRemind:undefined,
            aname:undefined,
        },()=>{this.loadData();})
    }

    render() {
        return (
            <div className="user-content">
                <div className="content-title">
                    <span>专利列表</span>
                </div>
                <Tabs defaultActiveKey="1" className="test">
                    <TabPane tab="搜索" key="1">
                        <div style={{padding:'10px'}}>
                            <Form
                                layout="inline"
                                name="basic"
                            >
                                <div style={{display:'flex',alignItems:'center'}}>
                                    <Form.Item>
                                        <Input style={{ width: '130px' }} value={this.state.name} onChange={(e)=>{this.setState({name:e.target.value})}} placeholder="请输入专利名称"/>
                                    </Form.Item>
                                    <Form.Item>
                                        <Input style={{ width: '130px' }} value={this.state.aname} onChange={(e)=>{this.setState({aname:e.target.value})}} placeholder="请输入跟进人名称"/>
                                    </Form.Item>
                                    <Form.Item>
                                        <Input style={{ width: '130px' }} value={this.state.userName} onChange={(e)=>{this.setState({userName:e.target.value})}} placeholder="请输入客户名称"/>
                                    </Form.Item>
                                    <Form.Item>
                                        <Select
                                            placeholder="请选择专利类型"
                                            style={{ width: "140px" }}
                                            value={this.state.type}
                                            onSelect={(e)=>{
                                                this.setState({
                                                    type:e
                                                })
                                            }}
                                        >
                                            {patentTypeList.map(function(item) {
                                                return (
                                                    <Select.Option key={item.value}>
                                                        {item.key}
                                                    </Select.Option>
                                                );
                                            })}
                                        </Select>
                                    </Form.Item>
                                    <Form.Item>
                                        <Select placeholder="请选择是否代缴费" onSelect={(e)=>{
                                            this.setState({
                                                annualFeeStatus:e
                                            })
                                        }} value={this.state.annualFeeStatus} style={{ width: "140px" }}>
                                            <Select.Option value={0} >未代缴</Select.Option>
                                            <Select.Option value={1} >已代缴</Select.Option>
                                        </Select>
                                    </Form.Item>
                                    <Form.Item>
                                        <Select placeholder="请选择是否通知" value={this.state.salesmanRemind} onSelect={(e)=>{
                                            this.setState({
                                                salesmanRemind:e
                                            })
                                        }} style={{ width: "140px" }}>
                                            <Select.Option value={0} >未通知</Select.Option>
                                            <Select.Option value={1} >已通知</Select.Option>
                                        </Select>
                                    </Form.Item>
                                    <Form.Item>
                                        <Select placeholder="请选择派单部门" value={this.state.depName} onSelect={e=>{this.setState({depName:e})}} style={{ width: "240px" }}>
                                            {
                                                this.state.departmentArr.map(function (item) {
                                                    return <Select.Option key={item.name} value={item.name}>{item.name}</Select.Option>
                                                })
                                            }
                                        </Select>
                                    </Form.Item>
                                    <Form.Item>
                                        <Button type="primary" onClick={()=>{this.loadData();}}>
                                            搜索
                                        </Button>
                                    </Form.Item>
                                    <Form.Item>
                                        <Button onClick={this.onReset}>
                                            重置
                                        </Button>
                                    </Form.Item>
                                </div>
                            </Form>
                        </div>
                    </TabPane>
                    <TabPane tab="操作" key="2">
                        <Popconfirm placement="top" title="确定要标识为已通知吗?" onConfirm={(e)=>{
                            e.stopPropagation();

                        }} okText="确定" cancelText="取消">
                            <Button
                                type="primary"
                                style={{ margin: "11px 0px 10px 10px" }}
                                disabled={this.state.selectedRowKeys.length === 0}
                            >
                                标识为已通知
                            </Button>
                        </Popconfirm>
                        <Upload {...this.state.upLoad} disabled={this.state.upLoadFileLoading}>
                            <Button
                                loading={this.state.upLoadFileLoading}
                                type="primary"
                                style={{ marginRight: "10px",marginLeft:'20px' }}
                            >
                                批量导入
                            </Button>
                        </Upload>
                        <Button
                            type="primary"
                            onClick={(e) => {
                                e.stopPropagation();
                                this.downloadFile();
                            }}
                            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;
