const validatePhoneCheck = async function (rule, value, callback) {
  if (value === '' || !value) {
    callback(new Error('手机号不能为空'))
  } else if (!/^1[3456789]\d{9}$/.test(value)) {
    callback(new Error('手机号不正确'))
  } else {
    callback()
  }
}
var vm = new Vue({
  el: '#rrapp',
  data: {
    loading: false,
    show_fdkkUser: '',
    info: {
      fdkkUser: null,
    },
    form: {
      fdkkPassword: '',
      fdkkUser: '',
    },
    ruleValidate: {
      fdkkUser: [
        {
          required: true,
          validator: validatePhoneCheck,
          trigger: 'blur',
        },
      ],
      fdkkPassword: [
        { required: true, message: '密码不能为空', trigger: 'blur' },
      ],
    },
  },
  watch: {},
  created() {
    this.getBindInfo()
  },
  methods: {
    handleSubmit: function (name) {
      handleSubmitValidate(vm, name, function () {
        var data = {
          fdkkPassword: encodeStr(Base64.encode(vm.form.fdkkPassword)),
          fdkkUser: vm.form.fdkkUser,
        }
        $.ajax({
          type: 'POST',
          url: '../sys/user/bind4dkkUser',
          data: data,
          dataType: 'json',
          headers: { token: localStorage.getItem('token') },
          success: function (result) {
            if (result.code == 0) {
              console.log(result)
              alert('操作成功', function (index) {
                vm.getBindInfo()
                vm.form = {
                  password: '',
                  mobild: '',
                }
              })
            } else {
              alert(result.msg)
            }
          },
          error: function (err) {},
        })
      })
    },
    handleReset: function (name) {
      handleResetForm(vm, name)
      vm.form = {
        password: '',
        mobild: '',
      }
    },
    unbind() {
      confirm('解绑后，场景对应绑定的数据将被删除，请谨慎操作', function () {
        $.ajax({
          type: 'POST',
          url: '../sys/user/unBind4dkkUser',
          headers: { token: localStorage.getItem('token') },
          data: { fdkkUser: vm.info.fdkkUser },
          dataType: 'json',
          success: function (result) {
            if (result.code == 0) {
              vm.info.fdkkUser = null
              alert('操作成功', function (index) {
                // vm.reload()
              })
            } else {
              alert(res.msg)
            }
          },
        })
      })
    },
    getBindInfo() {
      var userId = JSON.parse(localStorage.getItem('userInfo')).userId
      $.ajax({
        type: 'GET',
        url: `../sys/user/info/${userId}`,
        // data: data,
        // dataType: 'json',
        success: function (res) {
          vm.loading = true
          console.log(res)
          if (res.code == 0) {
            vm.info = res.user
            if (res.user.fdkkUser) {
              vm.show_fdkkUser =
                res.user.fdkkUser.substring(0, 4) +
                '****' +
                res.user.fdkkUser.substring(8, res.user.fdkkUser.length)
            }
          } else {
            alert(res.msg)
          }
        },
        error: function (err) {
          vm.loading = true
        },
      })
    },
  },
})
