import React from 'react';
import { Form, Button, Spin, message, Table, Popconfirm, Tooltip } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import { getfllowSituation, getcustomerStatue } from '@/tools.js';
import BusinessDetail from './businessDetail.jsx';

const Business = React.createClass({
  getInitialState() {
    return {
      loading: false,
      dataBusiness: [],
      followData: {},
      paginationt: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        pageSize: 10,
        onChange: function (page) {
          this.BusinessList(page, false);
        }.bind(this),
        showTotal: function (total) {
          return "共" + total + "条数据";
        },
      },
      businessIntentionList: [
        {
          title: "客户名称",
          dataIndex: "identifyName",
          key: "identifyName",
          render: (text) => {
            return (
              <Tooltip title={text}>
                <div
                  // style={{
                  //   maxWidth: '150px',
                  //   overflow: 'hidden',
                  //   textOverflow: "ellipsis",
                  //   whiteSpace: 'nowrap',
                  // }}
                >{text}</div>
              </Tooltip>
            )
          }
        },
        {
          title: "项目名称",
          dataIndex: "businessName",
          key: "businessName",
        },
        {
          title: "营销员",
          dataIndex: "adminName",
          key: "adminName",
        },
        {
          title: "业务意向进度",
          dataIndex: "followSituation",
          key: "followSituation",
          render: (text) => {
            return getfllowSituation(text);
          },
        },
        {
          title: "客户状态",
          dataIndex: "customerStatus",
          key: "customerStatus",
          render: (text) => {
            return getcustomerStatue(text);
          },
        }, {
          title: "意向时间",
          dataIndex: "createTime",
          key: "createTime",
        },
        {
          title: "操作",
          dataIndex: "ooo",
          key: "ooo",
          render: (text, record) => {
            return (
              <Button
                onClick={(e) => {
                  this.businessIntentionDetails(record);
                }}
              >
                查看跟进记录
              </Button>
            );
          },
        },
      ],
    };
  },
  businessIntentionDetails(record) {
    this.setState({
      followData: record,
      businessModul: true
    })
  },
  //业务意向
  BusinessList(pageNo) {
    this.setState({
      loading: true
    });
    $.ajax({
      method: "post",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + '/api/admin/customer/listBusiness',
      data: {
        pageNo: pageNo || 1,
        pageSize: this.state.paginationt.pageSize,
        uid: this.props.data.id
      },
      success: function (data) {
        let theArr = [];
        if (data.error.length || data.data.list == "") {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
          };
        } else {
          for (let i = 0; i < data.data.list.length; i++) {
            let thisdata = data.data.list[i];
            theArr.push({
              key: i,
              businessId: thisdata.businessId,
              businessProjectId: thisdata.businessProjectId,
              businessName: thisdata.businessName,
              identifyName: thisdata.identifyName,
              followSituation: thisdata.followSituation,
              customerStatus: thisdata.customerStatus,
              adminName: thisdata.adminName,
              createTime: thisdata.createTime
            });
          };
          this.state.paginationt.current = data.data.pageNo;
          this.state.paginationt.total = data.data.totalCount;
        };
        if (data.data.list && !data.data.list.length) {
          this.state.paginationt.current = 0;
          this.state.paginationt.total = 0;
        }
        this.setState({
          dataBusiness: theArr,
          paginationt: this.state.paginationt || []
        });
      }.bind(this),
    }).always(function () {
      this.setState({
        loading: false
      });
    }.bind(this));
  },
  componentWillMount() {
    this.BusinessList();
  },
  detailsModal() {
    this.props.closeDetail(false, false)
  },
  componentWillReceiveProps(nextProps) {
    if (nextProps.data.id && nextProps.businessState) {
      this.BusinessList();
    }
    this.setState({
      businessModul: false
    })
  },
  render() {
    return (
      <div className="clearfix">
        <Spin spinning={this.state.loading}>
          <Table
            pagination={this.state.paginationt}
            columns={this.state.businessIntentionList}
            dataSource={this.state.dataBusiness}
          />
        </Spin>
        <BusinessDetail
          ids={this.props.data.id}
          data={this.state.followData}
          businessModul={this.state.businessModul}
        />
      </div>
    )
  }
})

export default Business;
