import React, { Component } from 'react'
import {
  Form,
  Spin,
  Table,
  Modal,
  Col,
  Upload,
  Input,
  Button, message,
  Row,
} from 'antd'
import { getMaterialStatus, getUrgentStatus } from '@/tools'
import ApplyPaymentModal from './applyPaymentModal';
import './index.less'
import $ from "jquery";

class CheckProject extends Component {
  constructor(props) {
    super(props)
    this.state = {
      value: '',
      loading: false,
      previewImage: '',
      previewVisible: false,
      previewPayVisible: false,
      previewPayInfor: {},        //点击申请付款的第三方信息或者支付节点信息
      nodeId: -1,
      // 专利
      ContactsListsNew: [
        {
          title: '供应商名称',
          dataIndex: 'companyName',
          key: 'companyName',
          render: (text, record, index) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '单价（万元）',
          dataIndex: 'unitPrice',
          key: 'unitPrice',
          render: (text, record) => {
            if (text) {
              return <span>***</span>
            }
          },
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          key: 'quantity',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '总价(万元)',
          dataIndex: 'totalAmount',
          key: 'totalAmount',
          render: (text, record) => {
            if (text) {
              return <span>***</span>
            }
          },
        },
        {
          title: '已付(万元)',
          dataIndex: 'partyAmount',
          key: 'partyAmount',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '材料',
          dataIndex: 'material',
          key: 'material',
          render: (text, record) => {
            return getMaterialStatus(text)
          },
        },
        {
          title: '加急',
          dataIndex: 'urgent',
          key: 'urgent',
          render: (text, record) => {
            return getUrgentStatus(text)
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          key: 'action',
          render: (text, record) => {
            return (
              <div>
                {/*外包审核通过后，咨询师/咨询经理可申请付款*/}
                {/*0-待审核 ，1-审核通过，2-审核拒绝 true [NULL]*/}
                {this.props.isPreviewPay && this.props.status === 1 && this.props.dataSource.length === 0?
                <Button type="primary" onClick={() => {
                  this.setState({
                    previewPayVisible: true,
                    previewPayInfor: record,
                  })
                }}>
                  申请付款
                </Button> : <div/>}
              </div>
            )
          },
        },
      ],
      //付款节点表头
      PayNodeTableColunms: [
        {
          title: '供应商名称',
          dataIndex: 'companyName',
          key: 'companyName',
          render: (text, record, index) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '付款科目',
          dataIndex: 'dunType',
          key: 'dunType',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '付款时间',
          dataIndex: 'partyTimes',
          key: 'partyTimes',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          key: 'quantity',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '总价(万元)',
          dataIndex: 'totalAmount',
          key: 'totalAmount',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '已付(万元)',
          dataIndex: 'partyAmount',
          key: 'partyAmount',
          render: (text, record) => {
            if (text) {
              return <span>{text}</span>
            }
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          key: 'action',
          render: (text, record) => {
            return (
                <div>
                  {/*外包审核通过后，咨询师/咨询经理可申请付款*/}
                  {/*0-待审核 ，1-审核通过，2-审核拒绝 true [NULL]*/}
                  <Button type="primary" onClick={() => {
                    this.setState({
                      previewPayVisible: true,
                      nodeId: record.id,           //付款节点id
                      previewPayInfor: record,
                    })
                  }}>
                    申请付款
                  </Button>
                </div>
            )
          },
        },
      ],
      outsourceLogs: [],//审核日志
      selectOrderPayments: [],//支付列表
    }
  }

  componentDidMount() {
    if(this.props.tid){
      this.getSelectOutsourceLog();
      this.getSelectOrderPayment();
    }
  }

  onChange() {
    this.setState({
      value: e.target.value,
    })
  }

  tableRowClickOne(record) {}

  //点击付款节点详情
  payNodeTableRowClickOne(record) {}

  //外包日志列表
  getSelectOutsourceLog(){
    $.ajax({
      method: 'get',
      dataType: 'json',
      crossDomain: false,
      url: globalConfig.context + '/api/admin/outsourceOrg/selectOutsourceLog',
      data: {
        tid: this.props.tid,
      },
      success: function (data) {
        if (data.error.length) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
          }
        } else {
          this.setState({
            outsourceLogs: data.data
          })
        }
      }.bind(this),
    }).always(
        function () {

        }.bind(this)
    )
  }

  //支付列表
  getSelectOrderPayment() {
    $.ajax({
      method: 'get',
      dataType: 'json',
      crossDomain: false,
      url: globalConfig.context + '/api/admin/company/selectOrderPayment',
      data: {
        id: this.props.tid,
      },
      success: function (data) {
        if (data.error.length) {
          if (data.error && data.error.length) {
            message.warning(data.error[0].message);
          }
        } else {
          this.setState({
            selectOrderPayments: data.data
          })
        }
      }.bind(this),
    }).always(
        function () {

        }.bind(this)
    )
  }

