<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <a-form-model ref="form" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="validatorRules" :model="model">
      <a-form-model-item label="员工姓名:" prop="realName">
        <a-input v-model="model.realName" />
      </a-form-model-item>
      <a-form-model-item label="登录账号:" prop="userName">
        <a-input v-model="model.userName" />
      </a-form-model-item>
      <a-form-model-item label="所属部门" prop="deptId">
        <a-tree-select
          v-model="model.deptId"
          allow-clear
          tree-default-expand-all
          style="width: 100%"
          :tree-data="treeData"
          :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
        />
      </a-form-model-item>
      <a-form-model-item label="岗位类型" prop="post">
        <j-dict-select-tag
          v-model="model.post"
          dict-code="postType"
        />
      </a-form-model-item>
      <a-form-model-item label="职工职务" prop="roleIds">
        <sys-list-select-tag
          v-model="model.roleIds"
          mode="multiple"
          dict-code="role"
        />
      </a-form-model-item>
      <a-form-model-item label="职工工号" prop="nickName">
        <a-input v-model="model.nickName" />
      </a-form-model-item>
      <a-form-model-item label="手机号码" prop="phone">
        <a-input v-model="model.phone" />
      </a-form-model-item>
      <a-form-model-item label="微信账号">
        <a-input v-model="model.wechat" />
      </a-form-model-item>
      <a-form-model-item label="邮箱地址">
        <a-input v-model="model.email" />
      </a-form-model-item>
      <a-form-model-item label="出生日期">
        <a-date-picker v-model="model.birthday" style="width: 100%" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="请选择" />
      </a-form-model-item>
      <a-form-model-item label="性别">
        <j-dict-select-tag
          v-model="model.sex"
          type="radioButton"
          dict-code="sys_user_sex"
        />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { getAction, httpAction } from '@/api/request'
import JModal from '@/components/JModal'

export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      treeData: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        userName: [{ required: true, message: '请输入' }],
        realName: [{ required: true, message: '请输入' }],
        deptId: [{ required: true, message: '请选择' }],
        nickName: [{ required: true, message: '请选择' }],
        roleIds: [{ required: true, message: '请选择' }],
        post: [{ required: true, message: '请选择' }],
        phone: [{ required: true, message: '请输入' }]
      },
      url: {
        add: '/system/user',
        edit: '/system/user',
        tree: '/system/dept/treeSelect',
        roleIds: '/system/user/authRole/'
      }
    }
  },
  created() {
  },
  methods: {
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    add() {
      this.model = {}
      this.visible = true
      this.loadTree()
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      this.loadTree()
      this.getRoleIds()
    },
    close(isSubmit) {
      if (isSubmit) {
        this.checkData()
      } else {
        this.visible = false
      }
    },
    checkData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveData()
        } else {
          return false
        }
      })
    },
    saveData() {
      let url, type
      if (!this.model.userId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      httpAction(url, this.model, type).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$emit('ok')
          this.visible = false
        } else {
          console.log(res)
        }
      })
    },
    getRoleIds() {
      getAction(this.url.roleIds + this.model.userId).then((res) => {
        if (res.code === 200) {
          this.model.roleIds = res.user.roleIds
        }
      })
    }
  }
}
</script>
