import React, { Component } from "react";
import { Form, Upload, Modal, Input, Button, message, Select, Table } from "antd";
import AgreeButton from "./agreeButton.js";
import Rizhi from "./rizhi.js";
import { getProcessStatusNew, changeColor } from "@/tools.js";
import { shenghePeo } from "@/tools.js";
import $ from "jquery/src/ajax";
import tongguo from "../../../../../../image/tongguo.png";
import quxiao from "../../../../../../image/quxiao.png";
import ProAndCuiList from "./proAndCuiList.jsx";
import "./xButton.less";
import Refund from "./refund.js"
import ImgList from "../../../../common/imgList";
// import ReactToPrint from "react-to-print";

const formItemLayout = {
  labelCol: { span: 8 },
  wrapperCol: { span: 14 }
};

const changeType = [
  {
    value: "0",
    key: "退单退款"
  },
  {
    value: "1",
    key: "项目及金额变更"
  },
  {
    value: "2",
    key: "仅项目变更"
  },
  {
    value: "3",
    key: "仅金额变更"
  },
  {
    value: "4",
    key: "重报"
  },
  {
    value: "5",
    key: "赠送"
  }
];

const getChangeType = function (e) {
  if (e || e == 0) {
    let str = e.toString();
    let theType = "";
    changeType.map(function (item) {
      if (item.value == str) {
        theType = item.key;
      }
    });
    return theType;
  }
};

class Itemlist extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    let pageData = this.props.pageData;
    return (
      <div className="clearfix">
        <Form.Item
          label={pageData.labelA}
          {...pageData.formItemLayoutA}
          className="half-item"
        >
          <p style={{ width: 350, wordWrap: "break-word" }}>{pageData.dataA}</p>
        </Form.Item>
        <Form.Item
          label={pageData.labelB}
          {...pageData.formItemLayoutB}
          className="half-item"
        >
          <span>{pageData.dataB}</span>
        </Form.Item>
      </div>
    );
  }
}

class ChangeDetail extends Component {
  constructor(props) {
    super(props);
    this.state = {
      rotateDeg: 0,
      previewVisible: false,
      buttonStatus: true,
      changeType: 1,
      dataSource: [],
      reason: "",
      // 累计回收退票
      refundInvoice: 0,
      // 退票数组
      contactList: [],
      rejectState: 0,
      // 收款信息
      proceedsData: [],
      proceedsTotal: this.props.proceedsTotal,
      // 开票信息
      invoiceData: [],
      invoiceTotal: this.props.invoiceTotal,
      cost: 10,
      refund: 8,
      // 测试添加退票
      contactList: this.props.contactList,
      ContactsLists: [
        {
          title: "金额(万元)",
          dataIndex: "amount",
          key: "amount",
          render: (text, record) => {
            return (
              <div>
                实际回收退票(万元)
                <Input
                  value={record.amount}
                  placeholder="请输入金额(必填项)"
                  disabled={record.load}
                  key={record.id}
                  required="required"
                  onChange={(e) => {
                    record.amount = e.target.value;
                    this.setState({ contactList: this.state.contactList });
                  }}
                  style={{ width: "120px" }}
                />
              </div>
            );
          },
        },
        {
          title: "时间",
          dataIndex: "createTimes",
          key: "createTimes",
          render: (text, record) => {
            return <div>{record.createTimes}</div>;
          },
        },
      ],
    };
    this.getOrgCodeUrl = this.getOrgCodeUrl.bind(this);
    this.changeButtonStatus = this.changeButtonStatus.bind(this);
    this.moneyOld = this.moneyOld.bind(this);
    this.rotate = this.rotate.bind(this);
    this.downImg = this.downImg.bind(this);
    this.upImg = this.upImg.bind(this);
  }

  componentWillReceiveProps(nextProps) {
    this.setState({
      projectState: nextProps.data.projectState,
      zxsCost: nextProps.data.zxsCost,
      zxsRemarks: nextProps.data.zxsRemarks,
      paymentTimes: nextProps.data.paymentTimes,
      paymentAmount: nextProps.data.paymentAmount,
      invoiceTimes: nextProps.data.invoiceTimes,
      invoiceAmount: nextProps.data.invoiceAmount,
      cwCost: nextProps.data.cwCost,
      refundableAmount: nextProps.data.refundableAmount,
      cwRemarks: nextProps.data.cwRemarks,
      contactList: nextProps.contactList,
      refundInvoice: nextProps.data.refundInvoice,
    });
  }

