<template>
  <el-form :model="form" ref="queryForm" size="small"     :rules="rules" :inline="true" label-width="120px">
    <el-row>
      <el-col :span="24">
        <el-form-item prop="spuDesc">
          <span slot="label">商品介绍</span>
          <el-input
            style="width: 700px"
            type="textarea"
            :rows="3"
            placeholder="请输入商品介绍"
            v-model="form.spuDesc"
            @input="passValue"
          >
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="商品详情图" prop="spuName">
          <span class="tips">最多可上传10张</span>
          <el-upload
            list-type="picture-card"
            :file-list="spuDetailList"
            :before-remove="requestRemove"
            :http-request="(data) => requestUpload(data, 'detail')"
            :show-file-list="true"
            :limit="10"
            action="#"
            :multiple="true"
            accept=".png,.jpg,.jpeg"
            ref="files"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="特殊资质图" prop="spuName">
          <span class="tips">例如农残检测报告等，最多可上传10张</span>
          <el-upload
            list-type="picture-card"
            :file-list="specialList"
            :before-remove="requestRemove1"
            :http-request="(data) => requestUpload(data, 'zz')"
            :show-file-list="true"
            :limit="10"
            action="#"
            :multiple="true"
            accept=".png,.jpg,.jpeg"
            ref="files"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item prop="spuStatus">
          <span slot="label"> 发布状态</span>
          <el-radio-group v-model="form.spuStatus">
            <el-radio :label="0" :value="0">上架</el-radio>
            <el-radio :label="1" :value="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
import { getToken } from "@/utils/auth";
import axios from "axios";
export default {
  props: ["data"],
  data() {
    return {
      form: {
        spuDetailList: [],
        specialList: [],
        spuDesc: "",
        spuStatus: 0,
      },
      headers: { Auth: "Bearer " + getToken(), "Content-type": "multipart/form-data" },
      spuDetailList: [],
      specialList: [],
      index: 0,
      index1: 0,
      rules: {
        spuDesc: [{ required: true, message: "商品介绍不能为空", trigger: "blur" }],
        spuStatus: [{ required: true, message: "发布状态不能为空", trigger: "blur" }],
      },
    };
  },
  watch: {
    //监听info对象
    data: {
      handler(newVal, oldVal) {
        //监听info对象变化
        if (newVal) {
          this.form.spuDesc = newVal.spuDesc;
          this.spuDetailList = [];
          this.specialList = [];
          if (newVal.spuDetailList) {
            newVal.spuDetailList.forEach((e) => {
              this.spuDetailList.push({
                url: e.fileUrl,
                id: e.id,
                sort: e.sort,
              });
            });
          }
          if (newVal.specialList) {
            newVal.specialList.forEach((e) => {
              this.specialList.push({
                url: e.fileUrl,
                id: e.id,
                sort: e.sort,
              });
            });
          }
        }
      },
      deep: true, //深度监听
      immediate: true,
    },
  },

  methods: {
    formvalidate() {
      let that=this
      return new Promise((resolve) => {
        that.$refs.queryForm.validate((valid) => {
          console.log(valid);
          if (valid) {
            resolve(valid);
          } else {
            resolve(valid);
          }
        });
      });
    },
    passValue() {
      if (this.specialList) {
        this.form.specialList = [];
        for (let index = 0; index < this.specialList.length; index++) {
          this.form.specialList.push({
            fileUrl: this.specialList[index].url,
            sort: this.specialList[index].name,
          });
        }
      }
      if (this.spuDetailList) {
        this.form.spuDetailList = [];
        for (let index = 0; index < this.spuDetailList.length; index++) {
          this.form.spuDetailList.push({
            fileUrl: this.spuDetailList[index].url,
            sort: this.spuDetailList[index].name,
          });
        }
      }
      this.$emit("updateValue", this.form);
    },
    requestRemove1(val) {
      this.specialList.forEach((e, index) => {
        if (e.uid == val.uid) {
          this.specialList.splice(index, 1);
        }
      });
      this.passValue();
    },
    requestRemove(val) {
      this.spuDetailList.forEach((e, index) => {
        if (e.uid == val.uid) {
          this.spuDetailList.splice(index, 1);
        }
      });
      this.passValue();
    },

    // 可以获取图片参数
    async requestUpload(data, type) {
      const formdata = new FormData();
      formdata.append("file", data.file);
      const res = await axios.post(
        `${process.env.VUE_APP_NSY_UPLOAD_API}/api/third/op/v1/third/uploadPic`,
        formdata,
        {
          headers: this.headers,
        }
      );
      if (res.data.data) {
        if (type == "detail") {
          this.spuDetailList.push({
            name: this.index,
            url: res.data.data,
          });
          this.index++;
        } else if (type == "zz") {
          this.specialList.push({
            name: this.index1,
            url: res.data.data,
          });
          this.index1++;
        }
        this.passValue();
        this.$forceUpdate();
      }
    },
  },
};
</script>

<style></style>
