<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'" :visible.sync="dialog" width="450px" :close-on-click-modal="false"
               top="50px">
      <el-form ref="form" :model="form" label-width="100px" class="bind-phone">
        <el-form-item v-if="!exData.id" label="项目选择：">
          <el-select style="width: 100%;" v-model="form.user_project_id">
            <el-option v-for="(item, index) in projectArr" :key="index" :label="item.project_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.user_project_id" label="部门选择：">
          <el-select style="width: 100%;" v-model="form.user_department_id">
            <el-option v-for="(item, index) in departmentArr" :key="index" :label="item.department_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.user_department_id" label="角色选择：">
          <el-select style="width: 100%;" v-model="form.user_role_id">
            <el-option v-for="(item, index) in roleArr" :key="index" :label="item.role_name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号：">
          <el-input v-model="form.user_name" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item label="姓名：">
          <el-input v-model="form.user_full_name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item v-if="!exData.id" label="是否管理员：">
          <el-select style="width: 100%;" v-model="form.is_admin">
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号：">
          <el-input v-model="form.user_phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
          <el-input v-model="form.user_email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item v-if="!exData.id" label="密码：">
          <el-input v-model="form.user_password" placeholder="请输入密码" type="password"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="danger" :disabled="booLock" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  computed: {
    ...mapGetters([
      'projectArr'
    ])
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        is_admin: 0,
        user_project_id: undefined,
        user_department_id: undefined,
        user_role_id: undefined,
        user_name: '',
        user_full_name: '',
        user_phone: '',
        user_email: '',
        user_password: '',
      },
      departmentArr: [],
      roleArr: [],
      booLock: false
    }
  },
  methods: {
    async handleSubmit () {
      let url = this.exData.id ? '/user/edit' : '/user/add'
      this.form.user_phone = this.form.user_phone.replace(/ /g, '')
      this.booLock = true
      const data = await this.$fetch('/api/auth' + url, { ...this.form })
      this.booLock = false
      if (data.code === 200) {
        this.$message.success('提交成功')
        this.$emit('success')
        this.dialog = false
      }
    },
    async getDepartmentArr (project_id) {
      const data = await this.$fetch('/api/auth/department/get_department_list', { project_id }, 'get')
      if (data.code == 200) {
        this.departmentArr = data.data
      }
    },
    async getRoleArr (department_id) {
      const data = await this.$fetch('/api/auth/role/get_role_list', { department_id }, 'get')
      if (data.code == 200) {
        this.roleArr = data.data
      }
    }
  },
  async mounted () {
    if (this.exData.id) {
      // await this.getDepartmentArr()
      // await this.getRoleArr()
      // delete this.form.user_project_id
      // delete this.form.user_department_id
      // delete this.form.user_role_id
      this.form.id = this.exData.id
      for (const key in this.form) {
        this.form[key] = this.exData[key]
      }
      delete this.form.is_admin
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    },
    'form.user_project_id' (newValue, oldValue) {
      this.getDepartmentArr(newValue)
      if (oldValue != undefined) {
        this.form.user_department_id = undefined
        this.form.user_role_id = undefined
      }
    },
    'form.user_department_id' (newValue, oldValue) {
      this.getRoleArr(newValue)
      if (oldValue != undefined) {
        this.form.user_role_id = undefined
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
