// packageD/quickPay/member_add_bank_second.js
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    order_pay_id: "",
    bank: [],
    payer_name: "",
    payer_phone: "",
    id_no: "",
    cvv: "",
    date: "2016-06",
    card_no: "",
    bank_name: "",
    bank_type: "",
    id_type: "",
    user_agrt: "",
    checked: true,
    show2: false,
    show3: false,
    show4: false,
    show5: false,
    radio: "",
    radio1: "",
    radio2: "",
    idType: [
      {
        id: "1",
        text: "身份证",
      },
      {
        id: "2",
        text: "军官证",
      },
      {
        id: "3",
        text: "士兵证",
      },
      {
        id: "4",
        text: "护照",
      },
      {
        id: "5",
        text: "港澳台居民往来通行证",
      },
      {
        id: "6",
        text: "临时身份证",
      },
      {
        id: "7",
        text: "户口本",
      },
      {
        id: "8",
        text: "警官证",
      },
      {
        id: "9",
        text: "外国人永久居留证",
      },
      {
        id: "10",
        text: "其他",
      },
      {
        id: "11",
        text: "外国护照",
      },
      {
        id: "12",
        text: "营业执照",
      },
    ],
    card: [
      {
        id: "0",
        text: "储蓄卡",
      },
      {
        id: "1",
        text: "信用卡",
      },
    ],
    datas: [
      {
        id: "1",
        text: "中国工商银行",
      },
      {
        id: "2",
        text: "中国银行",
      },
      {
        id: "3",
        text: "中信银行",
      },
      {
        id: "4",
        text: "上海银行",
      },
      {
        id: "5",
        text: "中国建设银行",
      },
      {
        id: "6",
        text: "光大银行",
      },
      {
        id: "7",
        text: "民生银行",
      },
      {
        id: "8",
        text: "北京银行",
      },
      {
        id: "9",
        text: "平安银行",
      },
      {
        id: "10",
        text: "交通银行",
      },
      {
        id: "11",
        text: "招商银行",
      },
      {
        id: "12",
        text: "广发银行",
      },
      {
        id: "13",
        text: "浦发银行",
      },
      {
        id: "14",
        text: "邮政银行",
      },
    ],
    status: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAgrement();
    this.getInfo();
    this.getBank();
    this.initData();
    let option = options;
    this.setData({
      card_no: option.card_no,
      order_pay_id: option.order_pay_id,
    });

    if(options.status) {
      this.setData({
        status: options.status,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  nameInp(e) {
    let val = e.detail.value;
    this.setData({
      payer_name: val,
    });
  },
  phoneInp(e) {
    let val = e.detail.value;
    this.setData({
      payer_phone: val,
    });
  },
  idInp(e) {
    let val = e.detail.value;
    this.setData({
      id_no: val,
    });
  },
  cvvInp(e) {
    let val = e.detail.value;
    this.setData({
      cvv: val,
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
    });
  },
  openBank() {
    this.setData({
      show2: true,
    });
  },
  closeBank() {
    this.setData({
      show2: false,
    });
  },
  openCard() {
    this.setData({
      show3: true,
    });
  },
  closeCard() {
    this.setData({
      show3: false,
    });
  },
  openType() {
    this.setData({
      show4: true,
    });
  },
  closeType() {
    this.setData({
      show4: false,
    });
  },
  bankChange(e) {
    let val = e.detail;
    this.setData({
      radio: val,
    });
  },
  cardChange(e) {
    let val = e.detail;
    this.setData({
      radio1: val,
    });
  },
  idChange(e) {
    let val = e.detail;
    this.setData({
      radio2: val,
    });
  },
  openAgg() {
    this.setData({
      show5: true,
    });
  },
  closeAgg() {
    this.setData({
      show5: false,
    });
  },
  confirm() {
    this.setData({
      show2: false,
    });
    this.data.datas.forEach((item, index, key) => {
      if (item.id == this.data.radio) {
        this.setData({
          bank_name: item.text,
        });
      }
    });
  },
  cardConfirm() {
    this.setData({
      show3: false,
    });
    this.data.card.forEach((item, index, key) => {
      if (item.id == this.data.radio1) {
        this.setData({
          bank_type: item.text,
        });
      }
    });
  },
  typeConfirm() {
    this.setData({
      show4: false,
    });
    this.data.idType.forEach((item, index, key) => {
      if (item.id == this.data.radio2) {
        this.setData({
          id_type: item.text,
        });
      }
    });
  },
  toSelect(e) {
    this.setData({
      checked: e.detail,
    });
  },
  initData() {
    this.setData({
      bank: [],
      payer_name: "",
      payer_phone: "",
      id_no: "",
      cvv: "",
      date: "2016-06",
      card_no: "",
      bank_name: "",
      bank_type: "",
      id_type: "",
      user_agrt: "",
      checked: true,
      show2: false,
      show3: false,
      show4: false,
      show5: false,
      radio: "",
      radio1: "",
      radio2: "",
      idType: [
        {
          id: "1",
          text: "身份证",
        },
        {
          id: "2",
          text: "军官证",
        },
        {
          id: "3",
          text: "士兵证",
        },
        {
          id: "4",
          text: "护照",
        },
        {
          id: "5",
          text: "港澳台居民往来通行证",
        },
        {
          id: "6",
          text: "临时身份证",
        },
        {
          id: "7",
          text: "户口本",
        },
        {
          id: "8",
          text: "警官证",
        },
        {
          id: "9",
          text: "外国人永久居留证",
        },
        {
          id: "10",
          text: "其他",
        },
        {
          id: "11",
          text: "外国护照",
        },
        {
          id: "12",
          text: "营业执照",
        },
      ],
      card: [
        {
          id: "0",
          text: "储蓄卡",
        },
        {
          id: "1",
          text: "信用卡",
        },
      ],
      datas: [
        {
          id: "1",
          text: "中国工商银行",
        },
        {
          id: "2",
          text: "中国银行",
        },
        {
          id: "3",
          text: "中信银行",
        },
        {
          id: "4",
          text: "上海银行",
        },
        {
          id: "5",
          text: "中国建设银行",
        },
        {
          id: "6",
          text: "光大银行",
        },
        {
          id: "7",
          text: "民生银行",
        },
        {
          id: "8",
          text: "北京银行",
        },
        {
          id: "9",
          text: "平安银行",
        },
        {
          id: "10",
          text: "交通银行",
        },
        {
          id: "11",
          text: "招商银行",
        },
        {
          id: "12",
          text: "广发银行",
        },
        {
          id: "13",
          text: "浦发银行",
        },
        {
          id: "14",
          text: "邮政银行",
        },
      ],
    });
  },
  getBank() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.index"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            bank: res.data,
          });
        } else {
          that.setData({
            bank: res.data,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getInfo() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-payer-info"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && res.data) {
          that.setData({
            payer_name: res.data.payer_name,
            radio2: res.data.id_type,
            id_no: res.data.id_no,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  shijian() {
    let y = this.data.date.split("-")[0];
    let m = this.data.date.split("-")[1];
    return `${y}/${m}`;
  },
  getType(id) {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-pay-type"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.pay_type == 1) {
            wx.navigateTo({
              url:
                "/packageD/quickPay/choose_bank/choose_bank?order_pay_id=" +
                that.data.order_pay_id + '&status='+that.data.status,
            });
          } else {
            wx.navigateTo({
              url:
                "/packageD/quickPay/bind_bank/bind_bank?card_id=" +
                id +
                "&order_pay_id=" +
                that.data.order_pay_id+ '&status='+that.data.status,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  getAgrement() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.get-user-agrt"
    );
    app._getNetWork({
      url: urlStr,
      showToastIn: false,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            user_agrt: res.data.user_agrt,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  addBank() {
    var json;
    if (this.data.radio1 == "0") {
      json = {
        card_no: this.data.card_no,
        payer_name: this.data.payer_name,
        payer_phone: this.data.payer_phone,
        id_no: this.data.id_no,
        id_type: this.data.radio2,
        bank_type: this.data.radio1,
        bank_name: this.data.bank_name,
      };
    } else if (this.data.radio1 == "1") {
      json = {
        card_no: this.data.card_no,
        payer_name: this.data.payer_name,
        payer_phone: this.data.payer_phone,
        id_no: this.data.id_no,
        id_type: this.data.radio2,
        bank_type: this.data.radio1,
        bank_name: this.data.bank_name,
        expire_date: this.shijian(this.data.date),
        cvv: this.data.cvv,
      };
    }
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.converge_pay.frontend.controllers.quick-pay-bank-card.create"
    );
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: {
        data: json,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.getType(res.data.id);
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
});
