<template>
  <div class="container">
    <div class="container_top">
      <!-- :model 收集表单上的数据 -->
      <el-form
        :inline="true"
        ref="form"
        :model="reimebursementItemform"
        class="demo-form-inline"
      >
        <el-row
          v-for="(item, index) in reimebursementItemform.reimebursementItemlist"
          :key="item.index"
        >
          <el-col :xl="6" :lg="8" :md="8" :sm="10" :xs="24">
            <el-form-item
              :label="'报销项名称' + (index + 1)"
              :prop="'reimebursementItemlist.' + index + '.name'"
              :rules="{
                required: true,
                message: '名称不能为空',
                trigger: 'blur',
              }"
            >
              <el-input
                v-model.trim="item.name"
                placeholder="请输入新建项名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xl="12" :lg="12" :md="12" :sm="12" :xs="24">
            <el-form-item
              :label="'报销项说明' + (index + 1)"
              :prop="'reimebursementItemlist.' + index + '.explain'"
              :rules="{
                required: true,
                message: '内容不能为空',
                trigger: 'blur',
              }"
            >
              <div>
                <el-row :gutter="10">
                  <el-col :span="18">
                    <el-input
                      type="textarea"
                      :rows="5"
                      placeholder="请输入内容"
                      v-model.trim="item.explain"
                    >
                    </el-input>
                  </el-col>
                  <el-col :span="4">
                    <el-button @click.prevent="removeDomain(item)"
                      >删除</el-button
                    >
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-button
          type="primary"
          :disabled="reimebursementItemform.reimebursementItemlist.length == 0"
          @click="submitForm()"
          >提交</el-button
        >
        <el-button @click="addDomain">新增类型</el-button>
        <el-button @click="resetForm()">重置</el-button>
        <el-button @click="back()">返回</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { insert } from "@/api/reimburtypeList";

export default {
  data() {
    return {
      reimebursementItemform: {
        reimebursementItemlist: [
          {
            name: "",
            explain: "",
            companyId: this.$store.getters.companyId,
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    //提交
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let getl = [];

          console.log(this.reimebursementItemform.reimebursementItemlist);
          this.reimebursementItemform.reimebursementItemlist.forEach((e) => {
            getl.push(e.name);
          });
          var mySet = new Set(getl);
          console.log([...mySet]);
          if (getl.length == [...mySet].length) {
            console.log("11111");

            insert(this.reimebursementItemform.reimebursementItemlist)
              .then((res) => {
                if (res.success) {
                  this.$message.success("添加成功！");
                  this.back();
                } else {
                  this.$message.error(res.msg);
                }
              })
              .catch((error) => {
                console.log(error);
                this.$message.error("添加失败");
              });
          } else {
            console.log("no");
            this.$message({
              showClose: true,
              message: "相同类型的名称不可以相同",
              type: "warning",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 添加新建项
    addDomain() {
      this.reimebursementItemform.reimebursementItemlist.push({
        name: "",
        explain: "",
        companyId: this.$store.getters.companyId,
      });
    },
    removeDomain(item) {
      var index =
        this.reimebursementItemform.reimebursementItemlist.indexOf(item);
      if (index !== -1) {
        this.reimebursementItemform.reimebursementItemlist.splice(index, 1);
      }
    },
    // 返回上一级
    back() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push("reimburtypeList");
    },
    // 重置
    resetForm() {
      this.$refs.form.resetFields();
    },
  },
};
</script>

<style>
.container_top {
  margin: 15px 20px;
}
</style>