
import http from '@ohos.net.http';
import {BusinessError} from '@ohos.base';
import PaymentManager from  '../Manager/PaymentManger'

@Entry
@Component
struct Index {
  @State tn: string = ''
  @State mode: string = '00'
  @State url: string = ''
  @State seType: string = ''
  @State inputSeType: string = ''
  controller: TextInputController = new TextInputController()

  build() {
    Row() {
      Column() {
        Row() {
          TextInput({ text: this.tn, placeholder: '输入tn号', controller: this.controller })
            .placeholderColor(Color.Grey)
            .placeholderFont({ size: 14, weight: 400 })
            .caretColor(Color.Blue)
            .width('50%')
            .height(40)
            .margin({left:'2%'})
            .fontSize(13)
            .fontColor(Color.Black)
            .onChange((value: string) => {
              this.tn = value
            })

          TextInput({ text: this.mode, placeholder: '输入mode', controller: this.controller })
            .margin({left:'2%',right:'2%'})
            .placeholderColor(Color.Grey)
            .placeholderFont({ size: 10, weight: 400 })
            .caretColor(Color.Blue)
            .width('15%')
            .height(40)
            .fontSize(13)
            .fontColor(Color.Black)
            .onChange((value: string) => {
              this.mode = value
            })

          TextInput({ text: this.inputSeType, placeholder: '输入seType', controller: this.controller })
            .placeholderColor(Color.Grey)
            .placeholderFont({ size: 10, weight: 400 })
            .caretColor(Color.Blue)
            .width('25%')
            .height(40)
            .fontSize(13)
            .fontColor(Color.Black)
            .onChange((value: string) => {
              this.inputSeType = value
            })
        }.width('100%')



        Button('发起支付', { type: ButtonType.Normal, stateEffect: true })
          .fontSize(20)
          .fontWeight(FontWeight.Bold)
          .borderRadius(8)
          .backgroundColor(0x317aff)
          .width('80%')
          .height(40)
          .margin(20)
          .onClick(()=> {
            this.directPay();
          })

      }
      .width('100%')
    }
    .height('100%')
  }

  directPay() {
    if (this.mode && this.tn) {
      PaymentManager.startPay(this.tn , this.mode, 'uppaymerchantdemo', this.inputSeType);
    } else {
      AlertDialog.show({message: '请先输入mode 和 tn', alignment:DialogAlignment.Center});
    }
  }

  startPay(mode: string,seType?: string) {
    this.sendTnRequest(mode).then((tn) => {
      PaymentManager.startPay(tn, mode, 'uppaymerchantdemo', seType);

      console.log("调用远程支付成功");
    }).catch((error:BusinessError) => {
      console.log("调用远程支付失败");
    });
  }



  sendTnRequest(mode: string): Promise<string> {
    return new Promise((resolve, reject) => {
      let httpRequest = http.createHttp();
      let url = "http://172.21.135.13:10000/para/getRedis";
      let requestOption: http.HttpRequestOptions = {
        method: http.RequestMethod.GET,
      };

      let responsePromise = httpRequest.request(url,requestOption);

      responsePromise.then((data: http.HttpResponse) => {
        let error:BusinessError = {
          code: 9999,
          name: "com.uinonpay.merchantdemo",
          message: "tn请求失败"
        }

        if (data.responseCode === 200) {
          let tn: string = data.result as string;
          if (tn) {
            resolve(tn);
            return;
          }
        } else {
          error.code = data.responseCode;
          error.message = data.result as string;
        }
        reject(error);
      }).catch((err: BusinessError) => {

        reject(err);
      });
    });
  }



}

