<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="spuDetailList">
          <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" @change="radioChange">
            <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" }],
        spuDetailList: [
          { required: true, message: "商品详情图不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getFormInfo(record) {
      this.form.spuDesc = record.spuDesc;
      this.spuDetailList = [];
      this.specialList = [];
      if (record.spuDetailList) {
        record.spuDetailList.forEach((e) => {
          this.spuDetailList.push({
            url: e.fileUrl,
            id: e.id,
            sort: e.sort,
          });
        });
      }

      if (record.specialList) {
        record.specialList.forEach((e) => {
          this.specialList.push({
            url: e.fileUrl,
            id: e.id,
            sort: e.sort,
          });
        });
      }
      this.form.specialList = this.specialList;
      this.form.spuDetailList = this.spuDetailList;
    },

    formvalidate() {
      let that = this;
      console.log(this.form);

      return new Promise((resolve) => {
        that.$refs.queryForm.validate((valid, row) => {
          if (valid) {
            resolve(valid);
          } else {
            for (const key in row) {
              for (let index = 0; index < row[key].length; index++) {
                this.$message.error(row[key][index].message);
                return;
              }
            }
            resolve(valid);
          }
        });
      });
    },
    radioChange() {
      this.passValue();
    },
    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>
