import React,{Component} from 'react';
import {Button, Input, message, Select, Spin, Table, Modal, Popconfirm} from "antd";
import AddOfficialFeePrice from './addOfficialFeePrice'
import $ from "jquery/src/ajax";
import '../content.less';

class OfficialFees extends Component{
    constructor(props) {
        super(props);
        this.state={
            type:'',
            dataSource: [],
            columns: [
                {
                    title: '专利类型',
                    dataIndex: 'type',
                    key: 'type',
                    render: (text) => {
                        return text === 0 ? '实用新型' : text === 1 ? '外观专利' : '发明专利'
                    }
                },
                {
                    title: '全额(万元)',
                    dataIndex: 'amount',
                    key: 'amount',
                },
                {
                    title: '减缴85%',
                    dataIndex: 'proportion85',
                    key: 'proportion85',
                },
                {
                    title: '备注',
                    dataIndex: 'remarks',
                    key: 'remarks',
                },
                {
                    title: '操作',
                    dataIndex: 'id',
                    key: 'id',
                    render: (text) => {
                        return <div>
                            <Popconfirm
                                title="是否删除?"
                                onConfirm={() => {
                                    this.deleteOfficialFeePrice(text);
                                }}
                                okText="删除"
                                cancelText="不删除"
                            >
                                <Button type="danger" onClick={(e) => {
                                    e.stopPropagation()
                                }}>
                                    删除
                                </Button>
                            </Popconfirm>
                        </div>
                    }
                },
            ],
            addSoftVisible: false,
            infor: {},
        }
        this.loadData = this.loadData.bind(this);
        this.tableRowClick = this.tableRowClick.bind(this);
        this.reset = this.reset.bind(this);
    }

    componentDidMount() {
        this.loadData();
    }

    //搜索功能和初始列表加载
    loadData() {
        this.setState({
            loading: true,
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/company/listOfficialFeePrice',
            data: {
                type:this.state.type,       //名称
            },
            success: function (data) {
                if (!data.data || !data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    data.data.map((v,i)=>{v.key = i})
                    this.setState({
                        dataSource: data.data,
                    });
                };

            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    }

    tableRowClick(infor) {
        this.setState({
            addSoftVisible : true,
            infor: infor
        })
    }

    reset(){
        this.setState({
            type:'',       //名称
        },()=>{
            this.loadData();
        })
    }

    //删除
    deleteOfficialFeePrice(id) {
        let _this = this;
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url:globalConfig.context + '/api/admin/company/deleteOfficialFeePrice',
            data:{
                id: id
            },
        }).done(function (data) {
            _this.setState({
                loading: false
            });
            if (!data.error.length) {
                message.success('删除成功!');
                _this.loadData();
            } else {
                message.warning(data.error[0].message);
            }
        }.bind(this));
    }

    render() {
        return(
            <div className="user-content" >
                <div className="content-title">
                    <div className="user-search" style={{display:'flex',flexFlow:'row',marginBottom:'10px'}}>
                        <span style={{marginLeft: 'auto'}}>
                            <Button type="primary" size={'middle'} onClick={()=>{
                                this.setState({
                                    addSoftVisible : true
                                })
                            }}>
                                增加
                            </Button>
                        </span>
                    </div>
                    <div className="patent-table">
                        <Spin spinning={this.state.loading}>
                            <Table columns={this.state.columns}
                                   dataSource={this.state.dataSource}
                                   pagination={false}
                                   onRowClick={this.tableRowClick}/>
                        </Spin>
                    </div>
                </div>
                {this.state.addSoftVisible ? <AddOfficialFeePrice
                    infor={this.state.infor}
                    visible={this.state.addSoftVisible}
                    onCancel={()=>{
                        this.setState({
                            addSoftVisible : false,
                            infor: {},
                        })
                    }}
                    successFn={()=>{
                        this.loadData();
                        this.setState({
                            addSoftVisible : false,
                            infor: {}
                        })
                    }}/> : <div/>}
            </div>
        )
    }
}

export default OfficialFees