<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类" prop="category">
              <SelectCategory v-model="form.category" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="规格" prop="spec">
              <SelectSpecs v-model="form.spec" ref="selectSpecs" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="制作时长" prop="takeTime">
              <el-input v-model="form.takeTime" placeholder="请输入制作时长" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="折扣价" prop="discount">
              <el-input v-model="form.discount" placeholder="请输入折扣价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原价" prop="price">
              <el-input v-model="form.price" placeholder="请输入原价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="缩略图点单屏" prop="thumbnail">
              <ImageUpload :value="form.pics.thumbnail" v-model="form.pics.thumbnail" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="缩略图小程序" prop="thumbnailApplet">
              <ImageUpload :value="form.pics.thumbnailApplet" v-model="form.pics.thumbnailApplet" :limit="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="介绍图点单屏" prop="introImages">
              <ImageUpload :value="form.pics.introImages" v-model="form.pics.introImages" :limit="3" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="介绍图小程序" prop="introImagesApplet">
              <ImageUpload :value="form.pics.introImagesApplet" v-model="form.pics.introImagesApplet" :limit="3" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="内容图点单屏" prop="detailImages">
              <ImageUpload :value="form.pics.detailImages" v-model="form.pics.detailImages" :limit="3" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内容图小程序" prop="detailImagesApplet">
              <ImageUpload :value="form.pics.detailImagesApplet" v-model="form.pics.detailImagesApplet" :limit="3" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="标签" prop="label">
              <el-input v-model="form.label" placeholder="请输入标签" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="描述" prop="desc">
              <el-input v-model="form.desc" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSpu, getSpu, updateSpu } from "@/api/system/goods";
import SelectCategory from "@/components/SelectCategory";
import SelectSpecs from "@/components/SelectSpecs";
import { setSpec } from "./href";
export default {
  components: { SelectCategory, SelectSpecs },
  data() {
    return {
      title: "",
      specNames: "",
      // 表单参数
      form: {
        pics: {},
        spec: [],
      },
      open: false,
      // 表单校验
      rules: {
        spuName: [{ required: true, message: "不能为空", trigger: "blur" }],
        status: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getSpu(id).then((response) => {
        const { data } = response;
        data.pics = JSON.parse(data.pics);
        data.spec = JSON.parse(data.spec);
        this.form = data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    // 表单重置
    reset() {
      this.form = {
        pics: {},
        id: null,
        spuName: null,
        productionTime: null,
        discountPrice: null,
        originalPrice: null,
        label: null,
        desc: null,
        remark: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        updateUserId: null,
        shelfTime: null,
        status: "1",
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const sendForm = JSON.parse(JSON.stringify(this.form));
          sendForm.spec = JSON.stringify(setSpec(sendForm.spec));
          sendForm.pics = JSON.stringify(sendForm.pics);
          sendForm.goodsTagList = [{ tag: "1", state: 1 }];

          if (sendForm.id != null) {
            updateSpu(sendForm).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpu(sendForm).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>

<style>
</style>
