import React, { Component } from 'react';
import {
    Button, DatePicker,
    Input,
    message,
    Select,
    Spin,
    Table, Tabs,
} from "antd";
import './index.less';
import $ from "jquery/src/ajax";
import CustomerDetails from './customerDetails';
import moment from "moment";
import { ChooseList } from "../../../order/orderNew/chooseList";
const { RangePicker } = DatePicker;
const { TabPane } = Tabs;
class DepartmentStatistics extends Component {
    constructor(props) {
        super(props);
        this.state = {
            superId: undefined,
            nameSearch: '',
            releaseDate: [moment(Date.now() - 777600000).format('YYYY-MM-DD'), moment(Date.now()).format('YYYY-MM-DD')],
            columns: [
                {
                    title: "部门",
                    dataIndex: "name",
                    key: "name",
                    width: '25%',
                },
                {
                    title: "私有数",
                    dataIndex: "userCount",
                    key: "userCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                            
 {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aid: record.aid,
                                            aName: record.aName,
                                            type: 0,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "外联数",
                    dataIndex: "channelCount",
                    key: "channelCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aid: record.aid,
                                            aName: record.aName,
                                            type: 1,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "签单数",
                    dataIndex: "signCount",
                    key: "signCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 2,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "私有新增数",
                    dataIndex: "newCount",
                    key: "newCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 3,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "外联新增数",
                    dataIndex: "channelNewCount",
                    key: "channelNewCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 4,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "私有面谈数",
                    dataIndex: "completeCount",
                    key: "completeCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 5,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "外联面谈数",
                    dataIndex: "channelCompleteCount",
                    key: "channelCompleteCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 6,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
                {
                    title: "私有领取数",
                    dataIndex: "receiveCount",
                    key: "receiveCount",
                    className: 'tableRowStyle',
                    width: 120,
                    render: (text, record) => {
                        return (
                             {
                                    if (record.aid) {
                                        e.stopPropagation();
                                        this.setState({
                                            customerVisible: true,
                                            aName: record.aName,
                                            aid: record.aid,
                                            type: 7,
                                        })
                                    }
                                }}>
                                {text}
                            
                        );
                    },
                },
            ],
            contactsOption: null,
            loading: false,
            ispage: 1,
            aid: '',
            type: '',
            visible: false,
        };
        this.loadData = this.loadData.bind(this);
        this.reset = this.reset.bind(this);
        this.selectSuperId = this.selectSuperId.bind(this);
        this.changeList = this.changeList.bind(this);
    }
    componentDidMount() {
        this.selectSuperId();
    }
    loadData() {
        if (!this.state.superId) {
            message.warning('请选择部门后再搜索')
            return;
        }
        this.setState({
            loading: true,
        });
        let api = '/api/admin/customer/selectAllUser';
        let data = {};
        if (this.state.superId) {
            data.depId = this.state.superId;
        }
        if (this.state.nameSearch) {
            data.aName = this.state.nameSearch;
        }
        if (this.state.releaseDate[0]) {
            data.startTime = this.state.releaseDate[0]
        }
        if (this.state.releaseDate[1]) {
            data.endTime = this.state.releaseDate[1]
        }
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + api,
            data: data,
            success: function (data) {
                let theArr = [];
                if (data.error.length !== 0) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    }
                } else {
                    data.data.children = data.data.list;
                    data.data.children = data.data.children.concat(data.data.aList);
                    theArr.push(data.data);
                    theArr = this.handleTabsInfor(theArr);
                    this.setState({
                        ispage: data.data.pageNo,
                        dataSource: theArr,
                    });
                }
            }.bind(this),
        }).always(
            function () {
                this.setState({
                    loading: false,
                });
            }.bind(this)
        );
    };
    //表格数据处理
    handleTabsInfor(infor) {
        for (let i = 0; i < infor.length; i++) {
            if (!infor[i].key) {
                infor[i].key = infor[i].id ? infor[i].id : (infor[i].aid ? infor[i].aid : i)
            }
            if (infor[i].children) {
                for (let c = 0; c < infor[i].children.length; c++) {
                    infor[i].children[c].key = infor[i].children[c].id;
                    infor[i].children[c].children = infor[i].children[c].list;
                    if (infor[i].children[c].aName) { infor[i].children[c].name = infor[i].children[c].aName }
                    if (infor[i].children[c].aList && Array.isArray(infor[i].children[c].aList) && infor[i].children[c].aList.length) {
                        if (infor[i].children[c].children && Array.isArray(infor[i].children[c].children)) {
                            for (let t of infor[i].children[c].aList) {
                                t.key = t.aid;
                                t.name = t.aName;
                            }
                            infor[i].children[c].children = infor[i].children[c].children.concat(infor[i].children[c].aList);
                        }
                    }
                    this.handleTabsInfor(infor[i].children);
                }
            }
        }
        return infor;
    }
    reset() {
        this.setState({
            nameSearch: '',
            releaseDate: [moment(Date.now() - 777600000).format('YYYY-MM-DD'), moment(Date.now()).format('YYYY-MM-DD')],
        }, () => {
            this.loadData();
        });
    };
    //获取上级组织
    selectSuperId() {
        this.state.data = []
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/organization/selectSuperId",
            data: {},
            success: function (data) {
                let theArr = [];
                let theId = [];//用于保存上级组织的ID和名称
                let thedata = data.data;
                if (!thedata) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thedata = {};
                };
                for (var i = 0; i < data.data.length; i++) {
                    let theData = data.data[i];
                    theArr.push(
                        {theData.name}
                    );
                    theId.push(
                        [theData.id, theData.name]
                    );
                };
                this.setState({
                    SuperArr: thedata,
                    contactsOption: theArr,
                    theId: theId,
                    orderStatusOptions: data.data,
                });
                if (!this.state.superId) {
                    this.setState({
                        superId: data.data[0].id
                    }, () => {
                        this.loadData();
                    })
                }
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    }
    changeList(arr) {
        const newArr = [];
        this.state.columns.forEach(item => {
            arr.forEach(val => {
                if (val === item.title) {
                    newArr.push(item);
                }
            });
        });
        this.setState({
            changeList: newArr
        });
    }
    render() {
        return (
            
                
                    部门统计
                
                
                    
                        
                            
                                
                            
                             {
                                    this.setState({ nameSearch: e.target.value });
                                }}
                            />
                            
                                 {//“运营管理”角色只能查看部分时间段(暂时10天)的数据
                                        let iskeep = window.adminData.shiroList.indexOf("11") != -1
                                        return iskeep && current && current.valueOf() < Date.now() - 864000000;
                                    }}
                                    value={[
                                        this.state.releaseDate[0]
                                            ? moment(this.state.releaseDate[0])
                                            : null,
                                        this.state.releaseDate[1]
                                            ? moment(this.state.releaseDate[1])
                                            : null,
                                    ]}
                                    onChange={(data, dataString) => {
                                        this.setState({ releaseDate: dataString });
                                    }}
                                />
                            
                            
                            
                         
                    
                    
                        
                            
                        
                    
                
                
                {this.state.customerVisible ? 
 {
                        this.setState({
                            customerVisible: false,
                            aid: '',
                            type: '',
                        })
                    }}
                /> : }
             
        )
    }
}
export default DepartmentStatistics;