<template>
  <el-form
    :model="form"
    ref="queryForm"
    size="small"
    :inline="true"
    label-width="120px"
    :rules="rules"
  >
    <el-row>
      <el-col :span="24">
        <el-form-item prop="lookSalesVolume">
          <span slot="label">展示销量</span>
          <el-input
            v-model="form.lookSalesVolume"
            placeholder="请输入展示销量"
            clearable
            @input="passValue"
          />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item prop="giftDesc" label="快递类型">
          <el-radio v-model="form.expressType" :label="0" :value="0" @change="passValue"
            >水果特惠</el-radio
          >
          <el-radio v-model="form.expressType" :label="1" :value="1" @change="passValue"
            >生鲜特快</el-radio
          >
          <el-radio v-model="form.expressType" :label="2" :value="2" @change="passValue"
            >京东标快</el-radio
          >
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item prop="unit">
          <span slot="label">计量单位</span>
          <el-select v-model="form.unit" placeholder="请选择" @change="passValue">
            <el-option
              v-for="item in measure"
              :key="item.dictLabel"
              :label="item.dictLabel"
              :value="item.dictLabel"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item>
          <span slot="label"><span class="required">*</span> 商品规格</span>
          <el-button @click="addSpecification" type="primary">添加规格</el-button>
          <el-table :data="form.skuList" style="width: 100%; margin-top: 20px">
            <el-table-column prop="name" label="规格名称" width="160">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.skuSpecsList[0].specsName"
                  placeholder="请输入规格名称"
                  clearable
                  maxlength="8"
                  @input="passValue"
                  show-word-limit
                />
              </template>
            </el-table-column>
            <el-table-column prop="address" label="规格内容" width="160">
              <template slot-scope="scope">
                <el-input
                  v-model="scope.row.skuSpecsList[0].specsValue"
                  placeholder="规格内容"
                  clearable
                  maxlength="8"
                  @input="passValue"
                  show-word-limit
                />
              </template>
            </el-table-column>

            <el-table-column prop="name" label="预计库存" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.predictStocks"
                  placeholder="预计库存"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }} </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="实际库存" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.stock"
                  placeholder="实际库存"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }} </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column label="起批量" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuPriceList[0].minPurchase"
                  placeholder="起批量"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix">
                    {{ form.unit }}
                  </template>
                </el-input-number>
              </template>
            </el-table-column>

            <el-table-column label="原价" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuPriceList[0].originalPrice"
                  placeholder="原价"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column label="现价" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuPriceList[0].price"
                  placeholder="现价"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>

            <!-- <el-table-column label="净重量(kg)" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.weight"
                  placeholder="请输入净重量"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> /Kg </template>
                </el-input-number>
              </template>
            </el-table-column> -->
            <!-- <el-table-column label="是否上架">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.sj"> </el-switch>
              </template>
            </el-table-column> -->
            <el-table-column prop="address" label="操作" width="160">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  style="color: red"
                  @click="removeSpecification(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-col>
      <el-col :span="24" style="margin-top: 20px">
        <el-form-item>
          <span slot="label"><span class="required">*</span> 商品属性</span>
          <div class="main">
            <div style="padding-top: 20px">
              <el-form-item label="包装方式" prop="packing">
                <el-select
                  v-model="form.packing"
                  placeholder="请选择包装方式"
                  @change="passValue"
                >
                  <el-option
                    v-for="item in package"
                    :key="item.dictLabel"
                    :label="item.dictLabel"
                    :value="item.dictLabel"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="shippingTimeDesc">
                <span slot="label">发货时间</span>
                <el-select
                  v-model="form.shippingTimeDesc"
                  placeholder="请选择发货时间"
                  @change="passValue"
                >
                  <el-option label="付款24小时之内" value="付款24小时之内" />
                  <el-option label="付款36小时之内" value="付款36小时之内" />
                  <el-option label="付款48小时之内" value="付款48小时之内" />
                  <el-option label="付款72小时之内" value="付款72小时之内" />
                </el-select>
              </el-form-item>
            </div>

            <el-form-item label="自定义属性" v-if="form.props && form.props.length > 0">
              <div
                style="
                  width: 100%;
                  display: flex;
                  flex-direction: row;
                  flex-flow: wrap;
                  justify-content: center;
                "
              >
                <div
                  class="item"
                  v-for="(item, index) in form.props ? form.props : []"
                  :key="index"
                >
                  <el-input
                    v-model="item.name"
                    placeholder="请输入属性名称"
                    clearable
                    maxlength="8"
                    show-word-limit
                    style="width: 200px"
                  />
                  <el-input
                    v-model="item.value"
                    placeholder="请输入属性值"
                    clearable
                    maxlength="8"
                    show-word-limit
                    @input="passValue"
                    style="width: 200px"
                  />
                  <i
                    class="el-icon-delete"
                    size="22"
                    style="color: red"
                    @click="removeAttribute(item, index)"
                  ></i>
                </div>
              </div>
            </el-form-item>
          </div>
          <div class="item1" style="width: 180px">
            <span @click="addattribute" style="cursor: pointer">
              <i class="el-icon-plus" size="22"></i>自定义添加商品属性</span
            >
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="24" style="margin-top: 20px" v-if="!showCom">
        <el-form-item prop="freeShipping">
          <span slot="label">运费</span>
          <div class="main1">
            <el-radio-group v-model="form.freeShipping" class="radios">
              <el-radio :label="0" :value="0"
                >双方协商后卖家进行修改，买家和订单一起支付</el-radio
              >
              <el-radio :label="1" :value="1">全国包邮，由卖家承担运费</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-col>
      <el-col :span="24" style="margin-top: 20px">
        <el-form-item prop="shippingAddrBean.addrDetail">
          <span slot="label"> 发货地址</span>
          <div style="display: flex; flex-direction: row; align-items: center">
            <el-button
              @click="selectAddr"
              type="text"
              style="
                font-size: 16px;
                display: flex;
                flex-direction: row;
                align-items: center;
              "
              ><img src="@/assets/images/point.png" style="width: 20px; height: 20px" />
              选择地址</el-button
            >
            <span style="margin-left: 20px">已选择地址:{{ addr }}</span>
          </div>

          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入详细地址"
            v-model="form.shippingAddrBean.addrDetail"
            style="width: 700px"
            @blur="passValue"
          >
          </el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <Amap ref="mapOpen" @address="Mapaddress"></Amap>
  </el-form>
