import React from "react";
import {
  Row,
  Col,
  Icon,
  Modal,
  Button,
  Spin,
  Input,
  message,
  Badge,
  Table,
  Tabs,
  Tag,
} from "antd";
import { getProvince } from "../NewDicProvinceList.js";
import { getActives } from "../tools.js";
import "./topTab.less";
import $ from "jquery/src/ajax";
import logo from "../../../image/acc-logo.png";
import FollowDetail from "./customer/NEW/intentionCustomer/followDetail";
const TabPane = Tabs.TabPane;
const MessageModal = React.createClass({
  getInitialState() {
    return {
      unreadData: [],
      unreadDataType: "0",
      dataSourceType: "1",
      unreadDataIndex: 0,
      visible: false,
      loading: false,
      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        pageSize: 10,
        onChange: function (page) {
          this.loadReaded(page);
        }.bind(this),
        showTotal: function (total) {
          return "共" + total + "条数据";
        },
      },
      UnreadPagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        pageSize: 10,
        onChange: function (page) {
          this.loadUnread(page);
        }.bind(this),
        showTotal: function (total) {
          return "共" + total + "条数据";
        },
      },
      categoryArr: [],
      columns: [
        {
          title: "类型",
          dataIndex: "noticeType",
          key: "noticeType",
          render: (text, record) => {
            return {record.noticeTypeName};
          },
        },
        {
          title: "时间",
          dataIndex: "createTimeFormattedDate",
          key: "createTimeFormattedDate",
        },
        {
          title: "内容",
          dataIndex: "content",
          key: "content",
        },
        {
          title: "操作",
          dataIndex: "abc",
          key: "abc",
          render: (text, record) => {
            let contractNo = "";
            if (
              (record.noticeType === 14 || record.noticeType === 40) &&
              record.content.indexOf("合同编号-") !== -1
            ) {
              let arr = record.content.split("合同编号-");
              contractNo = arr[1].match(/(\S*), 订单编号/)[1];
            }
            return record.noticeType === 48 || record.noticeType === 49 ? (
              
            ) : record.noticeType === 14 || record.noticeType === 40 ? (
              
            ) : null;
          },
        },
      ],
      data: [],
    };
  },
  category() {
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/Varieties/getSuperList",
      data: {},
      success: function (data) {
        let thedata = data.data;
        let theArr = [];
        if (!thedata) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
          }
          thedata = {};
        } else {
          thedata.map(function (item, index) {
            theArr.push({
              value: item.id,
              key: item.cname,
            });
          });
        }
        this.setState({
          categoryArr: theArr,
        });
      }.bind(this),
    });
  },
  loadReaded(pageNo) {
    this.state.data = [];
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/notice/readed",
      data: {
        pageNo: pageNo || 1,
        pageSize: this.state.pagination.pageSize,
        type: this.state.unreadDataType,
      },
    })
      .done((data) => {
        if (!data.data) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
            return;
          }
        }
        this.state.data = [];
        for (let i = 0; i < data.data.list.length; i++) {
          let thisdata = data.data.list[i];
          this.state.data.push({
            key: i + 1,
            id: thisdata.id,
            rid: thisdata.rid,
            uid: thisdata.uid,
            aid: thisdata.aid,
            noticeType: thisdata.noticeType,
            year: thisdata.year,
            noticeTypeName: thisdata.noticeTypeName,
            content: thisdata.content,
            createTime: thisdata.createTime,
            createTimeFormattedDate: thisdata.createTimeFormattedDate,
            principle: thisdata.principle,
            unitName: thisdata.unitName,
          });
        }
        this.state.pagination.current = data.data.pageNo;
        this.state.pagination.total = data.data.totalCount;
        this.setState({
          dataSource: this.state.data,
          pagination: this.state.pagination,
        });
      })
      .always(
        function () {
          this.setState({
            loading: false,
          });
        }.bind(this)
      );
  },
  loadUnread(pageNo) {
    this.state.data = [];
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/notice/unread",
      data: {},
    })
      .done((data) => {
        if (data.error && data.error.length) {
          message.warning(data.error[0].message);
          return;
        } else {
          let arr = data.data.filter((v) => v.list.length > 0);
          if (arr.length) {
            this.setState({
              newVisible: true,
            });
          }
          let index = data.data.findIndex((v) => v.list.length > 0);
          if (index >= 0) {
            data.data[index].isRead = true;
            this.setState({
              unreadDataType: index ? data.data[index]["id"] + "" : "0",
              unreadDataIndex: index,
            });
          } else {
            this.setState({
              unreadDataType: data.data[0]["id"] + "",
              unreadDataIndex: "0",
            });
          }
          this.setState({
            unreadData: data.data,
          });
        }
      })
      .always(
        function () {
          this.setState({
            loading: false,
          });
        }.bind(this)
      );
  },
  componentWillReceiveProps(nextProps) {
    if (!this.state.visible && nextProps.showDesc) {
      this.loadReaded();
      this.loadUnread();
    }
    this.state.visible = nextProps.showDesc;
  },
  handleOk() {
    let arr = this.state.unreadData.filter(
      (v) => v.list.length > 0 && !v.isRead
    );
    if (arr.length > 0) {
      message.warning("还存在未读消息,请查看后再关闭当前页面!");
      return;
    } else {
      this.setState({
        visible: false,
      });
    }
    this.props.closeDesc(false, true);
  },
  handleCancel(e) {
    let arr = this.state.unreadData.filter(
      (v) => v.list.length > 0 && !v.isRead
    );
    if (arr.length > 0) {
      message.warning("还存在未读消息,请查看后再关闭当前页面!");
      return;
    } else {
      this.setState({
        visible: false,
      });
    }
    this.props.closeDesc(false, true);
  },
  closeDesc() {
    this.setState(
      {
        categoryArr: [],
        followData: "",
        visitModul: false,
      },
      () => {
        this.loadReaded(this.state.pagination.current);
      }
    );
  },
  render() {
    return (
      
        
          
            
               {
                  let index = this.state.unreadData.findIndex((d) => d.id == v);
                  this.state.unreadData[index].isRead = true;
                  this.setState(
                    {
                      unreadDataIndex: index,
                      unreadData: this.state.unreadData,
                      unreadDataType: v,
                    },
                    () => {
                      if (this.state.dataSourceType === "2") {
                        this.loadReaded();
                      }
                    }
                  );
                }}
              >
                {this.state.unreadData.map((v) => (
                  
                        {v.list.length === 0 || v.isRead ? null : (
                          
                        )}
                        {v.name}
                       
                    }
                  />
                ))}
              
               {
                  this.setState({
                    dataSourceType: v,
                  });
                  if (v === "2") {
                    this.loadReaded();
                  }
                }}
              >
                
                  
                
                
                  
                
              
            
            
          
        
        
      
    );
  },
});
const UserModal = React.createClass({
  getInitialState() {
    return {
      visible: false,
      loading: false,
      locations: [],
    };
  },
  loadData() {
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/adminInfo",
      success: function (data) {
        if (!data.data) {
          if (data.error[0] && data.error[0]) {
            message.warning(data.error[0].message);
          }
          return;
        }
        this.setState({
          id: data.data.id,
          mobile: data.data.mobile,
          contactMobile: data.data.contactMobile,
          name: data.data.name,
          position: data.data.position,
          email: data.data.email,
          createTime: data.data.createTime,
          number: data.data.number,
          createTimeFormattedDate: data.data.createTimeFormattedDate,
          pwd: "",
          password: "",
        });
        this.loadLocations(data.data.id);
      }.bind(this),
    }).always(
      function (data) {
        this.setState({
          loading: false,
        });
      }.bind(this)
    );
  },
  loadLocations(id) {
    this.setState({
      loading: true,
    });
    $.ajax({
      url: globalConfig.context + "/api/admin/supervise/adminDetailLocation",
      cache: false,
      data: {
        id: id,
      },
    }).done((data) => {
      if (!data.data) {
        if (data.error && data.error.length) {
          message.warning(data.error[0].message);
          return;
        }
      }
      data.data.map((item) => {
        if (item.city) {
          item.city = item.city.split(",");
        }
      });
      this.setState({
        locations: data.data,
        loading: false,
      });
    });
  },
  componentWillReceiveProps(nextProps) {
    if (!this.state.visible && nextProps.showDesc) {
      this.loadData();
    }
    this.state.visible = nextProps.showDesc;
  },
  handleOk() {
    this.setState({
      loading: true,
    });
    if (this.state.contactMobile) {
      if (!/^1[1-9][0-9]\d{4,8}$/.test(this.state.contactMobile)) {
        message.warning("不是完整的11位手机号或者正确的手机号前七位");
        this.setState({
          loading: false,
        });
        return false;
      }
    }
    //      if(this.state.mobile.length>11){
    //      	message.warning("登录账号不能超过11位");
    //      	return false;
    //      }
    $.ajax({
      method: "post",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/updateAdminInfo",
      data: {
        id: this.state.id,
        mobile: this.state.mobile,
        contactMobile: this.state.contactMobile,
        name: this.state.name,
        email: this.state.email,
        province: this.state.province,
        password: this.state.password,
        pwd: this.state.pwd,
      },
    }).always(
      function (data) {
        if (data.error && data.error.length) {
          message.warning(data.error[0].message);
        } else {
          this.setState({
            loading: false,
          });
          message.success("保存成功!");
          this.props.closeDesc(false, true);
        }
      }.bind(this)
    );
  },
  handleCancel(e) {
    this.setState({
      visible: false,
    });
    this.props.closeDesc(false);
  },
  render() {
    return (
      
    );
  },
});
const TopTab = React.createClass({
  getInitialState() {
    return {
      username: "请登录!",
      defaultActiveKey: "-1",
      htmlMenus: [],
    };
  },
  logOut() {
    let theActive = this.props.active;
    $.ajax({
      method: "get",
      dataType: "json",
      url: globalConfig.context + "/login",
    }).done(function (data) {
      window.location.href = globalConfig.context + "/admin/login.html";
    });
  },
  componentWillMount() {
    if (this.props.active) {
      this.loadData();
    }
    this.mainMenu();
  },
  //主菜单请求
  mainMenu() {
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/selectNavList",
      data: {},
      success: function (data) {
        if (!data.data) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
            return;
          }
        }
        let menu = data.data;
        var htmlMenu = [];
        let index = menu.findIndex(
          (v) => getActives(v.name) === this.props.active
        );
        this.setState({
          htmlMenus: menu,
          defaultActiveKey: index + "",
        });
      }.bind(this),
    }).always(
      function (data) {
        this.setState({
          loading: false,
        });
      }.bind(this)
    );
  },
  loadData() {
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/notice/unreadCount",
      success: function (data) {
        if (!data.data) {
          if (data.error[0] && data.error[0]) {
            message.warning(data.error[0].message);
            return;
          }
        }
        this.setState({
          badge: data.data,
        });
      }.bind(this),
    }).always(
      function (data) {
        this.setState({
          loading: false,
        });
      }.bind(this)
    );
  },
  closeUserModal(e, s) {
    this.state.userModalShow = e;
    if (s) {
      this.loadData();
    }
  },
  closeMessageModal(e, s) {
    this.state.messageModalShow = e;
    if (s) {
      this.loadData();
    }
  },
  render() {
    return (
      
        
        
        
          {this.state.htmlMenus.length > 0 ? (
             {
                window.location.href=globalConfig.context + this.state.htmlMenus[parseInt(v)].url+'?rid='+this.state.htmlMenus[parseInt(v)].id;
              }}
              // onChange={v=>{
              //     window.location.href=globalConfig.context + this.state.htmlMenus[parseInt(v)].url+'?rid='+this.state.htmlMenus[parseInt(v)].id;
              // }}
            >
              {this.state.htmlMenus.map((v, k) => (
                
              ))}
            
          ) : null}
        
        
        
       
    );
  },
});
export default TopTab;