<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" @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) {
          insert(this.reimebursementItemform.reimebursementItemlist)
            .then((res) => {
              if (res.success) {
                this.$message.success("添加成功！");
                this.back()
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((error) => {
              this.$message.error("添加失败");
            });
        } 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>