<template>
  <div class="app-container">
    <el-form
      :model="form"
      :rules="rules"
      ref="form"
      label-width="100px"
      align="center"
      :inline="false"
      size="normal"
    >
      <el-form-item prop="teamId">
        <el-select v-model="form.teamId">
          <el-option
            v-for="item in form.masterTeam"
            :key="item.value"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          class="btn"
          @click="submitHandler"
          icon="el-icon-success"
          >绑定班组</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { submitNodeForm } from "@/api/bpmprocess/runDialogApi/index";
export default {
  name: "AssignEmployees",
  props: ["formData", "row"],
  data() {
    return {
      isEdit: false,
      form: {
        teamId: "",
      },
      rules: {
        teamId: [{ required: true, trigger: "change" }],
      },
    };
  },
  computed: {
    myFormData() {
      return this.formData;
    },
  },
  watch: {
    myFormData: {
      handler(nval) {
        this.form = nval.resultMap;
        this.isEdit = this.form.taskProcessKey && this.form.taskNodeKey;
      },
      deep: true,
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    async submitHandler() {
      console.log(this.row);
      let res = await this.$refs.form?.validate();
      if (res) {
        let payLoad = {};
        let { teamId } = this.form;
        if (this.isEdit) {
          let { taskProcessKey, taskNodeKey } = this.myFormData.resultMap;
          payLoad.updateCommonEntityList = [
            {
              basicMap: {
                tableName: "task_team_relevance",
              },
              conditionMap: {
                task_process_key: taskProcessKey,
                task_node_key: taskNodeKey,
              },
              commMap: {
                team_id: teamId,
                task_id: this.row.bepTaskPlanKey,
              },
            },
          ];
        } else {
          payLoad.insertCommonEntityList = [
            {
              basicMap: {
                tableName: "task_team_relevance",
              },
              addListMap: [
                {
                  team_id: teamId,
                  task_id: this.row.bepTaskPlanKey,
                  taskProcessKey: this.row.benTaskProcessKey,
                  taskNodeKey: this.row.benTaskNodeKey,
                },
              ],
            },
          ];
        }
        submitNodeForm(payLoad).then((response) => {
          if (response.code == 200) {
            this.$modal.msgSuccess("保存成功");
          } else {
            this.$modal.error("保存失败,请稍后再试");
          }
        });
      }
    },
    async getFormData() {
      let formData = {
        flag: false,
        msg: "",
      };
      try {
        let valid = await this.$refs.form.validate();
        if (valid) {
          formData.flag = true;
          formData.data = this.form;
          return formData;
        } else {
          formData.msg = "表单校验异常,请规范填写表单数据";
          return formData;
        }
      } catch (error) {
        // console.log(error);
        formData.msg = "表单校验异常,请规范填写表单数据";
        return formData;
      }
    },
  },

};
</script>

<style lang="scss" scoped>
.app-container {
  .btn {
    background: rgb(123, 213, 195);
    border: rgb(123, 213, 195);
    float: right;
  }
}

::v-deep .el-select {
  width: 100%;
}
</style>
