import React, { Component } from "react";
import {
  Form,
  Button,
  message,
  Modal,
  Spin,
  Input,
  DatePicker,
  Radio,
  Popconfirm
} from "antd";
import ImgList from "../../../common/imgList";
import $ from "jquery/src/ajax";
const { TextArea } = Input;
const RadioGroup = Radio.Group;
class legalAdd extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      mainModalKeys: 0,
      type: "",
      date: "",
      remarks: "",
      loading: false,
    };
    this.addLegal = this.addLegal.bind(this);
  }
  addLegal() {
    let theorgCodeUrl = [];
    if (!this.state.fileList) {
      message.warning("请上传凭证");
      return;
    } else {
      let picArr = [];
      this.state.fileList.map(function (item) {
        if (item.response && item.response.data && item.response.data.length) {
          picArr.push(item.response.data);
        }
      });
      theorgCodeUrl = picArr.join(",");
    }
    if (!this.state.date) {
      message.warning("请选择时间");
      return;
    }
    this.setState({
      addDunOrderLoading: true,
      loading: true,
    });
    $.ajax({
      method: "post",
      dataType: "json",
      crossDomain: false,
      url: globalConfig.context + "/api/admin/lagalAffairs/addLagalAffairsLog",
      data: {
        orderNo: this.props.orderNo,
        type: this.state.type,
        status: this.state.status ? this.state.status : 0 ,
        date: this.state.date,
        remarks: this.state.remarks,
        attachmentUrl: theorgCodeUrl.length ? theorgCodeUrl : "",
      },
      success: function (data) {
        if (data.error.length) {
          this.setState({
            loading: false,
          });
          message.warning(data.error[0].message);
        } else {
          this.setState({
            loading: false,
          });

          this.props.modelBack();
          // this.props.visible = false
          message.success("添加成功");
          this.tabelContentRef && this.tabelContentRef.onRefresh();
        }
      }.bind(this),
    }).done(
      function () {
        this.setState({
          addDunOrderLoading: false,
        });
      }.bind(this)
    );
  }
  render() {
    // const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: { span: 10 },
      wrapperCol: { span: 12 },
    };
    return (
      <div>
        <Modal
          maskClosable={false}
          visible={this.props.visibles}
          onOk={this.props.onOk}
          onCancel={this.props.onCancel}
          key={this.state.mainModalKeys}
          width="900px"
          title="新增法务数据"
          footer=""
          className="admin-desc-content"
        >
          <Form onSubmit={this.handleSubmit} ref={this.formRef}>
            <Form.Item
              {...formItemLayout}
              label="法务类型"
              name="type"
              hasFeedback
            >
              <RadioGroup
                onChange={(e) => {
                  this.setState({
                    type: e.target.value,
                  });
                }}
              >
                <Radio value="0">法务催收中</Radio>
                <Radio value="1">延期法务</Radio>
                <Radio value="2">法务已完成</Radio>
              </RadioGroup>
            </Form.Item>
            {this.state.type == 2 ? (
              <Form.Item
                {...formItemLayout}
                label="完成状态"
                name="status"
                hasFeedback
              >
                <RadioGroup
                  onChange={(e) => {
                    this.setState({
                      status: e.target.value,
                    });
                  }}
                >
                  <Radio value="1">已回款</Radio>
                  <Radio value="2">以诉讼</Radio>
                  <Radio value="3">以坏账处理</Radio>
                </RadioGroup>
              </Form.Item>
            ) : null}
            <Form.Item
              name="date"
              {...formItemLayout}
              label="延期时间"
              hasFeedback
            >
              <DatePicker
                onChange={(date, dateString) => {
                  this.setState({
                    date: dateString,
                  });
                }}
              />
            </Form.Item>
            <Form.Item
              name="remarks"
              {...formItemLayout}
              label="法务情况"
              hasFeedback
            >
              <TextArea
                rows={4}
                onChange={(e) => {
                  this.setState({
                    remarks: e.target.value,
                  });
                }}
              />
            </Form.Item>
            <Form.Item
              name="fileList"
              {...formItemLayout}
              label="附件"
              hasFeedback
            >
              <ImgList
                accept="application/pdf,image/png,application/msword"
                domId="nowProjectStatus"
                uploadConfig={{
                  action:
                    globalConfig.context +
                    "/api/admin/newOrder/uploadDunLogFile",
                  data: { sign: "dun_log_attachment" },
                  multiple: true,
                  listType: "text",
                }}
                onChange={(info) => {
                  let fileList = info.fileList;
                  this.setState({ fileList });
                }}
                fileList={this.state.fileList}
              />
            </Form.Item>
            <div style={{ display: "flex" }}>
              <Button
                type="primary"
                loading={this.state.loading}
              >
                <Popconfirm
                  title="您确定此订单提交法务数据?"
                  onConfirm={this.addLegal}
                  onCancel={()=>{
                    this.setState({
                      loading:false
                    })
                  }}
                  okText="确定"
                  cancelText="取消"
                  loading={this.state.loading}
                >
                  <a href="#">提交</a>
                </Popconfirm>
              </Button>
              <Button
                type="dashed"
                style={{ marginLeft: "15px" }}
                onClick={this.props.onCancel}
              >
                取消
              </Button>
            </div>
          </Form>
        </Modal>
      </div>
    );
  }
}
export default legalAdd;