  render() {
    return (
      <div className="App">
        <div className="projectType">
          <div className="typeTitle">
            <div className="required">*</div>
            <div>类型:</div>
          </div>
          {
            parseInt(this.props.startType) === 0 ?
                <div>
                  <div>供应商信息(外包派单,不走总部)</div>
                  <div className="tipsText">提示高于总部价格,费用个人承担</div>
                </div> :
                <div>供应商信息(普通单)</div>
          }
        </div>
        <div className="thirdParty">
          <div>
            <span className="title">
              第三方信息
            </span>
          </div>
          <div
            className="clearfix"
          >
            <Spin spinning={this.state.loading}>
              <Form layout="horizontal">
                <Table
                  pagination={false}
                  columns={this.state.ContactsListsNew}
                  dataSource={this.props.thirdInfoList}
                  onRowClick={this.tableRowClickOne}
                  scroll={{ x: 'max-content', y: 0 }}
                  bordered
                  size="small"
                />
                <Col span={24} offset={9} style={{ marginTop: '15px' }}/>
              </Form>
            </Spin>
          </div>
        </div>
        <div className="thirdParty">
          <div>
            <span className="title">
              付款节点
            </span>
          </div>
          <div
            className="clearfix"
          >
            <Spin spinning={this.state.loading}>
              <Form layout="horizontal">
                <Table
                  pagination={false}
                  columns={this.state.PayNodeTableColunms}
                  dataSource={this.props.dataSource}
                  onRowClick={this.payNodeTableRowClickOne}
                  scroll={{ x: 'max-content', y: 0 }}
                  bordered
                  size="small"
                />
                <Col span={24} offset={9} style={{ marginTop: '15px' }}/>
              </Form>
            </Spin>
          </div>
        </div>
        <div
          style={{
            borderTop: '1px #000000 dashed',
            padding: '20px 20px 0px 20px',
          }}
        >
          <div>
            <div className="title">备注</div>
            <Input
                type="textarea"
                placeholder="请输入备注"
                rows={4}
                value={this.props.outsourceRemarks}
            />
          </div>
          <div style={{paddingTop:'20px'}}>
            <div className="title">合同/协议扫描件</div>
            <Upload
                className="demandDetailShow-upload"
                listType="picture-card"
                fileList={this.props.fileList}
                onPreview={(file) => {
                  this.setState({
                    previewImage: file.url || file.thumbUrl,
                    previewVisible: true,
                  })
                }}
            />
            <Modal
                maskClosable={false}
                footer={null}
                visible={this.state.previewVisible}
                onCancel={() => {
                  this.setState({ previewVisible: false })
                }}
            >
              <img
                  alt=""
                  style={{ width: '100%' }}
                  src={this.state.previewImage || ''}
              />
            </Modal>
          </div>
        </div>
        <div className="outsourceLogConent">
          <div className="title">外包状态:</div>
          <div className="outsourceLogList">
            {
              this.state.outsourceLogs.map((value,index)=>(
                  <div key={index} className="outsourceLogItem">
                    <Row gutter={16}>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          {value.aname}
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          {
                            value.status === 0 ? '发起外包审核' :
                                value.status === 1 ? '通过' : '驳回'
                          }
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          {value.remarks}
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          {value.createTimes}
                        </div>
                      </Col>
                    </Row>
                  </div>
              ))
            }
          </div>
        </div>
        <div className="outsourceLogConent">
          <div className="title">支付记录:</div>
          <div className="outsourceLogList">
            {
              this.state.selectOrderPayments.map((value,index)=>(
                  <div key={index} className="outsourceLogItem">
                    <Row gutter={16}>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                            编号
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          订单编号
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          支付类型
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          申请支付金额(万元)
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          申请时间
                        </div>
                      </Col>
                      <Col className="gutter-row" span={6}>
                        <div className="gutter-box">
                          状态
                        </div>
                      </Col>
                    </Row>
                  </div>
              ))
            }
          </div>
        </div>
        {/*
            申请付款
            tid:项目id
            nodeId: 付款节点id
            previewPayInfor: 项目或者节点信息
         */}
        <ApplyPaymentModal
            {...this.props}
            tid={this.props.tid}
            nodeId={this.state.nodeId}
            previewPayInfor={this.state.previewPayInfor}
            visible={this.state.previewPayVisible}
            changeVisible={()=>{
              this.setState({ previewPayVisible: false })
            }}/>
      </div>
    )
  }
}

export default CheckProject
