/*
 * Creater:     柿子
 * CreateDate:  2022/04/14
 * Describe：   扫码确认弹窗
 * */
import React from 'react';
import { Modal, Input, message, Button } from 'antd';

class ScanCodeModal extends React.Component {
  constructor(props) {
    super(props);
    var userData = JSON.parse(sessionStorage.getItem('userData'));
    this.state = {
      userData,
      visible: false,
      authcode: undefined,
      chargeRecodeId: '',
      okLoading: false
    };
    this.myRef = React.createRef();
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  }

  // 修改弹窗状态
  modifyVisible = (visible, chargeRecodeId, cancelLoading, cleanFlag) => {
    if (cancelLoading === 'Y') {
      this.setState({ okLoading: false })
    }
    if (chargeRecodeId !== undefined) { // 交款记录id
      this.setState({ chargeRecodeId })
    }
    this.setState({ visible }, () => {
      if (this.state.visible) {
        setTimeout(() => {
          this.myRef.current && this.myRef.current.focus && this.myRef.current.focus();
        }, 300)
      }
    })
    if (cleanFlag === 'Y') {
      this.setState({
        authcode: undefined
      })
    }
  }

  // 确认
  handleOk = () => {
    this.callDynamicLibrary();
  }

  // 调用动态库
  callDynamicLibrary = () => {
    let { type } = this.props
    let { userData, authcode, chargeRecodeId } = this.state;
    this.setState({ okLoading: true })
    let dllInputdata = {
      params: [{
        userID: userData.userID || '',
        payrecordinfo: chargeRecodeId, //交款记录id
        hospID: userData.hospID, //医院id
        groupID: userData.groupID,  //角色id
        paytype: type ? type : 'O',
        authcode
      }],
      code: "99070020"
    }
    var dllPath = "ReadYBCard/GZNYYHPay.dll";
    let className = 'GZNYYHPay.GZNYYHPay';
    var funcName = "SMPay";
    let dllParams = "{'inputDir':'" + dllPath + "','clsName':'" + className + "','funcName':'" + funcName + "','dllInput':'" + JSON.stringify(dllInputdata) + "'}";
    if (typeof (CefSharp) != "undefined") {   // 定制浏览器
      CefSharp.BindObjectAsync("xysCefToJavaScripts");
      xysCefToJavaScripts.driveLocalHardware(dllParams).then((returnVale) => {
        var res = JSON.parse(returnVale);
        if (res.errorCode == '0') {
          message.success('成功！')
          this.props.handleConfirm && this.props.handleConfirm();
        }
        else {
          message.error(returnVale);
          Modal.warning({
            title: '扫码支付错误：',
            content: returnVale,
          });
          this.modifyVisible(false, undefined, 'Y', "Y"); // 关闭弹窗
        }
      })
    } else {
      const frame = chrome?.webview?.hostObjects?.Bridge || ''; // 新版浏览器
      if (frame) {
        frame.DynamicLoadingDLL(dllParams)
          .then(
            data => {
              var res = JSON.parse(data);
              if (res.errorCode == '0') {
                message.success('成功！')
                this.props.handleConfirm && this.props.handleConfirm();
              }
              else {
                message.error(returnVale);
                Modal.warning({
                  title: '扫码支付错误：',
                  content: returnVale,
                });
                this.modifyVisible(false, undefined, 'Y', "Y"); // 关闭弹窗
              }
            },
            err => {
              console.log('error', err);
            }
          )
      }
    }
  }

  // 名称
  handleauthcodeChange = e => {
    this.setState({ authcode: e.target.value })
  }

  // 名称回车事件
  nameOnPressEnter = () => {
    this.callDynamicLibrary();
  }

  render() {
    const { visible, okLoading, authcode } = this.state;
    return (
      <Modal
        title="扫码确认"
        visible={visible}
        destroyOnClose={true}
        width="600px"
        footer={<div>
          <Button onClick={() => this.modifyVisible(false, "", "Y", "Y")}>取消</Button>
          <Button type="primary" onClick={this.handleOk} loading={okLoading}>确认</Button>
        </div>}
      >
        <div style={{ textAlign: 'center' }}>
          条码：
          <Input
            ref={this.myRef}
            style={{ width: '300px', marginRight: '15px' }}
            value={authcode} onChange={this.handleauthcodeChange}
            placeholder="输入内容后可回车检索"
            onPressEnter={this.nameOnPressEnter}
          />
        </div>
      </Modal>
    )
  }
};

export default ScanCodeModal;