// packageD/warranty/Add_Balance_manage/Add_Balance_manage.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    isOpenCompany: false,
    kwd: "",
    companyRadio: null,
    serial_number: "",
    storeName: "",
    insuer: "",
    certificateNumber: "",
    information: "",
    insuranceAddress: "",
    insuredProperty: "",
    usertype: "",
    amountForce: "",
    insurancePeriod: "",
    premium: "",
    insuranceTypeVal: "",
    glassRisk: "",
    insurer: "",
    companyNames: "",
    Memoinformation: "",
    insuranceTypeshow: false,
    // selectCompanyPopup: false,
    columns: [
      {
        text: "火险+盗抢",
      },
      {
        text: "单独盗抢",
      },
    ],
    companyList: [],
    // 弹窗
    selectCompanyPopup: false,
    // 下拉选择
    columns_ind: 0,
    id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
    });
    this.getJson();
  },
  // 读取数据
  getJson() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.supplier.frontend.insurance.insurance-edit"
    );
    let json = {
      id: that.data.id,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var response = resdata.data;
        // console.log(res)
        if (response.result == 1) {
          // that.Toast(res.msg);
          that.setData({
            serial_number: response.data.serial_number,
            storeName: response.data.shop_name,
            insuer: response.data.insured,
            certificateNumber: response.data.identification_number,
            information: response.data.phone,
            insuranceAddress: response.data.address,
            insuredProperty: response.data.insured_property,
            usertype: response.data.customer_type,
            amountForce: response.data.insured_amount,
            insurancePeriod: response.data.guarantee_period,
            premium: response.data.premium,
            insurer: response.data.insurance_company,
            insuranceTypeVal: response.data.insurance_coverage,
            companyRadio: response.data.company_id,
            glassRisk: response.data.additional_glass_risk,
            Memoinformation: response.data.note,
          });
          if (
            response.data.has_one_company &&
            response.data.has_one_company.name
          ) {
            that.setData({
              companyNames: response.data.has_one_company.name,
            });
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 搜索公司开关
  showCompany() {
    this.setData({
      selectCompanyPopup: true,
    });
    var e = {
      detail: {
        value: "",
      },
    };
    this.taPsearch(e);
  },
  goClockPunchRecord() {
    this.setData({
      selectCompanyPopup: false,
    });
  },
  // 选择险种
  bindPickerChange(e) {
    let that = this;
    this.setData({
      insuranceTypeVal: that.data.columns[e.detail.value].text,
    });
    console.log(that.data.columns[e.detail.value]);
  },
  // 选择保险公司
  selectTap(e) {
    console.log(e);
    this.setData({
      companyRadio: e.currentTarget.dataset.id,
      selectCompanyPopup: false,
      companyNames: e.currentTarget.dataset.name,
    });
  },
  serial_number(e) {
    console.log(e);
    this.setData({
      serial_number: e.detail.value,
    });
  },
  storeName(e) {
    console.log(e);
    this.setData({
      storeName: e.detail.value,
    });
  },
  insuer(e) {
    console.log(e);
    this.setData({
      insuer: e.detail.value,
    });
  },
  certificateNumber(e) {
    console.log(e);
    this.setData({
      certificateNumber: e.detail.value,
    });
  },
  information(e) {
    console.log(e);
    this.setData({
      information: e.detail.value,
    });
  },
  insuranceAddress(e) {
    console.log(e);
    this.setData({
      insuranceAddress: e.detail.value,
    });
  },
  insuredProperty(e) {
    console.log(e);
    this.setData({
      insuredProperty: e.detail.value,
    });
  },
  usertype(e) {
    console.log(e);
    this.setData({
      usertype: e.detail.value,
    });
  },
  amountForce(e) {
    console.log(e);
    this.setData({
      amountForce: e.detail.value,
    });
  },
  insurancePeriod(e) {
    console.log(e);
    this.setData({
      insurancePeriod: e.detail.value,
    });
  },
  premium(e) {
    console.log(e);
    this.setData({
      premium: e.detail.value,
    });
  },
  glassRisk(e) {
    console.log(e);
    this.setData({
      glassRisk: e.detail.value,
    });
  },
  // insurer(e) {
  //   console.log(e);
  //   this.setData({
  //     insurer: e.detail.value,
  //   });
  // },
  insurer(e) {
    console.log(e);
    this.setData({
      insurer: e.detail.value,
    });
  },
  submit(e) {
    let that = this;
    console.log(this.data.serial_number);
    if (app._isTextEmpty(this.data.serial_number)) {
      this.Toast("请填写序号！");
      return;
    }
    if (app._isTextEmpty(this.data.storeName)) {
      this.Toast("请填写店面名称！");
      return;
    }
    if (app._isTextEmpty(this.data.insuer)) {
      this.Toast("请填写被保险人！");
      return;
    }
    if (app._isTextEmpty(this.data.certificateNumber)) {
      this.Toast("请填写证件号码！");
      return;
    }
    if (app._isTextEmpty(this.data.information)) {
      this.Toast("请填写被保险人联系方式！");
      return;
    }
    if (app._isTextEmpty(this.data.insuranceAddress)) {
      this.Toast("请填写保险地址！");
      return;
    }
    if (app._isTextEmpty(this.data.insuredProperty)) {
      this.Toast("请填写投保财产！");
      return;
    }
    if (app._isTextEmpty(this.data.usertype)) {
      this.Toast("请填写用户类型！");
      return;
    }
    if (app._isTextEmpty(this.data.insurancePeriod)) {
      this.Toast("请填写保险期限！");
      return;
    }
    if (app._isTextEmpty(this.data.premium)) {
      this.Toast("请填写保费！");
      return;
    }
    if (app._isTextEmpty(this.data.insuranceTypeVal)) {
      this.Toast("请选择投保险种！");
      return;
    }
    if (app._isTextEmpty(this.data.glassRisk)) {
      this.Toast("请输入玻璃险！");
      return;
    }
    if (app._isTextEmpty(this.data.insurer)) {
      this.Toast("请输入投保人");
      return;
    }
    if (app._isTextEmpty(this.data.companyNames)) {
      this.Toast("请选择保险公司");
      return;
    }
    let json = {
      id: that.data.id,
      data: {
        serial_number: that.data.serial_number,
        shop_name: that.data.storeName,
        insured: that.data.insuer,
        identification_number: that.data.certificateNumber,
        phone: that.data.information,
        address: that.data.insuranceAddress,
        insured_property: that.data.insuredProperty,
        customer_type: that.data.usertype,
        insured_amount: that.data.amountForce,
        guarantee_period: that.data.insurancePeriod,
        premium: that.data.premium,
        insurance_company: that.data.insurer,
        insurance_coverage: that.data.insuranceTypeVal,
        additional_glass_risk: that.data.glassRisk,
        company_id: that.data.companyRadio,
        note: that.data.Memoinformation,
      },
    };
    console.log(json);
    let urlStr = app.getNetAddresss(
      "plugin.supplier.frontend.insurance.insurance-edit"
    );
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          // that.Toast(res.msg);
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1,
                });
              }, 1000);
            },
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 搜索
  taPsearch(e) {
    console.log(e);
    let kwd = e.detail.value;
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.supplier.frontend.insurance.searchCompany"
    );
    urlStr += "&kwd=" + kwd;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            companyList: res.data.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  Toast(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
      duration: 1000,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
