<template>
    <view>
        <view class="setting-content">
            <!--<uni-list>-->
            <!--<uni-list-item title="关于我们" clickable @click="toAboutOur" showArrow></uni-list-item>-->
            <!--<uni-list-item title="修改登录密码" clickable @click="toAboutOur" showArrow></uni-list-item>-->
            <!--<uni-list-item title="清除缓存" clickable @click="cleanStorage" ></uni-list-item>-->
            <!--</uni-list>-->


            <uni-section title="修改登录密码" type="line">
                <view class="example" style="padding: 0 15px;">
                    <!-- 基础表单校验 -->
                    <uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" label-width="100">
                        <uni-forms-item label="原登录密码" required name="oldPwd">
                            <uni-easyinput v-model="valiFormData.oldPwd" placeholder="请输入原登录密码"/>
                        </uni-forms-item>
                        <uni-forms-item label="新登录密码" required name="newPwd">
                            <uni-easyinput v-model="valiFormData.newPwd" placeholder="请输入新登录密码"/>
                        </uni-forms-item>
                        <uni-forms-item label="新登录密码" required name="newPwdAga">
                            <uni-easyinput v-model="valiFormData.newPwdAga" placeholder="请再次输入新登录密码"/>
                        </uni-forms-item>
                    </uni-forms>

                </view>
            </uni-section>


            <view class="uni-padding-wrap uni-common-mt">
                <button type="warn" @click="submit('valiForm')">确定修改</button>
            </view>

        </view>

    </view>
</template>

<script>

export default {
  data() {
    return {
      // 校验表单数据
      valiFormData: {
        oldPwd: '',
        newPwd: '',
        newPwdAga: '',
      },
      // 校验规则
      rules: {
        oldPwd: {
          rules: [{
            required: true,
            errorMessage: '原密码不能为空'
          }, {
            minLength: 6,
            errorMessage: '请输入大于六位数',
          }
          ]
        },
        newPwd: {
          rules: [{
            required: true,
            errorMessage: '新密码不能为空'
          }, {
              minLength: 6,
              errorMessage: '请输入大于六位数',
            }
          ]
        },
        newPwdAga: {
          rules: [{
            required: true,
            errorMessage: '新密码不能为空'
          }, {
              minLength: 6,
              errorMessage: '请输入大于六位数',
            }
          ]
        }
      },
    }
  },
  methods: {
    submit(ref) {
      let that = this;
      this.$refs[ref].validate().then(res => {
        if(this.valiFormData.newPwd !== this.valiFormData.newPwdAga){
          uni.showToast({
            icon:'none',
            title: `两次输入的新密码不一样`
          })
          return
        }
        // uni.getStorageSync("indexInfo").userName

        let changePassword = this.$common.changePassword;
        let jsondata = {
          userCode:  uni.getStorageSync("indexInfo").userCode,
          oldPassWord: this.valiFormData.oldPwd,
          newPassWord: this.valiFormData.newPwdAga,
        };
        uni.request({
          url: changePassword,
          data: JSON.stringify(this.$common.requestSign(jsondata)),
          dataType: "json",
          method: "POST",
          success: (res) => {
            console.log(res)
            if (res.data.code == 0) {
              uni.showToast({
                icon:'none',
                title: `修改成功`
              })
              that.loginOut();
            }else{
              uni.showToast({
                icon:'none',
                title: res.data.message
              })

            }
          }
        })

      }).catch(err => {
        console.log('err', err);
      })
    },
    toAboutOur() {
      uni.navigateTo({
        url: '../aboutOur/aboutOur'
      });
    },
    cleanStorage() {
      uni.clearStorageSync();
      uni.showToast({
        title: "清理缓存成功",
        mask: false,
        duration: 2000
      })
    },
    loginOutAlert() {
      let that = this;
      uni.showModal({
        title: '提示',
        content: '是否确定退出登录？',
        success: (res) => {
          if (res.confirm) {
            that.loginOut();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    loginOut() {
      let that = this;
      that.$myRequest({
        url: that.$common.userLoginout,
        method: 'POST',
        data: that.$common.requestSign()
      }).then(res => {
        // 获取真实数据之前，务必判断状态是否为200
        console.log('退出：' + JSON.stringify(res));
        if (res.code == 0) {
          uni.clearStorageSync();
          uni.reLaunch({
            url: "../index/index"
          })
          uni.hideLoading();
        } else {
          uni.clearStorageSync();
          uni.reLaunch({
            url: "../index/index"
          })
          uni.hideLoading();
        }
      })
    }
  }
}
</script>

<style lang="scss">
    .setting-content {
        background-color: #f6f6f6;
        height: 100vh;

    }
</style>
