import React, { Component } from "react";
import { Button, Cascader, Input, message, Select, Spin, Table, Tabs, Tooltip } from "antd";
import $ from "jquery/src/ajax";
import { ChooseList } from "../../../order/orderNew/chooseList";
import { projectStatusCascader } from '@/dataDic.js';
import CheckInfor from './checkInfor';
import OrderInfor from "./orderInfor";
import Cascaders from "../../../../common/cascaders"

const TabPane = Tabs.TabPane;

class BusinessManagement extends Component {
  constructor(props) {
    super(props);
    this.state = {
      changeList: [],
      pageNo: 1,
      columns: [
        {
          title: '项目编号',
          dataIndex: 'id',
          key: 'id',
          // fixed: 'left',
          render: (text, record) => {
            if (record.splitStatus == 2) {
              return <span>{record.splitSuper + '-' + record.splitId}</span>
            } else {
              return text
            }
          },
        },
        {
          title: '项目名称',
          dataIndex: 'commodityName',
          key: 'commodityName',
          // fixed: 'left',
          width: 100,
          render: text => {
            return (
              <Tooltip title={text}>
                <div style={{
                  maxWidth: '100px',
                  overflow: 'hidden',
                  textOverflow: "ellipsis",
                  whiteSpace: 'nowrap',
                }}>{text}</div>
              </Tooltip>
            )
          }
        },
        {
          title: '项目数量',
          dataIndex: 'commodityQuantity',
          key: 'commodityQuantity',
          width: 50,
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          key: 'orderNo',
        },
        {
          title: '合同编号',
          dataIndex: 'contractNo',
          key: 'contractNo',
        },
        {
          title: '客户名称',
          dataIndex: 'userName',
          key: 'userName',
          className: 'userName',
          render: text => {
            return (
              <Tooltip title={text}>
                <div
                // style={{
                //   maxWidth: '100px',
                //   overflow: 'hidden',
                //   textOverflow: "ellipsis",
                //   whiteSpace: 'nowrap',
                // }}
                >{text}</div>
              </Tooltip>
            )
          }
        },
        {
          title: '业务类别',
          dataIndex: 'cname',
          key: 'cname',
        },
        {
          title: '分配时间',
          dataIndex: 'taskDistributionTime',
          key: 'taskDistributionTime',
        },
        {
          title: '订单部门',
          dataIndex: 'depName',
          key: 'depName',
        },
        {
          title: '操作',
          dataIndex: 'operation',
          key: 'operation',
          render: (text, record) => {
            return (
              <Button
                type="primary"
                style={{ margin: '0 10px' }}
                onClick={(e) => {
                  e.stopPropagation();
                  this.setState({
                    visible: true,
                    infor: record
                  })
                }}
              >
                核对专利
              </Button>

            )
          }
        },
      ],
      dataSource: [],
      infor: {},


      nameSearch: '',
      orderNoSearch: '',
      contractNoSearch: undefined,
      taskNoSearch: '',
      departmenttSearch: undefined,
      projectStatusSearch: [], //项目状态
      approvalSearch: undefined,
      outsourceStatusSearch: [],

      pagination: {
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        pageSize: 10,
        onChange: function (page) {
          this.loadData(page)
        }.bind(this),
        showTotal: function (total) {
          return '共' + total + '条数据'
        },
      },

      departmentArr: []
    }
    this.loadData = this.loadData.bind(this);
    this.changeList = this.changeList.bind(this);
    this.departmentList = this.departmentList.bind(this);
    this.reset = this.reset.bind(this);
  }

  componentDidMount() {
    this.loadData();
    this.departmentList();
  }

  loadData(pageNo) {
    this.setState({
      loading: true,
    });
    $.ajax({
      method: "get",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/orderProject/selectProjectCheck",
      data: {
        pageNo: pageNo || 1,
        pageSize: this.state.pagination.pageSize,
        name: this.state.nameSearch || undefined, //客户名称
        deps: this.state.departmenttSearch, //订单部门
        orderNo: this.state.orderNoSearch || undefined, //订单编号
        contractNo: this.state.contractNoSearch, //合同编号
        taskId: this.state.taskNoSearch || undefined, //项目编号
        taskStatus: this.state.taskStatuSearch, //项目状态
        projectStatus: this.state.projectStatusSearch && this.state.projectStatusSearch.length > 0 ?
          (typeof this.state.projectStatusSearch[0] === 'string' ? this.state.projectStatusSearch[1] : this.state.projectStatusSearch[0]) : undefined,  //项目状态
        adminName: this.state.adminName, //项目受理人
        approval: this.state.approvalSearch, //特批状态搜索
        outsourceStatus:
          this.state.outsourceStatusSearch >= 999
            ? undefined
            : this.state.outsourceStatusSearch, //外包状态搜索
      },
      success: function (data) {
        let theArr = []
        if (data.error.length === 0) {
          for (let i = 0; i < data.data.list.length; i++) {
            let thisdata = data.data.list[i];
            thisdata.key = i;
            theArr.push(thisdata)
          }
          this.state.pagination.current = data.data.pageNo
          this.state.pagination.total = data.data.totalCount
          if (data.data && data.data.list && !data.data.list.length) {
            this.state.pagination.current = 0
            this.state.pagination.total = 0
          }
          this.setState({
            dataSource: theArr,
            pagination: this.state.pagination,
            pageNo: data.data.pageNo
          })
        } else {
          message.warning(data.error[0].message)
        }
      }.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
    });
  }

