<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="leaderId">
          <el-select v-model="form.leaderId" placeholder="请选择" style="width: 100%;" filterable @change="selectLeader">
            <el-option
              v-for="item in workerList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="form.telephone" disabled></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getGroupTeam, updateGroupTeam, createGroupTeam, getCode } from '@/api/base/groupTeam'
import { getWorkerList } from '@/api/base/worker'
export default {
  name: 'groupTeamAdd',
  data() {
    return {
      form: {
        id: '',
        name: '',
        code: '',
        leaderId: '',
        telephone: ''
      },
      isEdit: false, //是否是编辑
      rules: {
        name: [{ required: true, message: '请输入班组名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入班组编码', trigger: 'blur' }],
        leaderId: [{ required: true, message: '请选择组长', trigger: 'select' }]
      },
      workerList: []
    }
  },
  methods: {
    init(id) {
      this.getWorkerList()
      if (id) {
        this.isEdit = true
        this.form.id = id
        getGroupTeam( id ).then((res) => {
          if (res.code === 0) {
            this.form = res.data
            this.selectLeader()
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
        getCode().then((res) => {
          this.form.code = res.data
        })
      }
    },
    // 获取员工list
    getWorkerList() {
      getWorkerList().then(res => {
        this.workerList = res.data
      })
    },
    // 获取手机号
    selectLeader() {
      if (this.form.leaderId) {
        this.workerList.map(item => {
          if (item.id === this.form.leaderId) {
            this.form.telephone = item.telephone || ''
          }
        })
      } else {
        this.form.telephone = ''
      }
    },
    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>
