/*
 * Author: 黎永顺
 * name: 在线申报-缴款反馈按钮
 * Description:
 * Date: 2023/8/22
 */
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import { inject, observer } from "mobx-react";
import PaymentDialog from "./paymentDialog";
import PaymentFeedBackDetail from "./paymentFeedBackDetail";
import { paymentFeedbackConditions } from "./constants";
import { convertToUrlString, getQueryString } from "../../../util/url";
import { taxPaymentAgreementPayFeedback, taxPaymentVoucherPrintFeedback } from "../../../apis/declare";

const { getLabel } = WeaLocaleProvider;

@inject("declareStore")
@observer
class PaymentFeedbackBtn extends Component {
  constructor(props) {
    super(props);
    this.state = {
      paymentDialog: {
        visible: false, title: "",
        buttons: [], conditions: null
      },
      paymentFeedbackDialog: {
        visible: false, title: "", showData: {}
      },
      loading: false
    };
  }

  getFeedback = () => {
    const { declareStore: { paymentForm } } = this.props;
    paymentForm.validateForm().then(f => {
      const { type } = paymentForm.getFormParams();
      if (f.isValid) {
        if (type === "WITHHOLDING_PAY") {
          // 三方缴款 弹窗，打印新开页面
          this.taxPaymentAgreementPayFeedback();
        } else {
          this.taxPaymentVoucherPrintFeedback();
        }
      } else {
        f.showErrors();
      }
    });
  };
  taxPaymentAgreementPayFeedback = () => {
    const { taxCycle: taxYearMonth, taxAgentId } = this.props.declareInfo;
    const payload = {
      taxDeclareRecordId: getQueryString("id"),
      taxAgentId, taxYearMonth: taxYearMonth + "-01"
    };
    this.setState({
      paymentDialog: {
        ...this.state.paymentDialog,
        buttons: [
          <Button type="primary" loading={true}>{getLabel(111, "获取反馈")}</Button>
        ]
      }
    });
    taxPaymentAgreementPayFeedback(payload).then(({ status, data, errormsg }) => {
      this.setState({
        paymentDialog: {
          ...this.state.paymentDialog,
          buttons: [
            <Button type="primary" loading={false} onClick={this.getFeedback}>{getLabel(111, "获取反馈")}</Button>
          ]
        }
      });
      if (status) {
        this.handleCancel();
        this.setState({
          paymentFeedbackDialog: {
            ...this.state.paymentFeedbackDialog,
            visible: true, title: getLabel(111, "三方缴款详情"),
            showData: data
          }
        });
      } else {
        message.error(errormsg || getLabel(111, "缴款反馈失败！"));
      }
    }).catch(() => {
      this.setState({
        paymentDialog: {
          ...this.state.paymentDialog,
          buttons: [
            <Button type="primary" loading={false} onClick={this.getFeedback}>{getLabel(111, "获取反馈")}</Button>
          ]
        }
      });
    });
  };
  taxPaymentVoucherPrintFeedback = () => {
    const { taxCycle: taxYearMonth, taxAgentId } = this.props.declareInfo;
    const payload = {
      taxYearMonth: taxYearMonth + "-01",
      taxDeclareRecordId: getQueryString("id"),
      taxAgentId, checkFeedback: 1
    };
    this.setState({
      paymentDialog: {
        ...this.state.paymentDialog,
        buttons: [
          <Button type="primary" loading={true}>{getLabel(111, "获取反馈")}</Button>
        ]
      }
    });
    taxPaymentVoucherPrintFeedback(payload).then(({ status, data, errormsg }) => {
      this.setState({
        paymentDialog: {
          ...this.state.paymentDialog,
          buttons: [
            <Button type="primary" loading={false} onClick={this.getFeedback}>{getLabel(111, "获取反馈")}</Button>
          ]
        }
      });
      if (status) {
        this.handleCancel();
        window.open(`${window.ecologyContentPath || ""}/spa/hrmSalary/static/index.html#/main/hrmSalary/bankVoucherDetail?${convertToUrlString(payload)}`);
      } else {
        message.error(errormsg || getLabel(111, "缴款反馈失败！"));
      }
    }).catch(() => {
      this.setState({
        paymentDialog: {
          ...this.state.paymentDialog,
          buttons: [
            <Button type="primary" loading={false} onClick={this.getFeedback}>{getLabel(111, "获取反馈")}</Button>
          ]
        }
      });
    });
  };
  handlePaymentFeedback = () => {
    const { paymentDialog } = this.state;
    this.setState({
      paymentDialog: {
        ...paymentDialog, visible: true,
        title: getLabel(111, "获取反馈"),
        buttons: [
          <Button type="primary" onClick={this.getFeedback}>{getLabel(111, "获取反馈")}</Button>
        ],
        conditions: _.map(paymentFeedbackConditions, item => {
          return {
            ...item,
            items: _.map(item.items, it => {
              return {
                ...it,
                options: [
                  { key: "WITHHOLDING_PAY", showname: getLabel(111, "三方缴款"), selected: true },
                  // { key: "WITHHOLDING_VOUCHER", showname: getLabel(111, "缴款凭证打印") }
                ]
              };
            })
          };
        })
      }
    });
  };
  handleCancel = () => {
    const { paymentDialog } = this.state;
    this.setState({
      paymentDialog: {
        ...paymentDialog, visible: false,
        title: "", buttons: [], conditions: null
      }
    });
  };

  render() {
    const { paymentDialog, paymentFeedbackDialog } = this.state;
    return (
      <React.Fragment>
        {/*个税接口改造*/}
        {/*<Button type="ghost" onClick={this.handlePaymentFeedback}>{getLabel(111, "缴款反馈")}</Button>*/}
        <Button type="ghost" onClick={this.taxPaymentAgreementPayFeedback}>{getLabel(111, "缴款反馈")}</Button>
        <PaymentDialog
          {...paymentDialog}
          onCancel={this.handleCancel}
        />
        <PaymentFeedBackDetail
          {...paymentFeedbackDialog}
          onCancel={() => {
            this.setState({
              paymentFeedbackDialog: {
                ...paymentFeedbackDialog,
                visible: false, title: "", showData: {}
              }
            }, () => this.props.updateDeclare());
          }}
        />
      </React.Fragment>
    );
  }
}

export default PaymentFeedbackBtn;