  //部门
  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)
    )
  }

  reset() {
    this.setState({
      nameSearch: '',
      orderNoSearch: '',
      contractNoSearch: undefined,
      taskNoSearch: '',
      departmenttSearch: undefined,
      projectStatusSearch: [], //项目状态
      approvalSearch: undefined,
      outsourceStatusSearch: [],
    }, () => {
      this.Cascaders.empty();
      this.loadData();
    })
  }

  render() {
    return (
      <div className="user-content">
        <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={{ marginLeft: 10 }}>
                <Input
                  placeholder="订单编号"
                  value={this.state.orderNoSearch}
                  onChange={(e) => {
                    this.setState({ orderNoSearch: e.target.value })
                  }}
                />
                <Input
                  placeholder="合同编号"
                  value={this.state.contractNoSearch}
                  onChange={(e) => {
                    this.setState({ contractNoSearch: e.target.value })
                  }}
                />
                <Input
                  placeholder="客户名称"
                  value={this.state.nameSearch}
                  onChange={(e) => {
                    this.setState({ nameSearch: e.target.value })
                  }}
                />
                <Input
                  placeholder="项目编号"
                  value={this.state.taskNoSearch}
                  onChange={(e) => {
                    this.setState({ taskNoSearch: e.target.value })
                  }}
                />
                <Cascaders
                  ref={node => this.Cascaders = node}
                  placeholder="选择部门"
                  id="id"
                  name="name"
                  children="list"
                  height={28}
                  onSel={(e) => {
                    this.setState({
                      departmenttSearch: JSON.stringify(e),
                    });
                  }}
                />
                {/* <Select
                                    placeholder="选择部门"
                                    style={{ width: 200, marginRight: '10px' }}
                                    value={this.state.departmenttSearch}
                                    onChange={(e) => {
                                        this.setState({ departmenttSearch: e })
                                    }}
                                >
                                    {this.state.departmentArr.map(function (item) {
                                        return (
                                            <Select.Option key={item.id}>{item.name}</Select.Option>
                                        )
                                    })}
                                </Select> */}
                <Cascader
                  placeholder="项目状态"
                  style={{ width: 150, marginRight: '10px' }}
                  options={projectStatusCascader}
                  value={this.state.projectStatusSearch}
                  onChange={(e) => {
                    this.setState({
                      projectStatusSearch: e
                    })
                  }}
                />
                <Select
                  placeholder="特批状态"
                  style={{ width: 150, marginRight: '10px' }}
                  value={this.state.approvalSearch}
                  onChange={(e) => {
                    this.setState({ approvalSearch: e })
                  }}
                >
                  <Select.Option key={0}>非特批</Select.Option>
                  <Select.Option key={1}>特批</Select.Option>
                </Select>
                <Select
                  placeholder="外包状态"
                  style={{ width: 150, marginRight: '10px' }}
                  value={this.state.outsourceStatusSearch}
                  onChange={(e) => {
                    this.setState({ outsourceStatusSearch: e })
                  }}
                >
                  <Select.Option key={0}>外包待审核</Select.Option>
                  <Select.Option key={2}>外包审核驳回</Select.Option>
                  <Select.Option key={999}>全部</Select.Option>
                </Select>
                <Button type="primary" onClick={() => {
                  this.loadData();
                }}>
                  搜索
                </Button>
                <Button
                  onClick={(e) => {
                    this.reset(true)
                  }}
                >
                  重置
                </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
              size="middle"
              columns={
                this.state.changeList.length !== 0
                  ? this.state.changeList
                  : this.state.columns
              }
              dataSource={this.state.dataSource}
              pagination={this.state.pagination}
              onRowClick={(record) => {
                this.setState({
                  orderVisible: true,
                  orderNo: record.orderNo
                })
              }}
            />
          </Spin>
        </div>
        {this.state.visible ? <CheckInfor
          visible={this.state.visible}
          infor={this.state.infor}
          onCancel={() => {
            this.setState({
              visible: false,
              infor: {}
            })
            this.loadData(this.state.pageNo);
          }}
        /> : null}
        {this.state.orderVisible ? <OrderInfor
          visible={this.state.orderVisible}
          orderNo={this.state.orderNo}
          onCancel={() => {
            this.setState({
              orderVisible: false,
              orderNo: ''
            })
          }}
        /> : null}
      </div>
    )
  }
}

export default BusinessManagement;