  getOrgCodeUrl(e) {
    this.setState({
      orgCodeUrl: e,
    });
  }

  //同意拒绝按钮的有无
  changeButtonStatus() {
    this.setState({
      buttonStatus: !this.state.buttonStatus,
    });
    window.setTimeout(() => {
      this.setState({
        reason: "",
      });
    }, 100);
  }

  downImg() {
    let num = 0;
    for (let i = 0; i < this.props.pictureUrl.length; i++) {
      if (this.props.pictureUrl[i].url == this.state.previewImage) {
        num = i;
      }
    }
    if (num == this.props.pictureUrl.length - 1) {
      return message.warning("已经是最后一张了哦");
    }
    this.state.previewImage = this.props.pictureUrl[num + 1].url;
    this.setState({
      previewImage: this.state.previewImage,
      rotateDeg: 0,
    });
  }
  upImg() {
    let num = 0;
    for (let i = 0; i < this.props.pictureUrl.length; i++) {
      if (this.props.pictureUrl[i].url == this.state.previewImage) {
        num = i;
      }
    }
    if (num == 0) {
      return message.warning("已经是第一张了哦");
    }
    this.state.previewImage = this.props.pictureUrl[num - 1].url;
    this.setState({
      previewImage: this.state.previewImage,
      rotateDeg: 0,
    });
  }
  rotate() {
    let rotateDeg = this.state.rotateDeg + 90;
    this.setState({
      rotateDeg,
    });
  }

  moneyOld(str, money) {
    if (money) {
      return (
        <span style={{ color: "red", fontWeight: 900, fontSize: 16 }}>
          {str}(原合同金额{money}万元)
        </span>
      );
    } else {
      return (
        <span style={{ color: "red", fontWeight: 900, fontSize: 16 }}>
          {str}
        </span>
      );
    }
  }

