<template>
  <div>
    <el-form :model="courseTimeForm" ref="courseTimeForm">
      <el-form-item
        label="分部"
        prop="organId"
        :label-width="formLabelWidth"
        :rules="[{ required: true, message: '请选择分部', trigger: 'blur' }]"
      >
        <el-select
          style="width: 100% !important"
          class="multiple"
          v-model.trim="courseTimeForm.organId"
          filterable
          clearable
          placeholder="请选择分部"
        >
          <el-option
            v-for="(item, index) in organList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="课程类型"
        prop="courseType"
        :label-width="formLabelWidth"
        :rules="[
          { required: true, message: '请选择课程类型', trigger: 'blur' },
        ]"
      >
        <el-select
          style="width: 100% !important"
          class="multiple"
          v-model.trim="courseTimeForm.courseType"
          filterable
          clearable
          placeholder="课程类型"
        >
          <el-option
            v-for="(item, index) in courseType"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程课时" prop="timer" :label-width="formLabelWidth">
        <el-tag
          :key="index"
          effect="dark"
          type="info"
          v-for="(tag, index) in dynamicTags"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)"
        >
          {{ tag }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model.trim="inputValue"
          ref="saveTagInput"
          style="width: 100px"
          size="small"
          @keyup.enter.native="handleInputConfirm"
        >
        </el-input>
        <el-button
          v-if="!inputVisible"
          class="button-new-tag"
          size="small"
          @click="showInput"
          >+ 添加</el-button
        >
        <el-button
          v-if="inputVisible"
          class="button-new-tag"
          size="small"
          @click="handleInputConfirm"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { resetOrganizationCourseDurationSettings,addOrganizationCourseDurationSettings } from "@/api/specialSetting";
export default {
  props: ["activeRow", "organList", "courseType"],
  data() {
    return {
      courseTimeForm: {
        organId: "",
        courseType: "",
        timer: [],
        id: "",
      },
      inputVisible: false,
      formLabelWidth: "80px",
      dynamicTags: [],
      inputValue: "",
    };
  },
  mounted() {
    console.log(this.activeRow);
    if (this.activeRow) {
      this.courseTimeForm.organId = this.activeRow.organId;
      this.courseTimeForm.timer = this.activeRow.duration.split(",");
      this.dynamicTags = this.activeRow.duration.split(",");
      this.courseTimeForm.courseType = this.activeRow.courseType;
      this.courseTimeForm.id = this.activeRow.id;
    }
  },
  methods: {
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    async submitInfo(str) {
      console.log(str);
      this.$refs.courseTimeForm.validate(async (_) => {
        if (_) {
          if (this.dynamicTags.length <= 0) {
            this.$message.error("请至少填写一个课程课时");
            return;
          }
          if (str == "update") {
            try {
              const res = await resetOrganizationCourseDurationSettings({
                organId: this.courseTimeForm.organId,
                duration: this.dynamicTags.join(","),
                courseType: this.courseTimeForm.courseType,
                id: this.courseTimeForm.id,
              });
                this.$message.success("修改成功");
                this.$emit('close')
            } catch {}
          }else if(str == "create"){
                   try {
              const res = await addOrganizationCourseDurationSettings({
                organId: this.courseTimeForm.organId,
                duration: this.dynamicTags.join(","),
                courseType: this.courseTimeForm.courseType,
              });
                this.$message.success("新建成功");
                this.$emit('close')
            } catch {}
          }
        }
      });

    
    },
  },
  watch: {
    dynamicTags(val) {
      this.courseTimeForm.timer = val;
    },
  },
};
</script>
<style lang="scss" scoped>
.courseMask {
  .el-tag.el-tag--info {
    margin-right: 4px;
  }
}
</style>