<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="600px">
    <el-form :model="dataForm" 
      :rules="dataRule" 
      ref="dataForm" 
      @keyup.enter.native="dataFormSubmit()" 
      label-width="80px" 
      size="mini"
      style="margin-left: 20px;width: 500px">
      <el-form-item label="部门名称" prop="departmentName">
        <el-input v-model="dataForm.departmentName"></el-input>
      </el-form-item>
      <el-form-item label="部门代码" prop="departmentCode">
        <el-input v-model="dataForm.departmentCode"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input v-model="dataForm.leader"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">关闭</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  // import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: undefined,
          departmentCode: '',
          departmentName: '',
          leader: ''
        },
        dataRule: {
          departmentName: [
            { required: true, message: '部门名称不能为空', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符之间', trigger: ['blur', 'change'] }
          ],
          departmentCode: [
            { required: true, message: '部门代码不能为空', trigger: 'blur' },
            // { type: 'string' , message: '以BM开头，后接三位数字', pattern: /^BM[0-9]{3}$/ }
          ],
          leader: [
            { required: true, message: '部门负责人不能为空', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符之间', trigger: ['blur', 'change'] }
          ]
        },
        tempKey: -666666, // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
        gender: [
          {
            value: 1,
            label: '男'
          },
          {
            value: 2,
            label: '女'
          }
        ],
        // pastures: [], // 牧场select
        departments: [], // 部门select
        jobs: [], // 岗位select
        roleList: []
      }
    },
    methods: {
      // resetForm () {
      //   this.dataForm.id = undefined
      //   this.dataForm.departmentCode = ''
      //   this.dataForm.departmentName = ''
      //   this.dataForm.leader = ''
      // },
      init (id) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        // 在role.vue调用该方法
        this.dataForm.id = id || 0
        if (!this.dataForm.id) {
          return;
        }
        this.$http({
          url: this.$http.adornUrl(`/sys/department/info/${this.dataForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dataForm.id = data.department.id
          this.dataForm.departmentCode = data.department.departmentCode
          this.dataForm.departmentName = data.department.departmentName
          this.dataForm.leader = data.department.leader
        })
        // this.$http({
        //   url: this.$http.adornUrl("/management/pasture/findAll"),
        //   method: "post"
        // }).then(async({ data }) => {
        //   // 牧场select
        //   data.all.forEach(pasture => {
        //     let item = {
        //       value: pasture.id,
        //       label: pasture.name
        //     }
        //     this.pastures.push(item)
        //   })
        //   // 部门select
        //   let departments = []
        //   let departmentR = await this.$http({
        //     url: this.$http.adornUrl("/sys/department/findAll"),
        //     method: "get"
        //   })
        //   departmentR.data.departmentList && (departments = departmentR.data.departmentList)
        //   departments.forEach(department => {
        //     let item = {
        //       value: department.departmentCode,
        //       label: department.departmentName
        //     }
        //     this.departments.push(item)
        //   })
        //   // 岗位select
        //   let jobs = []
        //   let jobR = await this.$http({
        //     url: this.$http.adornUrl("/sys/job/findAll"),
        //     method: "get"
        //   })
        //   jobR.data.jobList && (jobs = jobR.data.jobList)
        //   jobs.forEach(job => {
        //     let item = {
        //       value: job.jobCode,
        //       label: job.jobName
        //     }
        //     this.jobs.push(item)
        //   })
        //   // 获取status
        //   let roleR = await this.$http({
        //     url: this.$http.adornUrl('/sys/role/select'),
        //     method: 'get',
        //     params: this.$http.adornParams()
        //   })
        //   this.roleList = roleR.data && roleR.data.code === 0 ? roleR.data.list : []
        //   // 获取form的修改的东西
        //   if (!this.dataForm.userId) {
        //     return;
        //   }
        //   let formR = await this.$http({
        //     url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.userId}`),
        //     method: 'get',
        //     params: this.$http.adornParams()
        //   })
        //   // 修改操作：form表单完整
        //   console.log(formR.data.user);
        //   this.dataForm.username = formR.data.user.username
        //   this.dataForm.sex = formR.data.user.sex
        //   this.dataForm.birthday = formR.data.user.birthday
        //   this.dataForm.mobile = formR.data.user.mobile
        //   this.dataForm.email = formR.data.user.email
        //   this.dataForm.farmId = formR.data.user.farmId
        //   this.dataForm.departmentCode = formR.data.user.departmentCode
        //   this.dataForm.jobCode = formR.data.user.jobCode
        //   this.dataForm.address = formR.data.user.address
        //   this.dataForm.remarks = formR.data.user.remarks
        //   this.dataForm.roleIdList = formR.data.user.roleIdList
        //   this.dataForm.status = formR.data.user.status
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/department/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'departmentCode': this.dataForm.departmentCode,
                'departmentName': this.dataForm.departmentName,
                'leader': this.dataForm.leader
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$emit('refreshDataList')
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  // onClose: () => {
                  //   this.visible = false
                  //   this.$emit('refreshDataList')
                  // }
                })
                this.visible = false
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
