<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rulesThis"
    ref="ruleForm"
    :size="'mini'"
    label-width="110px"
    class="demo-ruleForm"
  >
    <el-row>
      <el-col :span="1" class="dtitle">
        <ul>
          <li>包</li>
          <li>装</li>
          <li>信</li>
          <li>息</li>
        </ul></el-col
      >
      <el-col :span="23" class="dmain">
        <el-row>
          <el-col :span="8">
            <el-form-item label="包装清单" prop="packing_list">
              <el-input
                placeholder="包装清单"
                v-model="ruleForm.packing_list"
                maxlength="250"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="包装方式" prop="packing_way">
              <el-input
                placeholder="包装方式"
                v-model="ruleForm.packing_way"
                maxlength="250"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="装箱规格" prop="packing_spec">
              <el-input
                placeholder="装箱规格"
                v-model="ruleForm.packing_spec"
                maxlength="250"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="装箱重量" prop="packing_weight">
              <el-input
                placeholder="装箱重量"
                v-model="ruleForm.packing_weight"
                type="number"
                :min="0"
                :max="9999999999.99"
                :step="2"
              >
                <template slot="append">g</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="装箱尺寸" prop="packing_szie">
              <el-input
                placeholder="装箱尺寸"
                v-model="ruleForm.packing_szie"
                maxlength="250"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="商品条形码" prop="good_bar">
              <el-input
                placeholder="物流时间"
                v-model="ruleForm.good_bar"
                type="number"
                :min="0"
                :max="9999999999999"
                :step="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-row class="ddiv">
      <el-col :span="1" class="dtitle">
        <ul>
          <li>发</li>
          <li>货</li>
          <li>信</li>
          <li>息</li>
        </ul></el-col
      >
      <el-col :span="23" class="dmain">
        <el-row>
          <el-col :span="8">
            <el-form-item label="供货区域" prop="supply_area">
              <el-select
                v-model="ruleForm.supply_area"
                filterable
                clearable
                style="width: 100%"
                placeholder="供货区域"
              >
                <el-option
                  v-for="group in options6"
                  :key="group.id"
                  :label="group.name"
                  :value="group.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发货地" prop="delivery_place">
              <select-area
                :placeholder="'发货地'"
                :value="ruleForm.delivery_place"
                :is-detail="id !== 'add'"
                :disabled="isDetail"
                :size="'mini'"
                @selectChange="selectAreadelivery_place"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产地" prop="origin_place">
              <select-area
                :placeholder="'产地'"
                :value="ruleForm.origin_place"
                :size="'mini'"
                :is-detail="id !== 'add'"
                :disabled="isDetail"
                @selectChange="selectAreaorigin_place"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物流时间" prop="delivery_day">
              <el-input
                placeholder="物流时间"
                v-model="ruleForm.delivery_day"
                type="number"
                :min="0"
                :max="9999999999"
                :step="0"
              >
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供货周期" prop="lead_time">
              <el-input
                placeholder="供货周期"
                v-model="ruleForm.lead_time"
                type="number"
                :min="0"
                :max="9999999999"
                :step="0"
              >
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="调样周期" prop="sample_day">
              <el-input
                placeholder="调样周期"
                v-model="ruleForm.sample_day"
                type="number"
                :min="0"
                :max="9999999999"
                :step="0"
              >
                <template slot="append">天</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-row class="ddiv">
      <el-col :span="1" class="dtitle">
        <ul>
          <li>图</li>
          <li>片</li>
          <li>信</li>
          <li>息</li>
        </ul></el-col
      >
      <el-col :span="23" class="dmain">
        <el-row style="padding: 0 0 20px 0">
          <el-col :span="8">
            <el-form-item label="缩略图" prop="good_thumb_img" class="activity-upload">
              <div class="btnupload" style="position: relative">
                <img v-if="ruleForm.good_thumb_img" :src="ruleForm.good_thumb_img" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <file-upload
                  class="Upload"
                  :disabled="id == '007'"
                  :accept="'.jpg,.png,.jpeg'"
                  :multiple="true"
                  :uploadcondition="beforeAvatarUpload"
                  @UploadErrorEvent="UploadErrorEventgood_thumb_img"
                  @UploadSuccessEvent="UploadSuccessEventgood_thumb_img"
                ></file-upload>
              </div>
              <div class="txt-tips fl">
                <p>尺寸比例：</p>
                <p>1:1</p>
              </div>
            </el-form-item>
            <el-form-item label="详情主图" prop="good_img" class="activity-upload">
              <div class="btnupload" style="position: relative">
                <img v-if="ruleForm.good_img" :src="ruleForm.good_img" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <file-upload
                  class="Upload"
                  :disabled="id == '007'"
                  :accept="'.jpg,.png,.jpeg'"
                  :multiple="true"
                  :uploadcondition="beforeAvatarUpload"
                  @UploadErrorEvent="UploadErrorEventgood_img"
                  @UploadSuccessEvent="UploadSuccessEventgood_img"
                ></file-upload>
              </div>
            </el-form-item>
                 <div style="font-size: 12px; color: #606266">
            <span style="padding: 0 0 0 20px">图片大小：小于1M</span>
            <span style="padding: 0 0 0 20px">图片类型.jpg/png/jpeg</span>
          </div>
          </el-col>
          <el-col :span="16">
            <el-form-item label="详情介绍图" prop="good_info_img">
              <div class="good_info_img-div">
                <ul v-if="ruleForm.good_info_img" class="clear">
                  <li class="img-show-li" v-viewer v-for="(img, iindex) in ruleForm.good_info_img" :key="img + iindex">
                    <img :src="img" alt="" />
                    <i class="el-icon-close" @click="closeImg(iindex)"></i>
                  </li>
                  <li class="img-upload-li" v-if="ruleForm.good_info_img.length < 10">
                    <div class="activity-upload">
                      <div class="btnupload" style="position: relative">
                        <i class="el-icon-plus avatar-uploader-icon"></i>
                        <file-upload
                          class="Upload"
                          :disabled="id == '007'"
                          :accept="'.jpg,.png,.jpeg'"
                          :multiple="true"
                          :uploadcondition="beforeAvatarUpload"
                          @UploadErrorEvent="UploadErrorEventgood_info_img"
                          @UploadSuccessEvent="UploadSuccessEventgood_info_img"
                        ></file-upload>
                      </div>
                    </div>
                  </li>
                </ul>
              </div>
            </el-form-item>
          </el-col>
     
        </el-row>
      </el-col>
    </el-row>
  </el-form>