  render() {
    const pageData = [
      {
        labelA: "客户名称",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.userName,
        labelB: "合同编号",
        formItemLayoutB: formItemLayout,
        dataB: this.props.data.contractNo,
      },
      {
        labelA: "时间",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.createTimes,
        labelB: "发起人",
        formItemLayoutB: formItemLayout,
        dataB: this.props.data.applicant,
      },
      {
        labelA: "当前进度",
        formItemLayoutA: formItemLayout,
        // 当前是咨询师以及咨询师经理时使用shenhePeo函数,其他流程正常显示
        dataA:
          this.props.data.processState == 2 || this.props.data.processState == 3
            ? shenghePeo(
                this.props.data.processState == 2
                  ? this.props.data.consultantExamine
                  : this.props.data.managerExamine,
                this.props.data.processState
              )
            : getProcessStatusNew(
                this.props.data.processState,
                this.props.data.status
              ),
        labelB: "变更类型",
        formItemLayoutB: formItemLayout,
        dataB: getChangeType(this.props.data.type),
      },
      {
        labelA: "合同额(万元)",
        formItemLayoutA: formItemLayout,
        dataA: this.moneyOld(this.props.data.totalAmount, this.props.money),
        labelB: "已收款(万元)",
        formItemLayoutB: formItemLayout,
        dataB: changeColor(this.props.data.settlementAmount),
      },
      {
        labelA: "欠款(万元)",
        formItemLayoutA: formItemLayout,
        dataA: changeColor(this.props.data.arrears),
        labelB: "申请退款(万元)",
        formItemLayoutB: formItemLayout,
        dataB: changeColor(this.props.data.changeAmount),
      },
      {
        labelA: "变更原因",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.remarks,
      },
    ];
    const planData = [
      {
        labelA: "项目进度",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.projectState,
        onChangeA: (value) => {
          this.setState({
            projectState: value,
          });
        },
        labelB: "发生成本费用(万元)",
        formItemLayoutB: formItemLayout,
        dataB: this.props.data.zxsCost,
        onChangeB: (value) => {
          this.setState({
            zxsCost: value,
          });
        },
      },
      {
        labelA: "备注",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.zxsRemarks,
        onChangeA: (value) => {
          this.setState({
            zxsRemarks: value,
          });
        },
      },
    ];
    const financeData = [
      {
        labelA: "发生成本(万元)",
        formItemLayoutA: formItemLayout,
        dataA: this.props.data.cwCost,
        labelB: "应退金额(万元)",
        formItemLayoutB: formItemLayout,
        dataB: this.props.data.refundableAmount,
      },
    ];
    const buttonData = [
      {
        name: "同意",
        title: "理由",
        placeholder: "请输入理由",
        type: "primary",
        status: 2,
        onChange: (value) => {
          this.setState({
            remarks: value,
          });
        },
      },
      {
        name: "拒绝",
        title: "理由",
        placeholder: "请输入理由",
        type: "danger",
        status: 3,
        onChange: (value) => {
          this.setState({
            remarks: value,
          });
        },
      },
    ];
    const orgCodeUrl = this.props.pictureUrl;

    return (
      <div className="clearfix changeDetail">
        <div
          className="clearfix"
          ref={(e) => {
            this.refs = e;
          }}
        >
          <div
            style={{
              borderRadius: "50%",
              width: 300,
              height: 300,
              position: "absolute",
              top: 230,
              left: 525,
              transform: "rotate(-5deg)",
              zIndex: 1,
              display:
                (this.props.data.processState == 7 ||
                  this.props.data.processState == 8) &&
                this.props.data.status == 4
                  ? "block"
                  : "none",
            }}
          >
            {/* <span
              style={{
                fontSize: 29,
                position: "absolute",
                left: "50%",
                top: "50%",
                transform: "translate(-50%,-50%)",
                color: "red"
              }}
            >
              通过
            </span> */}
            <img src={tongguo} style={{ width: "100%" }} />
          </div>
          <div
            style={{
              borderRadius: "50%",
              width: 300,
              height: 300,
              position: "absolute",
              top: 284,
              left: 586,
              transform: "rotate(-5deg)",
              zIndex: 1,
              display: this.props.data.status == 5 ? "block" : "none",
            }}
          >
            <img src={quxiao} style={{ width: "63%" }} />
          </div>

          <h2 style={{ textAlign: "center", marginBottom: 10 }}>
            合同变更记录
          </h2>
          {pageData.map((item, index) => {
            return <Itemlist key={index} pageData={item} />;
          })}
          <Form.Item
            label="变更凭证"
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 18 }}
          >
            <Upload
              className="demandDetailShow-upload"
              listType="picture-card"
              fileList={orgCodeUrl}
              onPreview={(file) => {
                this.setState({
                  previewImage: file.url || file.thumbUrl,
                  previewVisible: true,
                });
              }}
            />
            {/*<div style={{paddingTop:'10px',paddingBottom:'10px'}}>*/}
            {/*  <ImgList fileList={orgCodeUrl} ItemWidth={'96px'}/>*/}
            {/*</div>*/}
            <Modal
              maskClosable={false}
              footer={null}
              width={"50%"}
              visible={this.state.previewVisible}
              onCancel={() => {
                this.state.rotateDeg = 0;
                this.setState({
                  previewVisible: false,
                  rotateDeg: this.state.rotateDeg,
                });
              }}
            >
              <img
                alt=""
                style={{
                  width: "100%",
                  transform: `rotate(${this.state.rotateDeg}deg)`,
                }}
                src={this.state.previewImage || ""}
              />
              <Button
                onClick={this.rotate}
                style={{
                  position: "relative",
                  left: "50%",
                  transform: "translateX(-50%)",
                }}
              >
                旋转
              </Button>
              <Button
                onClick={this.upImg}
                style={{
                  position: "absolute",
                  left: -81,
                  top: "50%",
                  transform: "translateY(-50%)",
                }}
              >
                上一张
              </Button>
              <Button
                onClick={this.downImg}
                style={{
                  position: "absolute",
                  right: -81,
                  top: "50%",
                  transform: "translateY(-50%)",
                }}
              >
                下一张
              </Button>
            </Modal>
            <Rizhi changeId={this.props.id} />
          </Form.Item>

