<template>
  <div class="editCombo 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"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="套餐code：" prop="setMealCode">
        <el-input
          v-model="setQuery.setMealCode"
          class="item-width-300"
        ></el-input>
      </el-form-item>
       <el-form-item label="折扣参数：" prop="discount">
        <el-input
        type="number"
          v-model.number="setQuery.discount"
          class="item-width-300"
        ></el-input>
      </el-form-item>
      <el-form-item label="套餐颜色：" prop="colour">
        <el-input
          v-model="setQuery.colour"
          class="item-width-300"
          placeholder="请输入#FFF或rgb(255,255,255)格式"
        ></el-input>
      </el-form-item>
      <el-form-item label="套餐背景颜色：" prop="backColour">
        <el-input
          v-model="setQuery.backColour"
          class="item-width-300"
          placeholder="请输入#FFF或rgb(255,255,255)格式"
        ></el-input>
      </el-form-item>
       <el-form-item label="套餐图片：" prop="imgUrl">
        <Upload
          type="imgUrl"
          id="imgUrl"
          width="150px"
          height="150px"
          uploadType="OTHER"
          :imgUrl="setQuery.imgUrl"
          @uploadEnd="uploadEnd"
        ></Upload>
      </el-form-item>
       <el-form-item label="套餐背景图片：" prop="backImg">
        <Upload
          type="backImg"
          id="backImg"
          width="150px"
          height="150px"
          uploadType="OTHER"
          :imgUrl="setQuery.backImg"
          @uploadEnd="uploadEnd"
        ></Upload>
      </el-form-item>
      <el-form-item label="套餐说明：">
        <el-input
          type="textarea"
          placeholder="请输入套餐说明"
          v-model="setQuery.setMealDetail"
          maxlength="400"
          :rows="6"
          class="item-width-500"
          show-word-limit
        >
        </el-input>
      </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,detail } from "@/api/common";
import Upload from '@/components/Upload'

export default {
   components: {
        Upload,
    },
  data() {
    return {
      loading: false,
      loading1: false,
      mode:'',
      setQuery: {
        setMealCode: 0,
        setMealDetail: "",
        setMealId: 0,
        setMealName: "",
         discount: 0,
        imgUrl: "",
        backImg:"",
        colour:'',
        backColour:'',
      },
      rules: {
        setMealCode: [
          { required: true, message: "请输入套餐code", trigger: "blur" },
        ],
        setMealName: [
          { required: true, message: "请输入套餐名称", trigger: "blur" },
        ],
         colour: [
          { required: true, message: "请输入套餐颜色", trigger: "blur" },
        ],
         backColour: [
          { required: true, message: "请输入套餐背景颜色", trigger: "blur" },
        ],
         imgUrl: [
          { required: true, message: "请上传套餐图片", trigger: "change" },
        ],
         backImg: [
          { required: true, message: "请上传套餐背景图片", trigger: "change" },
        ],
      },
    };
  },
  methods: {
     uploadEnd(val){
      this.setQuery[val.type] = val.url
    },
    detail(){
        this.loading1 = true
        detail({setMealId:this.$route.query.id}).then(res=>{
            this.loading1 = false
            if (res.state == "Success") {
                this.setQuery.setMealId = res.content.id
                this.setQuery.setMealCode = res.content.setMealCode
                this.setQuery.setMealDetail = res.content.setMealDetail
                this.setQuery.setMealName = res.content.setMealName
                this.setQuery.imgUrl = res.content.imgUrl
                this.setQuery.colour = res.content.colour
                this.setQuery.backColour = res.content.backColour
                this.setQuery.discount = res.content.discount
                this.setQuery.backImg = res.content.backImg
                
            }
        })
    },
    save() {
      this.$refs.setQuery.validate((v) => {
        if (v) {
          this.loading = true;
          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/comboManage/index");
            }
          }).catch(()=>{
            this.loading = false;

          })
        }
      });
    },
  },
  created () {
    if(this.$route.query.id){
      this.mode = 'edit'
      this.detail();
    }else{
      this.mode = 'add'

    }
  },
};
</script>

<style lang="scss" scoped>
.editCombo {
  padding-bottom: 40px;
  .btn-group {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>