</template>

<script>
import { getStoreInfo } from "@/api/common/index";
import Amap from "@/components/Map/map.vue";
import { getDict } from "@/api/common/index.js";
export default {
  props: ["data", "saleType", "saleModels"],
  components: { Amap },
  data() {
    return {
      form: {
        expressType: 0,
        unit: "斤",
        props: [],
        freeShipping: 0,
        lookSalesVolume: "",
        shippingAddrBean: {
          addr: "",
          addrDetail: "",
          cityCode: "",
          countyCode: "",
          latitude: "",
          longitude: "",
          provinceCode: "",
        },
        shippingAddr: "",
        skuList: [],
        packing: "",
        shippingTimeDesc: "",
      },
      addr: "",
      measure: [],
      package: [],
      rules: {
        freeShipping: [{ required: true, message: "请选择运费方式", trigger: "blur" }],
        props: [{ required: true, message: "商品属性不能为空", trigger: "blur" }],
        unit: [{ required: true, message: "计量单位", trigger: "blur" }],
        shippingTimeDesc: [
          { required: true, message: "发货时间不能为空", trigger: "blur" },
        ],
        skuList: [{ required: true, message: "规格不能为空", trigger: "blur" }],
        packing: [{ required: true, message: "包装方式不能为空", trigger: "blur" }],
        "shippingAddrBean.addrDetail": [
          { required: true, message: "详细地址不能为空", trigger: "blur" },
        ],
      },
      shopInfo: {},
      saleType: undefined,
      sj: true,
      showCom: true,
      skuIds: [],
      saleModel: 1,
    };
  },
  watch: {
    //监听文件url改变时重新赋值
    saleModels(newVal, oldVal) {
      console.log("azxczx", newVal);
      this.saleModel = newVal;
      this.$forceUpdate();
    },
  },
  mounted() {
    this.getInfo();
    this.getdictImpl();
  },

  methods: {
    getFormInfo(record) {
      this.saleModel = record.saleModel;
      this.addr = record.shippingAddr;
      this.form.expressType = record.expressType;
      this.form.shippingAddrBean = {
        addr: record.shippingAddr,
        addrDetail: record.shippingAddrBean
          ? record.shippingAddrBean.addrDetail
          : undefined,
        cityCode: record.cityCode,
        countyCode: record.countyCode,
        latitude: record.latitude,
        longitude: record.longitude,
        provinceCode: record.provinceCode,
      };
      if (record.props && record.props?.length < 1) {
        this.form.props = [];
      } else if (!record.props) {
        this.form.props = [];
      } else {
        this.form.props = record.props;
      }
      this.form.lookSalesVolume = record.lookSalesVolume;
      this.form.unit = record.unit;
      this.form.shippingAddr = record.shippingAddr;
      this.form.packing = record.packing;
      this.form.shippingTimeDesc = record.shippingTimeDesc;
      if (this.form.freeShipping) {
        this.form.freeShipping = record.freeShipping;
      }

      this.form.skuList = record.skuList;
    },

    setShowCom(val) {
      this.showCom = val;
    },
    compilatePrice(data) {
      if (data.price) {
        return data.price.toFixed(2);
      } else if (data.originalPrice) {
        return data.originalPrice.toFixed(2);
      } else if (data.originalPrice && data.price) {
        return data.price.toFixed(2);
      }
    },
    formvalidate() {
      let that = this;
      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);
          }
        });
      });
    },
    getInfo() {
      getStoreInfo().then((res) => {
        if (res.code == 200) {
          this.shopInfo = res.data;
        }
      });
    },
    getdictImpl() {
      getDict("stockUnit").then((res) => {
        if (res.code == 200) {
          this.measure = res.data;
        }
      });
      getDict("packType").then((res) => {
        if (res.code == 200) {
          this.package = res.data;
        }
      });
    },
    computePrice(data) {
      if (this.shopInfo.scope == 0) {
        if (
          data.skuCost.purchaseFee &&
          data.skuCost.laborFee &&
          data.skuCost.agencyFee &&
          data.skuCost.materialFee
        ) {
          return (
            Number(data.skuCost.purchaseFee) +
            Number(data.skuCost.laborFee) +
            Number(data.skuCost.agencyFee) +
            Number(data.skuCost.materialFee)
          ).toFixed(2);
        } else {
          return 0;
        }
      } else {
      }
    },

    passValue() {
      if (this.shopInfo.scope == 0) {
        this.form.skuList.forEach((e) => {
          e.skuPriceList.forEach((a) => {
            a.price = this.computePrice(e);
            a.originalPrice = this.computePrice(e);
            a.lowestPrice = this.computePrice(e);
          });
        });
      } else {
        if (this.saleType == 4) {
          this.form.skuList.forEach((e) => {
            e.skuPriceList.forEach((a) => {
              a.price = a.originalPrice;
            });
          });
        }
      }
      this.$emit("updateValue", this.form);
    },

    Mapaddress(data) {
      this.form.shippingAddr = data.pname + data.cityname + data.adname;
      this.form.shippingAddrBean.addr = data.address;
      // this.form.shippingAddrBean.addrDetail = data.name ? data.name : undefined;
      this.form.shippingAddrBean.cityCode = data.cityCode;
      this.form.shippingAddrBean.countyCode = data.adcode;
      if (data.location) {
        this.form.shippingAddrBean.latitude = data.location.lat;
        this.form.shippingAddrBean.longitude = data.location.lng;
      }
      this.form.shippingAddrBean.provinceCode = data.pcode;
      this.addr = data.address;
      this.$refs.mapOpen.closeMap();
      // this.passValue();
    },
    selectAddr() {
      this.$refs.mapOpen.loadMap();
    },
    addattribute() {
      if (this.form.prop?.length > 0) {
        this.form.props.push({
          name: "",
          id: "",
          value: "",
        });
      } else {
        this.form.prop = [];
        this.form.props.push({
          name: "",
          id: "",
          value: "",
        });
      }
    },
    removeAttribute(record, index) {
      this.form.props.splice(index, 1);
      this.passValue();
    },
    removeSpecification(record, index) {
      console.log("record", record.id);
      this.skuIds.push(record.id);
      this.form.skuList.splice(index, 1);
      this.$emit("skuRemove", this.skuIds);
    },
    addSpecification() {
      this.form.skuList.push({
        stock: undefined,
        weight: undefined,
        sj: true,
        skuCost: {
          purchaseFee: undefined,
          laborFee: undefined,
          agencyFee: undefined,
          materialFee: undefined,
        },
        skuSpecsList: [
          {
            specsId: "0",
            specsName: "",
            specsValue: "",
          },
        ],
        skuPriceList: [
          {
            minPurchase: undefined,
            maxPurchase: undefined,
            price: undefined,
            originalPrice: undefined,
            lowestPrice: 1,
          },
        ],
      });
    },
  },
};
</script>
<style>
.el-upload-list__item {
  transition: none !important;
  -webkit-transition: nonne !important;
}
.el-upload-list__item-name {
  transition: none !important;
  -webkit-transition: nonne !important;
}
</style>
<style scoped>
/deep/ .el-radio-group {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  height: 50px;
}
.item {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 20px;
  margin-right: 10px;
}
.main1 {
  background: #f1f1f1;
  min-height: 80px;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  padding: 0px 20px;
}
.main {
  width: 58vw;
  background: #f1f1f1;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  /* display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  padding: 20px 20px 0px 20px; */
}
</style>
