<template>
  <view :style="{height: screenHeight == 0 ? '100%':screenHeight+'px'}">

    <view @click="keyHideStatus" style="height: 100%;">
      <uni-card>
        <uni-forms ref="baseForm" :rules="rules" :modelValue="baseFormData">
          <uni-forms-item label="旧密码" name="oldPassword">
           <!-- <uni-easyinput ref="oldPassword" type="password" v-model="baseFormData.oldPassword" @blur="close()" @focus="open(1)"></uni-easyinput> -->
		    <uni-easyinput ref="oldPassword" type="password" v-model="baseFormData.oldPassword"></uni-easyinput>
          </uni-forms-item>
          <uni-forms-item label="新密码" name="newPassword">
            <!-- <uni-easyinput type="password" v-model="baseFormData.newPassword" @blur="close()" @focus="open(2)"></uni-easyinput> -->
			<uni-easyinput type="password" v-model="baseFormData.newPassword"></uni-easyinput>
          </uni-forms-item>
          <uni-forms-item label="确认密码" name="newPassword1">
           <!-- <uni-easyinput type="password" v-model="baseFormData.newPassword1" @blur="close()" @focus="open(3)"></uni-easyinput> -->
		    <uni-easyinput type="password" v-model="baseFormData.newPassword1"></uni-easyinput>
          </uni-forms-item>
          <view>
            <button class="buttonClass" @click="save()">修改</button>
          </view>
        </uni-forms>
      </uni-card>

    </view>
   <!-- <view>
      <tki-float-keyboard ref="keyb" :mode="keyMode" :type="keyType" :title="title" @del="keyDel" @val="keyVal" @show="keyShow"
        @hide="keyHide"></tki-float-keyboard>
    </view> -->
  </view>
</template>

<script>
  import { UpdatePwd, updateLrPwd, unbind, logout } from '@/api/my.js'
  import { getUser } from '@/common/auth.js'
  import tkiFloatKeyboard from '@/components/tki-float-keyboard/tki-float-keyboard.vue'
  export default {
    components: {
      tkiFloatKeyboard
    },
    data() {
      return {
		  screenHeight:"",
        userType: '',
        // 键盘
        // 键盘类型  keyboard 普通键盘，car 汽车键盘，number 数字键盘
        keyMode: 'keyboard',
        // type 键盘可用区域，配合mode属性使用  默认值：0
        //mode = keyboard 时 type = 0 全部、 1 字母加数字、 2 符号、 3 字母、 4 数字、 5 字母加符号、6 数字加符号
        //mode = car 时 type = 0 全部、 1 字母加数字、 2 省、 3 字母加数字加特、 4 字母、 5 数字
        //mode = number 时 type = 0 全部、 1 禁用.
        keyType: 0,
        title: '密码键盘',
        keyShowStatus: false,
        showType: 0,
        baseFormData: {
          oldPassword: '',
          newPassword: '',
          newPassword1: '',
        },
        rules: {
          oldPassword: {
            rules: [{
              required: true,
              errorMessage: '旧密码不能为空'
						}]
          },
          newPassword: {
            rules: [{
              required: true,
              errorMessage: '新密码不能为空'
						}]
          },
          newPassword1: {
            rules: [{
              required: true,
              errorMessage: '确认密码不能为空'
						}]
          },
        },
      }
    },
    onLoad() {
      // 老人07、员工04、监管00
      let userInfo = getUser()
      this.userType = userInfo.userType
	  this.screenHeight = uni.getSystemInfoSync().windowHeight;
    },
    methods: {
      keyHideStatus() {
        if (this.keyShowStatus) this.$refs.keyb._keyHide()
      },
      keyVal(v) {
        if (this.showType == 1) {
          this.baseFormData.oldPassword += v
        } else if (this.showType == 2) {
          this.baseFormData.newPassword += v
        } else if (this.showType == 3) {
          this.baseFormData.newPassword1 += v
        }

      },
      keyDel(d) {
        if (this.showType == 1) {
          this.baseFormData.oldPassword = this.baseFormData.oldPassword.substring(0, this.baseFormData
            .oldPassword.length - 1)
        } else if (this.showType == 2) {
          this.baseFormData.newPassword = this.baseFormData.newPassword.substring(0, this.baseFormData
            .newPassword.length - 1)
        } else if (this.showType == 3) {
          this.baseFormData.newPassword1 = this.baseFormData.newPassword1.substring(0, this.baseFormData
            .newPassword1.length - 1)
        }
      },
      keyShow(h) {
        this.keyShowStatus = true
      },
      keyHide(h) {
        this.keyShowStatus = false
      },
      open(e) {
        this.showType = e
        uni.hideKeyboard()
        this.$refs.keyb._keyShow();
      },
      close() {},
      save() {
        if (this.keyShowStatus) this.$refs.keyb._keyHide()
        this.$refs['baseForm'].validate().then(res => {
          if (this.baseFormData.newPassword != this.baseFormData.newPassword1) {
            this.showConfirm('新密码与确认密码不同')
            return
          }
          uni.showLoading({
            title: '正在修改中...',
            mask:true,
          })
          if (['04', '07'].includes(this.userType)) {
            updateLrPwd(this.baseFormData).then(r => {
              uni.hideLoading()
              if (r.code == 200) {
                this.toast('修改成功')
                setTimeout(function() {
                  uni.switchTab({
                    url: '/pages/my/index'
                  })
                }, 1000)
              }
            })
          } else {
            UpdatePwd(this.baseFormData).then(r => {
              uni.hideLoading()
              if (r.code == 200) {
                this.toast('修改成功')
                setTimeout(function() {
                  uni.switchTab({
                    url: '/pages/my/index'
                  })
                }, 1000)
              }
            })
          }

        })
      }
    }
  }
</script>

<style>
  .buttonClass {
    margin-top: 22px;
    margin-left: 9vw;
    width: 68vw;
    height: 38px;
    border-radius: 5.8vw;
    /* border: 1px solid rgba(176, 179, 199, 1); */
    background: #28d87d;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0rpx 4rpx 10rpx 1rpx rgba(40, 216, 125, 0.4);
  }
</style>