import React from 'react';
import { Icon, Button, Input, Select, Spin, Table, DatePicker, message, Cascader, Switch, Modal } from 'antd';
import { contractStateList, contractTypeList } from '../../../dataDic.js';
import { getTime, companySearch, getContractState, getContractType, getSearchUrl } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import ContractAdd from './contractAdd.jsx';
import ContractDetail from './contractDetail.jsx';

import moment from 'moment';
import './contract.less';

const Contract = React.createClass({
    loadData(pageNo) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/contract/list",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                type: this.state.contractType,
                status: this.state.contractState,
                serialNumber: this.state.serialNumber,
                startDateFormattedDate: this.state.startDateFormattedDate[0],
                endDateFormattedDate: this.state.endDateFormattedDate[1],
                completeStartDate: this.state.endDateFormattedDate[0],
                completeEndDate: this.state.endDateFormattedDate[1]
            },
            success: function (data) {
                let theArr = [];
                if (!data.data || !data.data.list) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    return;
                };
                for (let i = 0; i < data.data.list.length; i++) {
                    let thisdata = data.data.list[i];
                    theArr.push({
                        key: i,
                        id: thisdata.id,
                        uid: thisdata.uid,
                        contacts: thisdata.contacts,
                        serialNumber: thisdata.serialNumber,
                        type: thisdata.type,
                        signDate: thisdata.signDate,
                        depict: thisdata.depict,
                        status: thisdata.status,
                        completeDate: thisdata.completeDate,
                        signDateFormattedDate: thisdata.signDateFormattedDate,
                        completeDateFormattedDate: thisdata.completeDateFormattedDate
                    });
                };
                this.state.pagination.defaultCurrent = data.data.pageNo;
                this.state.pagination.total = data.data.totalCount;
                this.setState({
                    dataSource: theArr,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getContactsList() {
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/cognizance/getContacts",
            success: function (data) {
                let theOption = [];
                if (data.error.length || !data.data) {
                    return;
                };
                for (let item in data.data) {
                    let theData = data.data[item];
                    theOption.push(
                        <Select.Option value={item} key={theData}>{theData}</Select.Option>
                    );
                };
                this.setState({
                    contactsOption: theOption,
                    contactsList: data.data
                });
            }.bind(this),
        });
    },
    getInitialState() {
        return {
            visible: false,
            contactsList: {},
            loading: false,
            showAdd: false,
            showDesc: false,
            searchMore: true,
            startDateFormattedDate: [],
            endDateFormattedDate: [],
            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: 'serialNumber',
                    key: 'serialNumber',
                }, {
                    title: '签订时间',
                    dataIndex: 'signDateFormattedDate',
                    key: 'signDateFormattedDate',
                }, {
                    title: '合同状态',
                    dataIndex: 'status',
                    key: 'status',
                    render: (text) => { return getContractState(text) }
                }, {
                    title: '合同类型',
                    dataIndex: 'type',
                    key: 'type',
                    render: (text) => { return getContractType(text) }
                }, {
                    title: '联系人',
                    dataIndex: 'contacts',
                    key: 'contacts',
                    render: (text) => { return this.state.contactsList[text] }
                }, {
                    title: '完成时间',
                    dataIndex: 'completeDateFormattedDate',
                    key: 'completeDateFormattedDate',
                }, {
                    title: '描述',
                    dataIndex: 'describe',
                    key: 'describe',
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        let _me = this, tArr = [], sArr = [];
        contractTypeList.map(function (item) {
            tArr.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        contractStateList.map(function (item) {
            sArr.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        this.setState({
            typeOption: tArr,
            stateOption: sArr
        });
        this.loadData();
        this.getContactsList();
        // if (window.location.search) {
        //     let theArr = getSearchUrl(window.location.search);
        //     if (theArr.length > 1) {
        //         let theObj = {};
        //         theObj.id = theArr[0];
        //         theObj.uid = theArr[1];
        //         if (theObj.id != 'null' && theObj.uid != 'null') {
        //             this.tableRowClick(theObj);
        //         };
        //     } else if (theArr.length == 1) {
        //         this.state.unitName = theArr[0];
        //     };
        // };
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        if (record.status === '0') {
            this.setState({
                showAdd: true
            });
        } else {
            this.setState({
                showDesc: true
            });
        };
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    addClick() {
        this.state.RowData = {};
        this.setState({
            showAdd: true,
        });
    },
    closeAdd(e, s) {
        this.setState({
            showAdd: e
        });
        if (s) {
            this.loadData();
        };
    },
    searchSwitch() {
        this.setState({
            searchMore: !this.state.searchMore
        });
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.contractState = undefined;
        this.state.contractType = undefined;
        this.state.serialNumber = undefined;
        this.state.startDateFormattedDate = [];
        this.state.endDateFormattedDate = [];
        this.loadData();
    },
    render() {
        const { MonthPicker, RangePicker } = DatePicker;
        return (
            <div className="foster-box">
                <div className="foster-content">
                    <div className="content-title">
                        <span>合同申请管理</span>
                        <ContractAdd
                            contactsOption={this.state.contactsOption}
                            typeOption={this.state.typeOption}
                            data={this.state.RowData}
                            onClick={this.addClick}
                            showAdd={this.state.showAdd}
                            closeAdd={this.closeAdd} />
                    </div>
                    <div className="foster-query">
                        <Input value={this.state.serialNumber}
                            placeholder="输入合同编号"
                            onChange={(e) => { this.setState({ serialNumber: e.target.value }); }} />
                        <Select placeholder="选择一个合同类型"
                            style={{ width: 200 }}
                            value={this.state.contractType}
                            showSearch
                            filterOption={companySearch}
                            onChange={(e) => { this.setState({ contractType: e }) }}>
                            {this.state.typeOption}
                        </Select>
                        <Select style={{ width: 120 }}
                            value={this.state.contractState}
                            onChange={(e) => { this.setState({ contractState: e }) }}
                            placeholder="选择一个状态">
                            {this.state.stateOption}
                        </Select>
                        <span>更多搜索  <Switch defaultChecked={false} onChange={this.searchSwitch} /></span>
                        <Button type="primary" onClick={this.search}>搜索</Button>
                        <Button onClick={this.reset}>重置</Button>
                        <div className="search-more" style={this.state.searchMore ? { display: 'none' } : {}}>
                            <span>签订时间:</span>
                            <RangePicker style={{ width: '240px' }}
                                allowClear={false}
                                value={[this.state.startDateFormattedDate[0] ? moment(this.state.startDateFormattedDate[0]) : null,
                                this.state.startDateFormattedDate[1] ? moment(this.state.startDateFormattedDate[1]) : null]}
                                onChange={(date, dateString) => { this.setState({ startDateFormattedDate: dateString }) }} />
                            <span>完成时间:</span>
                            <RangePicker style={{ width: '240px' }}
                                allowClear={false}
                                value={[this.state.endDateFormattedDate[0] ? moment(this.state.endDateFormattedDate[0]) : null,
                                this.state.endDateFormattedDate[1] ? moment(this.state.endDateFormattedDate[1]) : null]}
                                onChange={(date, dateString) => { this.setState({ endDateFormattedDate: dateString }) }} />
                        </div>
                    </div>
                    <div className="foster-table">
                        <Spin spinning={this.state.loading}>
                            <Table columns={this.state.columns}
                                dataSource={this.state.dataSource}
                                pagination={this.state.pagination}
                                onRowClick={this.tableRowClick} />
                        </Spin>
                    </div>
                    <ContractDetail
                        data={this.state.RowData}
                        statusOption={this.state.statusOption}
                        contactsList={this.state.contactsList}
                        showDesc={this.state.showDesc}
                        closeDesc={this.closeDesc} />
                </div >
            </div>
        );
    }
});

export default Contract;