// packageD/auction/auction-apply/auction-apply/auction-apply.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressShow: false,
    pickerValue: [0, 0, 0],
    selectAddressData: [],
    retCity: "",
    auctionValue: {
      name: "",
      phone: "",
      retCity: "",
      agreementChoose: false,
    },
    uploadUrl: "upload.uploadPic",
    showphone: false,
    yazshop: "",
    shenqShow: false,
    // 真实姓名
    real_name: "",
    // 省id
    province_id: "",
    // 市id
    city_id: "",
    // 区id
    district_id: "",
    // 详细地址
    address: "",
    // 身份证
    identity_card: "",
    // 企业
    company: "",
    // 身份证正面照
    positive: "",
    // 身份证反面照
    back: "",
    // 手持身份证
    holdID: "",
    // 营业执照
    license: "",
    remark: "",
    // 协议
    agreement: "",
    // 背景
    banner_img: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getYanz()
    this._initAddressInfo();
    // this.getdata();
    // this.getAgreement()
  },
  getverifyGoods() {
    var that = this;
    let json = {
      art: "verifyGoods",
    };
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res, "可以申请");
          that.getAgreement();
        } else {
          console.log(res, "需要购买指定商品才能申请");
          wx.showModal({
            title: res.msg.msg,
            success(resData) {
              if (resData.confirm) {
                wx.showLoading({
                  title: '加载中',
                });
                console.log('用户点击确定');
                if (!app._isTextEmpty(res.msg.url)) {
                  wx.navigateTo({
                    url: "/packageA/detail_v2/detail_v2?id=" + res.msg.url,
                    success: function (sue) {
                      wx.hideLoading({
                        success: (res) => {},
                      });
                    }
                  });
                } else {
                  wx.reLaunch({
                    url: "/packageG/member_v2/member_v2",
                    success: function (sue) {
                      wx.hideLoading({
                        success: (res) => {},
                      });
                    }
                  });
                }
              } else if (resData.cancel) {
                console.log('用户点击取消');
                wx.navigateBack({
                  delta: 1,
                });
              }
            }
          });
          // wx.showToast({
          //   title: res.msg.msg,
          //   icon: "none",
          //   duration: 1000,
          //   success() {
          // setTimeout(() => {
          //   if (!app._isTextEmpty(res.msg.url)) {
          //     wx.navigateTo({
          //       url: "/packageA/detail_v2/detail_v2?id=" + res.msg.url,
          //     });
          //   } else {
          //     wx.reLaunch({
          //       url: "/packageG/member_v2/member_v2",
          //     });
          //   }
          // }, 1000);
          // },
          // });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  isString(str) {
    return str instanceof String || typeof str == "string";
  },
  street(e) {
    this.setData({
      street: e.detail.value,
    });
  },
  isArray(arr) {
    return arr instanceof Array;
  },
  tapoff(e) {
    wx.reLaunch({
      url: "/packageG/index/index",
    });
  },
  address(e) {
    this.setData({
      address: e.detail.value,
    });
  },
  // 街道开启
  streetChoose() {
    console.log(this.data.auctionValue);
    if (app._isTextEmpty(this.data.auctionValue)) {
      wx.showToast({
        icon: "none",
        title: "请先选择所在地区",
        duration: 1500,
      });
    } else {
      this.setData({
        streetShow: true,
        opren: false,
      });
    }
  },
  // 街道关闭
  streetClose(e) {
    this.setData({
      streetShow: false,
      opren: true,
    });
  },
  //获取街道
  _getStreet(param) {
    var that = this;
    let urlStr = app.getNetAddresss("member.memberAddress.street");
    urlStr += "&district_id=" + param;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1 && !app._isTextEmpty(res.data)) {
          if (!app._isTextEmpty(res.data) && res.data.length > 0) {
            that.setData({
              districtVal: res.data,
              isShowStreet: true,
            });
          } else {
            that.setData({
              isShowStreet: false,
            });
          }
        } else {
          that.setData({
            isShowStreet: false,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  streetConfirm(e) {
    let strname = e.target.dataset.strname;
    console.log(strname, e.target.dataset.info.id);
    this.setData({
      streetId: e.target.dataset.info.id,
      street: strname,
      streetShow: false,
    });
  },
  identity_card(e) {
    this.setData({
      identity_card: e.detail.value,
    });
  },
  mobile(e) {
    this.setData({
      mobile: e.detail.value,
    });
  },
  real_name(e) {
    this.setData({
      real_name: e.detail.value,
    });
  },
  company(e) {
    this.setData({
      company: e.detail.value,
    });
  },
  Chooseinp() {
    this.setData({
      "auctionValue.agreementChoose": !this.data.auctionValue.agreementChoose,
    });
  },
  _closeDateLw() {
    this.setData({
      addressShow: false,
    });
  },
  onClicksQ() {
    this.setData({
      shenqShow: false,
    });
  },
  shenqShow() {
    this.setData({
      shenqShow: true,
    });
  },
  addressShowbtn() {
    this.setData({
      addressShow: true,
    });
  },
  isCoverLayer() {
    this.setData({
      showphone: false,
    });
  },
  resultAdd() {
    let seleData = this.data.selectAddressData;
    let retCity =
      seleData[0].name + " " + seleData[1].name + " " + seleData[2].name;
    this.setData({
      "auctionValue.retCity": retCity,
      addressShow: false,
      province_id: seleData[0].id,
      city_id: seleData[1].id,
      district_id: seleData[2].id,
    });
    this._getStreet(seleData[2].id);
  },
  //地址选择器数据初始化
  _initAddressInfo: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地址信息准备就绪");
        that._getProvinceData();
      },
      fail: function (res) {
        console.log("省fail");
        app._getNetWork({
          url: app.getNetAddresss("member.member-address.address"),
          success: function (res) {
            var response = res.data;
            var province = response.data.province;
            var city = response.data.city;
            var district = response.data.district;
            wx.setStorage({
              key: "provinceData",
              data: province,
            });
            wx.setStorage({
              key: "cityData",
              data: city,
            });
            wx.setStorage({
              key: "districtData",
              data: district,
            });
            that._initAddressInfo();
          },
          fail: function (res) {},
          state: true,
        });
      },
    });
  },
  // 读取省key
  _getProvinceData: function () {
    var that = this;
    var _keyName = "provinceData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("省读取成功", res.data);
        that.setData({
          provinceData: res.data,
        });
        that.setData({
          "selectAddressData[0].name": res.data[that.data.pickerValue[0]].areaname,
          "selectAddressData[0].id": res.data[that.data.pickerValue[0]].id,
          "selectAddressData[0].index": that.data.pickerValue[0],
        });
        that._getCityData();
      },
    });
  },
  // 读取城市key，筛选城市data
  _getCityData: function () {
    var _data = this.data.selectAddressData[0];
    console.log("城市", "_data", _data);
    var that = this;
    var _keyName = "cityData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("城市读取成功", res);

        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("城市数据筛选", _json);
        that.setData({
          cityData: _json,
        });
        that.setData({
          "selectAddressData[1].name": _json[that.data.pickerValue[1]].areaname,
          "selectAddressData[1].id": _json[that.data.pickerValue[1]].id,
          "selectAddressData[1].index": that.data.pickerValue[1],
        });
        that._getDistrictData();
      },
    });
  },
  // 读取地区key，筛选地区data
  _getDistrictData: function () {
    var _data = this.data.selectAddressData[1];
    console.log("地区", "_data", _data);
    console.log(_data);
    var that = this;
    var _keyName = "districtData";
    wx.getStorage({
      key: _keyName,
      success: function (res) {
        console.log("地区读取成功");
        var _json = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].parentid == _data.id) {
            _json.push(res.data[i]);
          }
        }
        console.log("地区数据筛选", _json);
        that.setData({
          districtData: _json,
        });
        console.log("districtData", _json);
        that.setData({
          "selectAddressData[2].name": _json[that.data.pickerValue[2]].areaname,
          "selectAddressData[2].id": _json[that.data.pickerValue[2]].id,
          "selectAddressData[2].index": that.data.pickerValue[2],
        });
        console.log("over", that.data.selectAddressData);
      },
    });
  },
  // 收货地址滚动事件
  bindChange: function (e) {
    var val = e.detail.value;
    if (this.data.pickerValue[0] != val[0]) {
      val[1] = 0;
      val[2] = 0;
    }
    if (this.data.pickerValue[1] != val[1]) {
      val[2] = 0;
    }
    this.setData({
      "pickerValue[0]": val[0],
      "pickerValue[1]": val[1],
      "pickerValue[2]": val[2],
    });
    this._initAddressInfo();
  },
  remarkinp(e) {
    this.setData({
      remark: e.detail.value,
    });
  },
  // 选择图片
  onRead() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        let photourl = tempFilePaths[0];
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: "file",
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                fileList1: res.data.img_url,
                positive: res.data.img_url,
              });
            } else {
              wx.showToast({
                icon: "none",
                title: res.msg,
                duration: 1500,
              });
            }
          },
        });
      },
    });
  },
  onRead_1() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        let photourl = tempFilePaths[0];
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: "file",
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                fileList2: res.data.img_url,
                back: res.data.img_url,
              });
            } else {
              wx.showToast({
                icon: "none",
                title: res.msg,
                duration: 1500,
              });
            }
          },
        });
      },
    });
  },
  onRead_2() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        let photourl = tempFilePaths[0];
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: "file",
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                fileList3: res.data.img_url,
                holdID: res.data.img_url,
              });
            } else {
              wx.showToast({
                icon: "none",
                title: res.msg,
                duration: 1500,
              });
            }
          },
        });
      },
    });
  },
  onRead_3() {
    let that = this;
    let urlStr = app.getNetAddresss(that.data.uploadUrl);
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        let photourl = tempFilePaths[0];
        wx.uploadFile({
          url: urlStr,
          filePath: photourl,
          name: "file",
          success(resdata) {
            var res = JSON.parse(resdata.data);
            if (res.result == 1) {
              that.setData({
                fileList4: res.data.img_url,
                license: res.data.img_url,
              });
            } else {
              wx.showToast({
                icon: "none",
                title: res.msg,
                duration: 1500,
              });
            }
          },
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  apply() {
    var that = this;
    if (!that.data.auctionValue.agreementChoose) {
      wx.showToast({
        title: "请阅读并勾选申请协议",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    // if (!that.data.real_name) {
    //   wx.showToast({
    //     title: "请填写姓名",
    //     icon: "none",
    //     duration: 1000,
    //   });
    // }
    // if (!that.data.mobile) {
    //   wx.showToast({
    //     title: "请填写手机号",
    //     icon: "none",
    //     duration: 1000,
    //   });
    //   return;
    // }
    // if (!that.data.province_id && !that.data.city_id && !district_id) {
    //   wx.showToast({
    //     title: "请选择城市",
    //     icon: "none",
    //     duration: 1000,
    //   });
    //   return;
    // }
    // if (!that.data.address) {
    //   wx.showToast({
    //     title: "请填写手机号",
    //     icon: "none",
    //     duration: 1000,
    //   });
    //   return;
    // }
    let json = {
      art: "apply",
      real_name: that.data.real_name,
      mobile: that.data.mobile,
      province_id: that.data.province_id,
      city_id: that.data.city_id,
      district_id: that.data.district_id,
      address: that.data.address,
      identity_card: that.data.identity_card,
      company: that.data.company,
      positive: that.data.positive,
      back: that.data.back,
      holdID: that.data.holdID,
      license: that.data.license,
      remark: that.data.remark,
      street_id: that.data.streetId,
    };
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            showphone: true,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getAgreement() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            agreement: res.data.getAgreement.agreement,
            banner_img: res.data.getAgreement.banner_img,
          });
          if (res.data.is_apply == 0) {
            that.getApply();
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getYanz() {
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.auction.api.auctioneer.verifyGoods"
    );
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            yazshop: 1,
            showphone: true,
          });
        } else {
          that.setData({
            yazshop: 0,
          });
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getApply() {
    var that = this;
    let json = {
      art: "getApply",
    };
    let urlStr = app.getNetAddresss("plugin.auction.api.index.getAuctionData");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            real_name: res.data.getApply.real_name,
            mobile: res.data.getApply.mobile,
            province_id: res.data.getApply.district_id,
            city_id: res.data.getApply.city_id,
            district_id: res.data.getApply.district_id,
            address: res.data.getApply.address,
            identity_card: res.data.getApply.identity_card,
            company: res.data.getApply.company,
            positive: res.data.getApply.positive,
            back: res.data.getApply.back,
            holdID: res.data.getApply.holdID,
            license: res.data.getApply.license,
            remark: res.data.getApply.remark,
            "auctionValue.agreementChoose": true,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
});