<template>
  <!-- 用户改对话框 -->
  <el-dialog
    @close="close"
    @opened="updateUser"
    v-dialogDrag
    :title="$t('Security.modifyUser')"
    style="font-size: 10px; " width='500px'
    :visible.sync="updateUserVisible">
    <el-form ref="form" :model="form" :inline="true">
      <el-form-item prop="userName" style="margin-bottom: -10px;" :label="'*'+$t('Security.userName') + '：'" :label-width="formLabelWidth">
        <el-input type="text" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.userName"
                  v-on:blur="userNameCheck"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="usergroupKey" style="margin-bottom: -10px;" :label="'*'+$t('Security.userGroup') + '：'" :label-width="formLabelWidth">
        <el-select v-model="form.usergroupKey" size="mini" style="width: 200px"
                   :placeholder="$t('common.placeholder')">
          <el-option
            v-for="item in userGroupList"
            :key="item.usergroupKey"
            :label="item.usergroupName"
            :value="item.usergroupKey">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="phone" style="margin-bottom: -10px;" :label="$t('Security.telephone') + '：'" :label-width="formLabelWidth">
        <el-input type="text" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.phone"
                  v-on:blur="phoneCheck"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="address" style="margin-bottom: -10px;" :label="$t('Security.address') + '：'" :label-width="formLabelWidth">
        <el-input type="text" size='mini' :placeholder="$t('common.placeholder')"
                  v-model="form.address"
                  v-on:blur="addressCheck"
                  autocomplete="off"
                  style="width:200px"></el-input>
      </el-form-item>
      <el-form-item prop="enable" style="margin-bottom: -10px;" :label="$t('Security.lockedOption') + '：'" :label-width="formLabelWidth">
        <el-checkbox v-model="form.enableChecked"
                     style="width:200px; padding-right: 150px"
                     @change="checkboxChange">{{$t('Security.locked')}}</el-checkbox>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' type="primary" icon='el-icon-circle-check' @click="updateUserConfiguration">{{$t("common.ok")}}</el-button>
      <el-button size='mini' icon='el-icon-circle-close' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import UserService from '../../../domain/services/UserService'
import UserGroupService from '../../../domain/services/UserGroupService'
import legitimacyCheck from '../../../utils/legitimacyCheck'
import HelperUtil from '../../../utils/HelperUtil'

