var app = getApp();
var $ = require('../../../js/util.js');
Page({
  data: {
    id:0,
    is_set: 0,
    param:{
      types:1,
      paypassword: '',
      newpaypassword: '',
      repaypassword: '',
    }
  },
  onLoad: function (options) {
    app.HCsetNavigationBarTitle('支付密码');
  },
  onReady: function () {
    //初始化数据
    var self = this;
    app.getUserInfo(function (ret) {
        self.getIndex();
    });
  },
  getIndex: function () {
    var self = this;
    var action = 'c=Member&a=get_paypwd';
    var param = $.ObjectToStr(self.data.param);
    app.showLoading('加载中...');
    app.ajaxRequest(action, 'GET', param, function (ret) {
      if(ret&&ret.status==1){
        if (ret.code && ret.code=='success'){
          app.HCtoast('操作成功！');
          setTimeout(function(){
              app.HCnavigateBack();
          },2000);
        } else {
          if (ret.data && ret.data.is_set) {
            self.setData({
              is_set : ret.data.is_set,
            });
          }
        }
      } else {
        app.HCerror('提示', ret.msg); return;
      }
    },true);
  },
  paypasswordInput:function(event){
    var param = this.data.param;
    param.paypassword = event.detail.value;
    this.setData({
      param: param
    });
  },
  newpaypasswordInput:function (event) {
    var param = this.data.param;
    param.newpaypassword = event.detail.value;
    this.setData({
      param: param
    });
  },
  repaypasswordInput:function (event) {
    var param = this.data.param;
    param.repaypassword = event.detail.value;
    this.setData({
      param: param
    });
  },
  formSubmit:function(event){
    var value = event.detail.value;
    var param = this.data.param;
    if(this.data.is_set==1){
      if (param.paypassword==''){
        app.HCerror('温馨提示', '请输入旧密码', false); return;
      }
    } 
    if (param.newpaypassword == '') {
      app.HCerror('温馨提示', '请输入新密码', false); return;
    }
    if (param.repaypassword == '') {
      app.HCerror('温馨提示', '请输入密码', false); return;
    }
    param.types = 2;
    param.paypassword = value.paypassword;
    param.newpaypassword = value.newpaypassword;
    param.repaypassword = value.repaypassword;
    this.getIndex();
  }
})