<template>
  <div class="addGoodsType app-container" v-loading="loading1">
    <el-form
      :model="setQuery"
      ref="setQuery"
      :rules="rules"
      label-width="130px"
    >
      <el-form-item label="模版名称：" prop="setMealName">
        <el-input
          v-model="setQuery.setMealName"
          :maxlength="11"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属商品：" prop="setMealCode">
        <el-input
          v-model="setQuery.setMealCode"
          :maxlength="11"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item :label="'商品属性'+(index+1)+'：'" v-for="(item,index) in setQuery.list" :key="index"  :prop="'list.' + index + '.key'" :rules="{
      required: true, message: '商品属性不能为空', trigger: 'blur'
    }">
        <div class="item" >
          <el-input
            placeholder="请输入属性名称"
            v-model="item.key"
            class="item-width-300"
          >
          </el-input>
          <i class="icon el-icon-plus" @click="add(index,'list')"></i>
          <i class="icon el-icon-minus" @click="remove(index,'list')"></i>
        </div>
      </el-form-item>
      <el-form-item :label="'商品规格'+(index+1)+'：'" v-for="(item,index) in setQuery.list1" :key="index"  :prop="'list.' + index + '.key'" :rules="{
      required: true, message: '商品规格不能为空', trigger: 'blur'
    }">
        <div class="item" >
          <el-input
            placeholder="请输入属性名称"
            v-model="item.key"
            class="item-width-300"
          >
          </el-input>
          <i class="icon el-icon-plus" @click="add(index,'list1')"></i>
          <i class="icon el-icon-minus" @click="remove(index,'list1')"></i>
        </div>
      </el-form-item>
    </el-form>

    <div class="btn-group">
      <el-button>取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >确 定</el-button
      >
    </div>
  </div>
</template>

<script>
import { addOrUpdate } from "@/api/common";
export default {
  data() {
    return {
      loading: false,
      loading1: false,
      setQuery: {
        setMealCode: 0,
        setMealDetail: "",
        setMealId: 0,
        setMealName: "",
         list:[
            {
              key:''
            },
          ],
          list1:[
            {
              key:''
            },
          ],
      },
      rules: {
        setMealCode: [
          { required: true, message: "请输入套餐code", trigger: "blur" },
        ],
        setMealName: [
          { required: true, message: "请输入套餐名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    add(index){
      this.setQuery.list.splice(index+1,0,{key:''})
    },
    remove(index){
      if(this.setQuery.list.length == 1) return
      this.setQuery.list.splice(index,1)
    },
    save() {
      // this.loading = true;
      this.$refs.setQuery.validate((v) => {
        if (v) {
          // addOrUpdate(this.setQuery).then((res) => {
          //   this.loading = false;
          //   if (res.state == "Success") {
          //     this.$notify({
          //       title: "成功",
          //       message: "操作成功",
          //       type: "success",
          //     });
          //     this.setQuery = {
          //       setMealCode: 0,
          //       setMealDetail: "",
          //       setMealId: 0,
          //       setMealName: "",
          //     };
          //     this.$router.push("/operationManage/goodsType/index");
          //   }
          // });
        }
      });
    },
  },
  created () {
  },
};
</script>

<style lang="scss" scoped>
.addGoodsType {
  padding-bottom: 40px;
  .item{
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .icon{
      font-size: 18px;
      font-weight: bold;
      margin-left: 10px;
      cursor: pointer;
    }
  }
  .btn-group {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>