</template>
   <script>
import asyncRequest from "@/apis/service/goodStore/goodsCost";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
import { options6 } from "../columns";
export default {
  name: "goodsCostAdd",
  mixins: [resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "goodsCostAdd"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      size: "small",
      rulesThis: this.rules,
      loading: false,
      options6: options6,
      ruleForm: {},
      rules: {
        packing_list: [
          {
            required: true,
            message: "请输入包装清单",
            trigger: "blur",
          },
        ],
        packing_way: [
          {
            required: true,
            message: "请输入包装方式",
            trigger: "blur",
          },
        ],

        packing_spec: [
          {
            required: true,
            message: "请输入装箱规格",
            trigger: "blur",
          },
        ],
        packing_weight: [
          {
            required: true,
            message: "请输入装箱重量",
            trigger: "blur",
          },
        ],
        packing_szie: [
          {
            required: true,
            message: "请输入装箱尺寸",
            trigger: "blur",
          },
        ],
        supply_area: [
          {
            required: true,
            message: "请选择供货区域",
            trigger: "change",
          },
        ],
        delivery_place: [
          {
            type: "array",
            required: true,
            message: "请选择发货地",
            trigger: "change",
          },
        ],
        origin_place: [
          {
            type: "array",
            required: true,
            message: "请选择产地",
            trigger: "change",
          },
        ],
        delivery_day: [
          {
            required: true,
            message: "请输入物流时间",
            trigger: "blur",
          },
        ],
        lead_time: [
          {
            required: true,
            message: "请输入供货周期",
            trigger: "blur",
          },
        ],
        sample_day: [
          {
            required: true,
            message: "请输入调样周期",
            trigger: "blur",
          },
        ],
        good_thumb_img: [
          {
            required: true,
            message: "商品缩略图",
            trigger: "change",
          },
        ],
        good_img: [
          {
            required: true,
            message: "商品详情主图",
            trigger: "change",
          },
        ],
        good_info_img: [
          {
            type: "array",
            required: true,
            message: "商品详情介绍图",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.resetFormData();
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },
    handleClick(row) {
      console.log(row);
    },
    async initData() {
      this.loading = true;
      let res = await asyncRequest.detail({ id: this.id });
      this.loading = false;
      if (res.code === 0) {
        await this.resetForm(res.data);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(async () => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },
    resetFormData() {
      this.ruleForm = {
        packing_list: "",
        packing_way: "",
        packing_spec: "",
        packing_weight: "",
        packing_szie: "",
        good_bar: "",
        supply_area: "",
        delivery_place: [],
        origin_place: [],
        delivery_day: "",
        lead_time: "",
        sample_day: "",
        good_thumb_img: "",
        good_img: "",
        good_info_img: [],
      };
    },
    //仓库省市区
    selectAreadelivery_place(e) {
      this.ruleForm.delivery_place = e;
      this.$refs.ruleForm.validateField("delivery_place");
    },
    //仓库省市区
    selectAreaorigin_place(e) {
      this.ruleForm.origin_place = e;
      this.$refs.ruleForm.validateField("origin_place");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let change = false;
          this.mock.forEach((v1) => {
            if (v1.change && v1.change.length > 0) {
              change = true;
            }
          });
          if (!change) {
            this.ruleForm.spec = "规格！";
            return;
          } else {
            this.ruleForm.spec = "";
            let table = false;
            let hasnot = false;
            let stock = [];
            this.right.specList.forEach((v1) => {
              if (v1) {
                v1.forEach((v2) => {
                  if (v2 && v2.limit && v2.limit.length > 0) {
                    let list = [];
                    v2.limit.forEach((v3) => {
                      if (v3.num != 0 && v3.price != 0) {
                        table = true;
                        list.push(v3);
                      }
                      if (
                        (v3.num != 0 && v3.price == 0) ||
                        (v3.num == 0 && v3.price !== 0)
                      ) {
                        hasnot = true;
                      }
                    });
                    if (list.length > 0) {
                      let model = Object.assign({}, v2);
                      model.limit = list;
                      stock.push(model);
                    }
                  }
                });
              }
            });

            if (hasnot) {
              this.ruleForm.table =
                "起订量与成本价需成对填写，未成对请设为零！";
            } else {
              if (!table) {
                this.ruleForm.table = "至少填写一堆起订量与成本价！";
              } else {
                this.ruleForm.table = "";
                this.loading = true;
                let obj = JSON.parse(JSON.stringify(this.ruleForm));
                obj.stock = stock;
                obj.catid = obj.catid[obj.catid.length - 1];
                obj.image = obj.image.join();
                //split(",");
                console.log(obj);
                let res = {};
                if (this.id === "add") {
                  delete obj["id"];
                  res = await asyncRequest.add(obj);
                } else {
                  res = await asyncRequest.update(obj);
                }
                this.loading = false;
                if (res.code === 0) {
                  let title = this.id === "add" ? "添加成功" : "修改成功";
                  this.$notify.success({
                    title,
                    message: "",
                  });
                  // 刷新
                  this.$emit("refresh");
                }
              }
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    closeImg(index) {
      this.ruleForm.good_info_img.splice(index, 1);
      this.$refs.ruleForm.validateField("good_info_img");
    },
    //图片上传成功
    async UploadSuccessEventgood_thumb_img(data) {
      await this.UploadSuccessEvent(1, data);
    },
    //图片上传成功
    async UploadSuccessEventgood_img(data) {
      await this.UploadSuccessEvent(2, data);
    },
    //图片上传成功
    async UploadSuccessEventgood_info_img(data) {
      await this.UploadSuccessEvent(3, data);
    },
    //图片上传成功
    async UploadSuccessEvent(type, data) {
      const { url } = data;
      if (url === "noToken") {
        await this.logout();
      } else {
        if (type === 1) {
          this.ruleForm.good_thumb_img = url;
          this.$refs.ruleForm.validateField("good_thumb_img");
        } else if (type === 2) {
          this.ruleForm.good_img = url;
          this.$refs.ruleForm.validateField("good_img");
        } else {
          this.ruleForm.good_info_img.push(url);
          this.$refs.ruleForm.validateField("good_info_img");
        }
        this.$message.success("图片上传成功!");
      }
    },
    //图片上传失败
    UploadErrorEventgood_thumb_img(res) {
      this.imgUploadError(1, res);
    },
    //图片上传失败
    UploadErrorEventgood_img(res) {
      this.imgUploadError(2, res);
    },
    //图片上传失败
    UploadErrorEventgood_info_img(res) {
      this.imgUploadError(3, res);
    },
    imgUploadError(type, res) {
      if (res !== "break") {
        this.$message.error("图片上传失败!");
        this.$refs.ruleForm.validateField(
          type === 1 ? "good_thumb_img" : type === 2 ? "good_img" : "good_info_img"
        );
      }
    },
    //判断图片规格
    beforeAvatarUpload(file) {
      let isJPG = false;
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/jpeg"
      ) {
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 1;
      if (!isJPG) {
        this.$message.error("图片格式不正确！");
      }
      if (!isLt2M) {
        this.$message.error("图片大小不能超过 1MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>
   <style lang="scss" scoped>
.goodsCostAdd {
  .good_info_img-div {
    ul {
      float: left;
      width: 100%;

      li {
        float: left;
        width: 102px;
        height: 102px;
        overflow: hidden;
        margin: 0 10px 10px 0;
        border: 1px solid rgb(220, 223, 230);
        position: relative;
        &.img-upload-li {
          border: 0;
        }
        &.img-show-li {
          img {
            display: inline-block;
            width: 100%;
          }
          i {
            position: absolute;
            width: 20px;
            height: 20px;
            font-size: 13px;
            text-align: center;
            line-height: 20px;
            top: 0;
            right: 0;
            z-index: 2;
            color: rgb(220, 223, 230);
          }
          &:hover {
            i {
              color: #63cbe7;
              &:hover {
                cursor: pointer;
              }
            }
          }
        }
      }
    }
  }
}
</style>
   