import React from 'react';
import {
  DatePicker,
  Tooltip,
  Button,
  AutoComplete,
  Input,
  Select,
  Spin,
  Table,
  message,
  Form,
  Tabs,
  Switch
} from 'antd';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import { ShowModal, getApprovedType } from "@/tools.js";
import { } from "@/dataDic";
import ShowModalDiv from "@/showModal.jsx";
import { ChooseList } from "../../order/orderNew/chooseList";

import './index.less';

const TabPane = Tabs.TabPane;
const { RangePicker, } = DatePicker;

const Unlimited = Form.create()(React.createClass({

  getInitialState() {
    return {
      searchValues: {},
      loading: false,
      exportPendingLoading: false,
      changeList: undefined,
      dataSource: [],
      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: "type",
          key: "type",
          width: 110,
          render: (text, record) => {
            return (
              <span>
                {getApprovedType(text ? text.split(",").map(Number) : [], record.typeExplain)}
              </span>
            );
          },
        },
        {
          title: "合同编号",
          dataIndex: "contractNo",
          key: "contractNo",
        },
        {
          title: '客户名称',
          dataIndex: 'userName',
          key: 'userName',
          render: (text, record) => {
            return (
              <Tooltip title={text}>
                <div style={{ maxWidth: 200 }}>{text}</div>
              </Tooltip>
            );
          }
        },
        {
          title: "订单部门",
          dataIndex: "depName",
          key: "depName",
          width: 100,
        },
        {
          title: "订单负责人",
          dataIndex: "salesmanName",
          key: "salesmanName"
        },
        {
          title: "签单日期",
          dataIndex: "time",
          key: "time",
        },
        {
          title: "下单时间",
          dataIndex: "createDate",
          key: "createTime",
          width: 80,
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "签单金额(万元)",
          dataIndex: "totalAmount",
          key: "totalAmount",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "开票金额(万元)",
          dataIndex: "invoiceAmount",
          key: "invoiceAmount",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "已收款(万元)",
          dataIndex: "settlementAmount",
          key: "settlementAmount",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "应收款(万元)",
          dataIndex: "orderReceivables",
          key: "orderReceivables",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "欠款(万元)",
          dataIndex: "orderArrears",
          key: "orderArrears",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "总成本/实付",
          dataIndex: "costAmount",
          key: "costAmount",
          render: (text) => {
            return <div style={{ textAlign: "center" }}>{text}</div>
          },
        },
        {
          title: "公出时长(时)",
          dataIndex: "duration",
          key: "duration",
        },
      ],
      dataSource: [],
      departmentArr: [],
    };
  },

  componentWillMount() {
    // this.departmentList();
    // this.selectSuperId();
    this.loadData();
  },

  loadData(pageNo) {
    const { searchValues, pagination } = this.state;
    this.setState({
      loading: true,
    });
    let datas = Object.assign(searchValues, {
      pageNo: pageNo || 1,
      pageSize: pagination.pageSize,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/statistis/orderProject",
      data: datas,
      success: function (data) {
        ShowModal(this);
        this.setState({
          loading: false,
        });
        if (data.error && data.error.length === 0) {
          if (data.data.list) {
            pagination.current = data.data.pageNo;
            pagination.total = data.data.totalCount;
            if (data.data && data.data.list && !data.data.list.length) {
              pagination.current = 0;
              pagination.total = 0;
            }
            this.setState({
              dataSource: data.data.list,
              pagination: this.state.pagination,
              pageNo: data.data.pageNo,
            });
          } else {
            this.setState({
              dataSource: data.data,
              pagination: false,
            });
          }
        } else {
          message.warning(data.error[0].message);
        }
      }.bind(this),
    }).always(
      function () {
        this.setState({
          loading: false,
        });
      }.bind(this)
    );
  },

  // 部门
  departmentList() {
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/organization/selectSuperId",
      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({
              key: index,
              name: item.name,
              id: item.id,
            });
          });
        }
        this.setState({
          departmentArr: theArr,
        });
      }.bind(this),
    }).always(
      function () {
        this.setState({
          loading: false,
        });
      }.bind(this)
    );
  },

  // 项目类别
  selectSuperId() {
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/ProjectSize/getAllCname",
      data: {
        flag: 0
      },
      success: function (data) {
        let theArr = [];
        let thedata = data.data;
        if (!thedata) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
          }
        }
        for (let i = 0; i < data.data.length; i++) {
          let theData = data.data[i];
          theArr.push(
            <Select.Option value={theData.id} key={theData.cname}>
              {theData.cname}
            </Select.Option>
          );
        }
        this.setState({
          contactsOption: theArr,
          contactsOptionData: data.data,
        });
      }.bind(this)
    }).always(

    );
  },

  changeList(arr) {
    const newArr = [];
    let list = this.state.columns
    list.forEach((item) => {
      arr.forEach((val) => {
        if (val === item.title) {
          newArr.push(item);
        }
      });
    });
    this.setState({
      changeList: newArr,
    });
  },

  search() {
    this.loadData();
  },

  reset() {
    this.setState({

      searchValues: JSON.parse(JSON.stringify({})),
    }, () => {
      this.loadData();
    })
  },

  render() {
    const { searchValues } = this.state
    let departmentArr = this.state.departmentArr || [];
    return (
      <div className="user-content">
        <ShowModalDiv ShowModal={this.state.showModal} onClose={() => { this.setState({ showModal: false }) }} />
        <div className="content-title" style={{ marginBottom: 10 }}>
          <span style={{ fontWeight: 900, fontSize: 16 }}>无限制公出管理</span>
        </div>
        <div className="user-search">
          <Tabs
            defaultActiveKey="1"
            className="test">
            <TabPane tab="搜索" key="1">
              <div
                className="user-search"
                style={{
                  marginTop: "10px",
                  marginLeft: "10px",
                  marginRight: "10px",
                }}
              >
                <Input
                  placeholder="合同编号"
                  style={{
                    width: 220,
                    marginRight: "10px",
                  }}
                  value={searchValues["contractNo"]
                    ? searchValues["contractNo"]
                    : ""}
                  onChange={(e) => {
                    searchValues["contractNo"] = e.target.value;
                    this.setState({
                      searchValues: searchValues,
                    });
                  }}
                />
                <Input
                  placeholder="客户名称"
                  style={{
                    width: 220,
                    marginRight: "10px",
                  }}
                  value={searchValues["userName"]
                    ? searchValues["userName"]
                    : ""}
                  onChange={(e) => {
                    searchValues["userName"] = e.target.value;
                    this.setState({
                      searchValues: searchValues,
                    });
                  }}
                />
                <Input
                  placeholder="项目名称"
                  value={searchValues["projectName"]
                    ? searchValues["projectName"]
                    : ""}
                  style={{ width: 220, marginRight: 10 }}
                  onChange={e => {
                    searchValues["projectName"] = e.target.value
                    this.setState({
                      searchValues: searchValues,
                    });
                  }}
                />

                <Button type="primary" onClick={this.search} style={{ marginRight: 10 }}>
                  搜索
                </Button>
                <Button onClick={this.reset}>重置</Button>
              </div>
            </TabPane>
            <TabPane tab="更改表格显示数据" key="2">
              <div style={{ marginLeft: 10 }}>
                <ChooseList
                  columns={this.state.columns}
                  changeFn={this.changeList}
                  changeList={this.state.changeList}
                  top={55}
                  margin={11}
                />
              </div>
            </TabPane>
          </Tabs>
        </div>
        <div className="patent-table">
          <Spin spinning={this.state.loading}>
            <Table
              columns={this.state.changeList
                ? this.state.changeList
                : this.state.columns}
              dataSource={this.state.dataSource}
              pagination={this.state.pagination}
              bordered
            />
          </Spin>
        </div>
      </div >
    );
  }
}));
export default Unlimited;