          <ul
            style={{
              width: "868px",
              overflow: "hidden",
              paddingLeft: "90px",
              position: "relative",
              bottom: "0px",
            }}
          >
            {/* <span>操作人:</span> */}
            {this.props.dataSource.map((item, index) => {
              if (item.status == 0) {
                item.status = "发起";
              } else if (item.status == 1) {
                item.status = "审核中";
              } else if (item.status == 2) {
                item.status = "通过";
              } else if (item.status == 3) {
                item.status = "驳回";
              } else if (item.status == 4) {
                item.status = "完成";
              }
              return (
                <li
                  key={index}
                  style={{
                    width: "100%",
                    height: "auto",
                    wordBreak: "break-all",
                    marginBottom: "10px",
                  }}
                >
                  {item.aname + ":" + "(" + item.status + ")" + item.remarks}
                </li>
              );
            })}
          </ul>

          <ProAndCuiList id={this.props.id} />

          <div className="clearfix">
            <h3 style={{ marginLeft: 20, fontWeight: 800, marginBottom: 10 }}>
              收款情况
            </h3>
            <div style={{ marginLeft: "90px" }}>
              <ul>
                {(this.props.proceedsData.bill || []).map((item, index) => (
                  <li key={index}>
                    {item.payTime +
                      " " +
                      item.aName +
                      " 收款:" +
                      item.transactionAmount +
                      "万元"}
                  </li>
                ))}
              </ul>

              <div>收款总计:{this.props.proceedsTotal}万元</div>
            </div>
            <br />
            <div style={{ marginLeft: "90px" }}>
              <ul>
                {(this.props.proceedsData.invoice || []).map((item, index) => (
                  <li key={index}>
                    {item.createTime + " " + " 开票:" + item.amount + "万元"}
                  </li>
                ))}
              </ul>
              <div>开票总计:{this.props.invoiceTotal}万元</div>
            </div>

            <p style={{ marginLeft: "90px", color: "red" }}>
              系统预估 产生成本(万元):
              {this.props.data.estimateCost
                ? this.props.data.estimateCost
                : "0"}{" "}
              &nbsp;应退金额(万元):
              {this.props.data.estimateRefundable
                ? this.props.data.estimateRefundable
                : "0"}
            </p>
          </div>

          {/* {this.props.data.type === 0 ? (
            <div>
              <h3 style={{ marginLeft: 10, fontWeight: 800, marginBottom: 10 }}>
                当前项目进度(咨询师经理填写)
              </h3>
              {planData.map((item, index) => {
                return <Itemlist key={index} pageData={item} />;
              })}
            </div>
          ) : (
            ""
          )} */}

          <div
            style={{
              display:
                (this.props.data.processState == 8 ||
                  this.props.data.processState == 7) &&
                (this.props.data.status == 2 || this.props.data.status == 4)
                  ? "block"
                  : "none",
            }}
          >
            <h3 style={{ marginLeft: 20, fontWeight: 800, marginBottom: 10 }}>
              回收退票
            </h3>

            <Table
              size="middle"
              pagination={false}
              bordered
              columns={this.state.ContactsLists}
              dataSource={this.state.contactList}
            />
          </div>

          <div
            style={{
              display: this.props.data.processState > 5 ? "block" : "none",
            }}
          >
            <h3 style={{ marginLeft: 20, fontWeight: 800, marginBottom: 10 }}>
              当前财务状态(财务填写)
            </h3>
            {financeData.map((item, index) => {
              return <Itemlist key={index} pageData={item} />;
            })}
          </div>

          {/*

            (
            <div>
              <h3 style={{ marginLeft: 10, fontWeight: 800, marginBottom: 10 }}>
                当前项目进度(咨询师经理填写)
              </h3>
              {planData.map((item, index) => {
                return <Itemlist key={index} pageData={item} />;
              })}
            </div>
          )


          */}
          {/* {this.props.data.processState >= 4 ? (
            ""
          ) : (
            <div style={{ marginBottom: 10 }}>
              <div className="clearfix">
                <h3
                  style={{ marginLeft: 10, fontWeight: 800, marginBottom: 10 }}
                >
                  当前项目进度(咨询师经理填写)
                </h3>
                <Form.Item
                  className="half-item"
                  label="项目进度"
                  labelCol={{ span: 8 }}
                  wrapperCol={{ span: 14 }}
                >
                  <Input
                    style={{ width: 160 }}
                    value={this.state.projectState}
                    placeholder="请输入项目进度"
                    onChange={e => {
                      this.setState({
                        projectState: e.target.value
                      });
                    }}
                  />
                </Form.Item>
                <Form.Item
                  className="half-item"
                  label="发生成本费用(万元)"
                  labelCol={{ span: 8 }}
                  wrapperCol={{ span: 14 }}
                >
                  <Input
                    style={{ width: 160 }}
                    value={this.state.zxsCost}
                    placeholder="请输入发生成本费用"
                    onChange={e => {
                      this.setState({
                        zxsCost: e.target.value
                      });
                    }}
                  />
                </Form.Item>
              </div>
            </div>
          )} */}

