<!-- 用户编辑弹窗 -->
<template>
  <a-modal
    :width="680"
    :visible="visible"
    :confirm-loading="loading"
    :title="isUpdate?'修改用户':'新建用户'"
    :body-style="{paddingBottom: '8px'}"
    @update:visible="updateVisible"
    @ok="save">
    <a-form
      ref="form"
      :model="form"
      :rules="rules"
      :label-col="{md: {span: 6}, sm: {span: 24}}"
      :wrapper-col="{md: {span: 17}, sm: {span: 24}}">
      <a-form-item
        label="头像:"
        name="avatar"
        :label-col="{sm: {span: 3}, xs: {span: 6}}"
        :wrapper-col="{sm: {span: 21}, xs: {span: 18}}">
        <uploadImage :limit="1" :updDir="updDir" v-model:value="form.avatar"/>
      </a-form-item>
      <a-row :gutter="16">
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="用户编号:" name="code">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入用户编号"
              v-model:value="form.code"/>
          </a-form-item>
          <a-form-item label="所属部门:" name="deptId">
            <a-tree-select
              allow-clear
              :tree-data="deptList"
              tree-default-expand-all
              placeholder="请选择所属部门"
              v-model:value="form.deptId"
              :dropdown-style="{maxHeight: '360px', overflow: 'auto'}"/>
          </a-form-item>
          <a-form-item label="用户账号:" name="username">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入用户账号"
              v-model:value="form.username"/>
          </a-form-item>
          <a-form-item label="角色:" name="roleIds">
            <a-select
              allow-clear
              mode="multiple"
              placeholder="请选择角色"
              v-model:value="form.roleIds">
              <a-select-option
                v-for="item in roleList"
                :key="item.id"
                :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item
            label="驾照类型："
            name="driverType"
            v-if="form.type==2">
            <a-select
              v-model:value="form.driverType"
              placeholder="请选择驾照类型："
              allow-clear>
              <a-select-option :value="1">飞行执照</a-select-option>
              <a-select-option :value="2">飞行合格证</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :md="12" :sm="24" :xs="24">
          <a-form-item label="用户姓名:" name="realname">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入用户姓名"
              v-model:value="form.realname"/>
          </a-form-item>
          <a-form-item label="状态" name="status">
            <a-radio-group
              v-model:value="form.status">
              <a-radio :value="1">正常</a-radio>
              <a-radio :value="2">禁用</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item
            label="登录密码:"
            name="password">
            <a-input-password
              :maxlength="20"
              placeholder="请输入登录密码"
              v-model:value="form.password"/>
          </a-form-item>
          <a-form-item label="用户类型" name="type">
            <a-radio-group
              v-model:value="form.type">
              <a-radio :value="1">普通用户</a-radio>
              <a-radio :value="2">飞手</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item 
            v-if="form.type==2"
            label="驾照编号:" 
            name="driverCode">
            <a-input
              allow-clear
              :maxlength="20"
              placeholder="请输入驾照编号"
              v-model:value="form.driverCode"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item
        label="备注:"
        :label-col="{sm: {span: 3}, xs: {span: 6}}"
        :wrapper-col="{sm: {span: 21}, xs: {span: 18}}">
        <a-textarea
          :rows="3"
          :maxlength="200"
          placeholder="请输入备注"
          v-model:value="form.intro"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import validate from 'ele-admin-pro/packages/validate';
import uploadImage from '@/components/uploadImage'
import regions from 'ele-admin-pro/packages/regions.js';

export default {
  name: 'UserEdit',
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        code: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ],
        realname: [
          {required: true, message: '请输入用户姓名', type: 'string', trigger: 'blur'}
        ],
        deptId: [
          {required: true, message: '请选择部门', type: 'number', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择状态', type: 'number', trigger: 'blur'}
        ],
        roleIds: [
          {required: true, message: '请选择角色', type: 'array', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入用户账号', type: 'string', trigger: 'blur'}
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 图片上传目录
      updDir: 'user',
      // 角色列表
      roleList: [],
      // 部门列表
      deptList: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data, {
          roleIds: this.data.roles.map(d => d.id)
        });
        // 清空密码输入框
        this.form.password = ''
        this.isUpdate = true;
      } else {
        this.form = {status: 1, type: 1};
        this.isUpdate = false;
      }
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
    }
  },
  mounted() {
    this.queryRoles();  // 查询角色列表
    this.queryDeptList(); // 查询部门列表
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs.form.validate().then(() => {
        this.loading = true;
        this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/user/edit' : '/user/add', this.form).then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            if (!this.isUpdate) {
              this.form = {};
            }
            this.updateVisible(false);
            this.$emit('done');
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /* 查询角色列表 */
    queryRoles() {
      this.$http.get('/role/getRoleList').then(res => {
        if (res.data.code === 0) {
          this.roleList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取部门列表
     */
    queryDeptList() {
      this.$http.get('/dept/getDeptList').then(res => {
        if (res.data.code === 0) {
          this.deptList = this.$util.toTreeData(res.data.data.map(d => {
            d.key = d.id;
            d.value = d.id;
            d.title = d.name;
            return d;
          }), 'id', 'pid');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
</style>
