<template>
  <el-dialog
    class="ele-dialog-form"
    :title="'排班组'"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="500px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="排班组名称" prop="name" style="margin-bottom: 22px">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="班组" prop="teamIds" style="margin-bottom: 22px">
        <el-select
          v-model="form.teamIds"
          placeholder="请选择"
          style="width: 100%"
          multiple
        >
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
  
  <script>
import { getteampage } from '@/api/workforceManagement/team';
import { teamqueuesave } from '@/api/workforceManagement/schedule';
export default {
  data() {
    const defaultForm = {
      name: '',
      teamIds: []
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        teamIds: [{ required: true, message: '请输入', trigger: 'change' }]
      },
      visible: false,
      loading: false,
      options: []
    };
  },
  methods: {
    open() {
      this.visible = true;
      this.getteampage();
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        teamqueuesave(this.form)
          .then((data) => {
            this.handleClose();
            this.$store.commit('team/setAddInfo', data);
            this.$router.push({
              path: '/workforceManagement/schedule/detail',
              query: {
                type: 'add'
              }
            });
          })
          .catch((e) => {
            this.$message.error(e.message);
          })
          .finally(() => {
            this.loading = false;
          });
      });
    },
    // 重置表单
    restForm() {
      this.$refs.form.clearValidate();
      this.form = { ...this.defaultForm };
    },
    handleClose() {
      this.restForm();
      this.visible = false;
    },
    getteampage() {
      getteampage().then((res) => {
        this.options = res.list;
      });
    }
  }
};
</script>
  <style lang="scss" scoped>
.location-warp {
  display: flex;
  .detail {
    margin-left: 10px;
  }
}
</style>
  