<template>
  <el-drawer
    :title="title"
    :modal-append-to-body="false"
    :modal="false"
    :wrapper-closable="false"
    size="35%"
    :visible.sync="dialogVisible"
  >
    <div class="content-container">
      <vuescroll :ops="ops" style="height: calc(100vh - 250px)">
        <el-form ref="ruleForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
          <el-form-item label="账号" prop="accountName">
            <el-input v-model="formData.accountName" placeholder="输入账号" />
          </el-form-item>

          <el-form-item label="所在部门" prop="positionId">
            <group-selector v-model="formData.groupId" />
          </el-form-item>

          <el-form-item label="密码">
            <span style="color: #eeeeee">初始化密码为 888888</span>
          </el-form-item>

          <el-form-item label="姓名" prop="accountRealName">
            <el-input v-model="formData.accountRealName" placeholder="输入姓名" />
          </el-form-item>

          <el-form-item label="工号" prop="accountStaffNo">
            <el-input v-model="formData.accountStaffNo" max="11" placeholder="输入工号" />
          </el-form-item>

          <el-form-item v-show="viewData.isFixed!==1" label="岗位" prop="positionId">
            <position-selector v-model="formData.positionId" />
          </el-form-item>

          <el-form-item label="手机号">
            <el-input v-model="formData.accountPhone" max="11" placeholder="输入手机号码" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="formData.accountIntro" type="textarea" rows="6" max="127" placeholder="备注" />
          </el-form-item>
        </el-form>

        <div class="btn-group">
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <el-button class="cancel-btn" @click="dialogVisible = false">取消</el-button>
        </div>
      </vuescroll>
    </div>

  </el-drawer>
</template>

<script>
import { GroupSelector, PositionSelector } from '@/components'
import { getUserById, createUser, updateUser } from '@/api/system/userApi'
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import vuescroll from 'vuescroll'

export default {
  name: 'User',
  components: { vuescroll, PositionSelector, GroupSelector },
  props: {
    title: {
      type: String,
      default: '添加用户'
    }
  },
  data() {
    return {
      ops: {
        bar: {
          keepShow: false,
          onlyShowBarOnScroll: false
        }
      },
      dialogVisible: false,
      formData: {
        gmId: undefined,
        gmpId: undefined,
        accountId: undefined, // 帐号ID
        groupId: undefined, // 所属部门
        groupName: '',
        accountName: '', // 帐号名称
        password: '888888', // 密码
        accountRealName: '', // 姓名
        accountStaffNo: '', // 工号
        accountPhone: '', // 手机号
        positionId: undefined, // 岗位,
        accountIntro: ''
      },
      viewData: {
        parentName: '',
        groupName: '',
        isFixed: 0
      },
      rules: {
        accountName: [
          { required: true, message: '请选择帐号名称', trigger: 'blur' }
        ],
        accountRealName: [
          { required: false, message: '请选择姓名', trigger: 'blur' }
        ],
        accountPhone: [
          { required: false, message: '请输入手机号', trigger: 'blur' }
        ],
        accountStaffNo: [
          { required: false, message: '请输入工号', trigger: 'blur' }
        ],
        positionId: [
          { required: true, message: '请选择岗位', trigger: 'blur' }
        ]
      },
      actionType: ''
    }
  },
  mounted() {},
  methods: {
    // Show Add Model Dialog
    showAddModel(groupId, groupName) {
      this.actionType = ACTION_ADD
      this.resetFormData()
      this.dialogVisible = true
      this.formData.groupId = groupId
      this.formData.groupName = groupName
    },

    // Show Edit Model
    showEditModel(groupId, userId) {
      this.actionType = ACTION_UPDATE
      this.dialogVisible = true
      getUserById(groupId, userId).then((resp) => {
        const { data } = resp
        this.formData = data
        this.viewData.isFixed = data.isFixed
      }).catch((error) => {
        console.log(error)
      })
    },

    // Init Form Data
    resetFormData() {
      this.formData = {
        accountId: undefined, // 帐号ID
        groupId: undefined, // 所属部门
        accountName: '', // 帐号名称
        password: '888888', // 密码
        accountRealName: '', // 姓名
        accountPhone: '', // 手机号
        positionId: undefined// 岗位
      }
    },

    // Submit Form
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleActionCommand()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // Handle 'ACTION' Command
    handleActionCommand() {
      const actionType = this.actionType
      switch (actionType) {
        case ACTION_ADD:
          createUser(this.formData).then((resp) => {
            const { msg } = resp
            this.dialogVisible = false
            this.$emit('formSuccess')
            this.$message.success(msg)
          }).catch((error) => {
            console.log(error)
          })
          break

        case ACTION_UPDATE:
          updateUser(this.formData).then((resp) => {
            const { msg } = resp
            this.dialogVisible = false
            this.$message.success(msg)
            this.$emit('formSuccess')
          }).catch((error) => {
            console.log(error)
          })
          break
      }
    },

    // Reset From Fields
    resetFormField(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss">

</style>
<style lang="scss" scoped>
.content-container {
  padding: 10px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;

  .btn-group {
    padding-left: 90px;
  }
}
</style>
