<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-row>
      <el-col :span="12">
        <el-form-item label="班组名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="班组编码" prop="code">
          <el-input v-model="form.code" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="班组组长" prop="leaderName">
          <el-input v-model="form.leaderName" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号" prop="leaderPhone">
          <el-input v-model="form.leaderPhone" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="班组人数" prop="num">
          <el-input-number style="width: 100%;" v-model="form.num" :step="1" step-strictly></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getGroupTeam, updateGroupTeam, createGroupTeam, getCode } from '@/api/base/groupTeam'
export default {
  name: 'groupTeamAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        leaderName: '',
        leaderPhone: '',
        num: ''
      },
      isEdit: false, //是否是编辑
      rules: {
        name: [{ required: true, message: '请输入班组名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入班组编码', trigger: 'blur' }],
        leaderName: [{ 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>