          {/* {this.props.data.processState >= 5 ? (
            <div>
              <h3 style={{ marginLeft: 10, fontWeight: 800, marginBottom: 10 }}>
                当前财务状态(财务填写)
              </h3>
              {financeData.map((item, index) => {
                return <Itemlist key={index} pageData={item} />;
              })}
            </div>
          ) : (
            ""
          )} */}
          <div
            style={{
              display:
                (this.props.data.processState == 7 ||
                  this.props.data.processState == 8) &&
                this.props.data.status == 4 &&
                this.props.data.refundStatus
                  ? "block"
                  : "none",
            }}
          >
            <Refund data={this.props.data} id={this.props.id} />
          </div>
        </div>
        {this.props.processState == this.props.data.processState ? (
          1 ? (
            <div
              style={{
                display: this.props.data.isExamine == 0 ? "block" : "none",
              }}
            >
              <div style={{ marginBottom: "10px" }}>
                <Form.Item
                  label="备注"
                  labelCol={{ span: 4 }}
                  wrapperCol={{ span: 14 }}
                >
                  <Input.TextArea
                    value={this.state.reason}
                    rows={4}
                    placeholder="请输入备注"
                    onChange={(e) => {
                      let reason = e.target.value;
                      this.setState({ reason });
                    }}
                  />
                </Form.Item>
              </div>

              <div
                className="clearfix"
                style={{ display: "flex", justifyContent: "space-around" }}
              >
                {buttonData.map((item, index) => {
                  return (
                    <AgreeButton
                      data={item}
                      key={index}
                      backData={this.props.data}
                      processState={this.props.processState}
                      visible={this.changeButtonStatus}
                      loadData={this.props.loadData}
                      reason={this.state.reason}
                      ajaxData={this.state}
                      rejectState={this.state.rejectState}
                      onCancel={this.props.onCancel}
                    />
                  );
                })}
              </div>

              <Select
                defaultValue="0"
                style={{
                  width: 120,
                  float: "left",
                  position: "relative",
                  bottom: "28px",
                  right: "-600px",
                }}
                onChange={(val) => {
                  this.setState({ rejectState: val });
                }}
              >
                <Option
                  value="0"
                  style={{
                    display: this.props.processState >= 1 ? "block" : "none",
                  }}
                >
                  营销员
                </Option>
                <Option
                  value="1"
                  style={{
                    display: this.props.processState >= 2 ? "block" : "none",
                  }}
                >
                  营销管理员
                </Option>
                <Option
                  value="2"
                  style={{
                    display: this.props.processState >= 3 ? "block" : "none",
                  }}
                >
                  技术员
                </Option>
                <Option
                  value="3"
                  style={{
                    display: this.props.processState >= 4 ? "block" : "none",
                  }}
                >
                  技术经理
                </Option>
                <Option
                  value="4"
                  style={{
                    display: this.props.processState >= 5 ? "block" : "none",
                  }}
                >
                  技术总监
                </Option>
                <Option
                  value="5"
                  style={{
                    display: this.props.processState >= 6 ? "block" : "none",
                  }}
                >
                  财务专员(退单)
                </Option>
                <Option
                  value="6"
                  style={{
                    display: this.props.processState >= 7 ? "block" : "none",
                  }}
                >
                  财务总监
                </Option>
                <Option
                  value="7"
                  style={{
                    display: this.props.processState >= 8 ? "block" : "none",
                  }}
                >
                  总裁
                </Option>
              </Select>
            </div>
          ) : (
            ""
          )
        ) : (
          ""
        )}
        {/* <ReactToPrint
          trigger={() => (
            <div className="clearfix">
              <Button type="primary" style={{ float: "right", marginTop: 10 }}>
                打印
              </Button>
            </div>
          )}
          content={() => this.refs}
        /> */}
      </div>
    );
  }
}

export { ChangeDetail };