<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="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"
            v-if="shopInfo.scope == 0"
          >
            <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"
                  show-word-limit
                  @input="passValue"
                />
              </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"
                  show-word-limit
                  @input="passValue"
                />
              </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 prop="address" 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 prop="address" label="收购价" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuCost.purchaseFee"
                  placeholder="收购价"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="人工费" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuCost.laborFee"
                  placeholder="人工费"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="代办费" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  v-model="scope.row.skuCost.agencyFee"
                  placeholder="代办费"
                  clearable
                  @input="passValue"
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="材料费" width="160">
              <template slot-scope="scope">
                <el-input-number
                  :controls="false"
                  @input="passValue"
                  v-model="scope.row.skuCost.materialFee"
                  placeholder="材料费"
                  clearable
                >
                  <template slot="suffix"> {{ form.unit }}/元 </template>
                </el-input-number>
              </template>
            </el-table-column>
            <!-- <el-table-column prop="name" label="原价" width="180"> </el-table-column> -->
            <el-table-column prop="address" label="商品单价" width="80">
              <template slot-scope="scope">
                {{ computePrice(scope.row) }}元/{{ form.unit }}
              </template>
            </el-table-column>
            <el-table-column label="规格重量" 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 prop="address" label="操作" width="160">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  style="color: red"
                  @click="removeSpecification(scope, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-table
            :data="form.skuList"
            style="width: 100%; margin-top: 20px"
            v-if="shopInfo.scope == 1"
          >
            <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"
                  show-word-limit
                  @input="passValue"
                />
              </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"
                  show-word-limit
                  @input="passValue"
                />
              </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 prop="address" label="商品单价" width="80">
              <template slot-scope="scope">
                {{
                  scope.row.skuPriceList[0].originalPrice
                    ? scope.row.skuPriceList[0].originalPrice
                    : 0
                }}
                元/{{ form.unit }}
              </template>
            </el-table-column>

            <el-table-column label="净重量" 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 prop="address" label="操作" width="160">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  style="color: red"
                  @click="removeSpecification(scope, 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-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
                    @input="passValue"
                    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">
              <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="shopInfo.scope == 0">
        <el-form-item prop="freeShipping">
          <span slot="label">运费</span>
          <div class="main1">
            <el-radio-group
              v-model="form.freeShipping"
              class="radios"
              @change="passValue"
            >
              <el-radio :label="0">双方协商后卖家进行修改，买家和订单一起支付</el-radio>
              <el-radio :label="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"
            @input="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"],
  components: { Amap },
  data() {
    return {
      form: {
        unit: "斤",
        props: [],
        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: {},
    };
  },
  mounted() {
    this.getInfo();
    this.getdictImpl();
  },
  watch: {
    //监听info对象
    data: {
      handler(newVal, oldVal) {
        //监听info对象变化
        if (newVal) {
          this.addr = newVal.shippingAddr;
          this.form.shippingAddrBean = {
            addr: newVal.shippingAddr,
            addrDetail: newVal.shippingAddrString
              ? newVal.shippingAddrString.split(newVal.shippingAddr)[1]
              : "",
            cityCode: newVal.cityCode,
            countyCode: newVal.countyCode,
            latitude: newVal.latitude,
            longitude: newVal.longitude,
            provinceCode: newVal.provinceCode,
          };
          this.form.props = newVal.props;
          this.form.unit = newVal.unit;
          this.form.shippingAddr = newVal.shippingAddr;
          this.form.packing = newVal.packing;
          this.form.shippingTimeDesc = newVal.shippingTimeDesc;
          this.form.freeShipping = newVal.freeShipping;
          this.form.skuList = newVal.skuList;
          if (this.shopInfo.scope == 0) {
            this.form.freeShipping = newVal.freeShipping;
          }
        }
      },
      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);
          }
        });
      });
    },
    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 (
            parseInt(data.skuCost.purchaseFee) +
            parseInt(data.skuCost.laborFee) +
            parseInt(data.skuCost.agencyFee) +
            parseInt(data.skuCost.materialFee)
          );
        } else {
          return 0;
        }
      }
    },
    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 {
        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.pname + data.cityname + data.adname;
      // 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.form.skuList = [];
      this.addr = data.address;
      this.$refs.mapOpen.closeMap();
      this.passValue();
    },
    selectAddr() {
      this.$refs.mapOpen.loadMap();
    },
    addattribute() {
      this.form.props.push({
        name: "",
        id: "",
        value: "",
      });
    },
    removeAttribute(record, index) {
      this.form.props.splice(index, 1);
      this.passValue();
    },
    removeSpecification(record, index) {
      this.form.skuList.splice(index, 1);
    },
    addSpecification() {
      this.form.skuList.push({
        stock: undefined,
        weight: undefined,
        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 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>
