<template>
  <el-form ref="form" :rules="rules" label-width="100px" :model="form">
    <el-form-item label="员工" prop="workerId" v-if='!isEdit'>
      <el-select v-model="form.workerId" placeholder="请选择" filterable style="width: 100%;" @change="selectWorker()">
        <el-option
          v-for="item in workerList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="员工" prop="workerId" v-if='isEdit'>
      <el-input v-model="workName" disabled></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="majorName">
      <el-input v-model="form.majorName" disabled></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="form.remark"></el-input>
    </el-form-item>
  </el-form>
</template>
<script>
import { otherWorkerList } from '@/api/base/worker'
import { teamDetCreate, teamDetUpdate, groupTeamDet } from '@/api/base/groupTeam'
export default {
  name: 'WorkerTeamAdd',
  data() {
    return {
      workerList: [],
      form: {
        teamId: '',
        workerId: '',
        remark: '',
        majorName: '',
        id: ''
      },
      isEdit: false,
      workName: '',//编辑时用
      rules: {
        workerId: [{ required: true, message: '员工不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(param) {
      this.form.teamId = param.teamId
      this.form.majorName = param.majorName
      this.workName = param.workName
      otherWorkerList({teamId:this.form.teamId}).then(res => {
        this.workerList = res.data || []
        if (param.id) {
          this.isEdit = true
          this.form.id = param.id
          groupTeamDet({id: this.form.id}).then((res) => {
            if (res.code === 0) {
              this.form.workerId = res.data.workerId
              this.form.remark = res.data.remark
            }
          })
        } else {
          this.isEdit = false
          this.form.id = ''
        }
      })
    },
    selectWorker() {
      if (this.form.workerId) {
        this.workerList.map(item => {
          if (item.id === this.form.workerId) {
            this.form.majorName = item.majorName
          }
        })
      }else{
        this.form.majorName = ''
      }
    },
    // 确定
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 编辑
            teamDetUpdate({
              teamId: this.form.teamId,
              workerId: this.form.workerId,
              remark: this.form.remark,
              id: this.form.id
            }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          } else {
            teamDetCreate({
              teamId: this.form.teamId,
              workerId: this.form.workerId,
              remark: this.form.remark
            }).then((res) => {
              if (res.code === 0) {
                this.$modal.msgSuccess("操作成功");
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.workName = ''
      this.form.majorName = ''
      this.isEdit = false
    }
  }
}
</script>