<template>
  <el-form ref="form" :rules="rules" label-width="80px" :model="form">
    <el-form-item label="班组名称" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="班组编码" prop="code">
      <el-input v-model="form.code" disabled></el-input>
    </el-form-item>
    <el-form-item label="班组人数" prop="num">
      <el-input v-model="form.num"></el-input>
    </el-form-item>
    <el-form-item label="班组组长" prop="leaderName">
      <el-input v-model="form.leaderName"></el-input>
    </el-form-item>
  </el-form>
</template>
<script>
import { getGroupTeam, updateGroupTeam, createGroupTeam, getCode } from '@/api/base/groupTeam'
export default {
  name: 'groupTeamAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        num: null,
        leaderName: ''
      },
      isEdit: false, //是否是编辑
      rules: {
        name: [{ required: true, message: '请输入班组名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        getGroupTeam( id ).then((res) => {
          if (res.code === 0) {
            this.form = res.data
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
        getCode().then((res) => {
          this.form.code = res.data
        })
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            //编辑
            updateGroupTeam({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            createGroupTeam({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