export default {
  props: ['command'],
  data: function () {
    return {
      updateInitList: this.command.target,
      userGroupList: [],
      updateUserVisible: false,
      formLabelWidth: '120px',
      form: {
        userName: '',
        userKey: '',
        usergroupName: '',
        usergroupKey: '',
        editFlag: 0,
        password: '123456',
        phone: '',
        address: '',
        modifyTime: '',
        delFlag: 0,
        lockFlag: 0,
        enableChecked: false
      }
    }
  },
  methods: {
    close () {
      this.command.done()
    },

    /***
      * @Description  : 点击锁定checkbox
      * @author       : zf
      * @date         : 2019/05/17 15:37
      */
    checkboxChange: function () {
      if (this.form.enableChecked) {
        this.form.lockFlag = 1
      } else {
        this.form.lockFlag = 0
      }
    },

    // ------------------------------------创建开始-------------------------------------------------------------------

    /***
       * @Description  : 获取全部用户组
       * @author       : zf
       * @date         : 2019/03/13 16:00
       */
    getAllUserGroups: function () {
      let _this = this
      UserGroupService.getAllUserGroups().then(result => {
        // 成功
        _this.userGroupList = Object.values(result)
      }).catch(err => {
        // 失败
        _this.InfoTip.errorTip(_this, err)
      })
    },

    updateUser () {
      this.getAllUserGroups()
      this.form.userKey = this.updateInitList.userKey
      this.form.userName = this.updateInitList.userName
      this.form.usergroupKey = this.updateInitList.usergroupKey
      this.form.editFlag = this.updateInitList.editFlag
      this.form.password = this.updateInitList.password
      this.form.phone = this.updateInitList.phone
      this.form.address = this.updateInitList.address
      this.form.modifyTime = this.updateInitList.modifyTime
      this.form.delFlag = this.updateInitList.delFlag
      this.form.lockFlag = this.updateInitList.lockFlag
      if (this.form.lockFlag === 1) {
        this.form.enableChecked = true
      } else {
        this.form.enableChecked = false
      }
    },

    /***
       * @Description  : 修改用户
       * @author       : zf
       * @date         : 2019/03/13 11:13
       */
    updateUserConfiguration: function () {
      let _this = this
      if (!(this.phoneCheck() && this.userNameCheck() && this.updateUserCheck(this.updateInitList, this.form) && this.userGroupCheck() && this.addressCheck())) {
        return false
      } else {
        let currentUser = {}
        currentUser['userKey'] = this.form.userKey
        currentUser['userName'] = this.form.userName
        currentUser['usergroupKey'] = this.form.usergroupKey
        currentUser['editFlag'] = this.form.editFlag
        currentUser['password'] = this.form.password
        currentUser['phone'] = this.form.phone
        currentUser['address'] = this.form.address
        currentUser['modifyTime'] = this.form.modifyTime
        currentUser['delFlag'] = this.form.delFlag
        if (this.form.enableChecked === true) {
          this.form.lockFlag = 1
        } else {
          this.form.lockFlag = 0
        }
        currentUser['lockFlag'] = this.form.lockFlag

        let loadingInstance = _this.Loading.openLoading()
        UserService.modifyUser(currentUser).then(result => {
          // 成功
          _this.Loading.closeLoading(loadingInstance)
          _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.MODIFY_CODE))
          _this.close()
        }).catch(err => {
          // 失败
          _this.Loading.closeLoading(loadingInstance)
          _this.InfoTip.errorTip(_this, err)
        })
      }
    },

    // ------------------------------------验证开始-------------------------------------------------------------------
    /**
       * @Description  ：用户名称校验： 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    userNameCheck: function () {
      let userCheck = legitimacyCheck()
      if (!userCheck.textNullCheck(this.form.userName)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('Security.userName'))
        return false
      } else {
        if (this.textLengthCheck(this.form.userName)) {
          return true
        } else {
          this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE), this.$t('Security.userName'))
          return false
        }
      }
    },

    /**
       * @Description  ：用户组名称校验： 不能为空
       * @author       :
       * @param        :
       * @return       :
       * @exception    :
       * @date         :
       */
    userGroupCheck: function () {
      let userCheck = legitimacyCheck()
      if (!userCheck.textNullCheck(this.form.usergroupKey)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('Security.userGroup'))
        return false
      } else {
        return true
      }
    },

    /**
       * @Description  ：文本框所输入的字符长度校验： 控制在20之内
       * @author       : bjh
       * @param        :
       * @return       :
       * @exception    :
       * @date         : 2019/1/15 16:09
       */
    textLengthCheck: function (text) {
      let userCheck = legitimacyCheck()
      if (!userCheck.textLengthCheck(text)) {
        return false
      }
      return true
    },

    /***
       * @Description  : 检验电话号码合法性
       * @author       : zf
       * @date         : 2019/03/18 14:29
       */
    phoneCheck: function () {
      let userCheck = legitimacyCheck()
      if (!userCheck.phoneNumberCheck(this.form.phone)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.PHONE_CODE))
        return false
      }
      return true
    },

    /***
       * @Description  : 检验地址合法性
       * @author       : zf
       * @date         : 2019/03/18 14:29
       */
    addressCheck: function () {
      let userCheck = legitimacyCheck()
      if (!userCheck.textLengthCheck(this.form.address)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.LENGTH_CODE), this.$t('Security.address'))
        return false
      }
      return true
    },

    /***
      * @Description  : 验证是否真正进行了修改
      * @author       : zf
      * @date         : 2019/05/10 09:42
      */
    updateUserCheck: function (before, after) {
      if (after.userKey === before.userKey &&
          after.userName === before.userName &&
          after.usergroupKey === before.usergroupKey &&
          after.phone === before.phone &&
          after.address === before.address &&
          after.lockFlag === before.lockFlag) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.UNUPDATE_CODE))
        return false
      } else {
        return true
      }
    }

    // ------------------------------------验证结束-------------------------------------------------------------------

  },

  created () {
  },

  mounted () {
    this.updateUserVisible = true
  }
}
</script>
