<template>
  <div class="product">
    <!-- <van-nav-bar title="产品" class="van-nav-bar-my-fixed" fixed /> -->
    <div class="bgImg">
<!--      <img src="@/assets/images/product/bgImg.png" alt="" />-->
      <div class="title">
        <p>藏金 · 1745产品</p>
      </div>
      <div class="tags">
        <img src="@/assets/images/product/tags.png" alt="" />
      </div>
    </div>

    <div v-loading="loading">
      <div class="product-baozhuang">
        <div class="productPack">
          <div class="packageBox">
            <li @click="handleClick(0)">
              <!-- <img :src="currIndex == '0' ? selectActive : select" alt="" /> -->
              <div class="box" :class="{'selected':currIndex==0}">
                <img src="@/assets/images/product/box.png" alt="" />
                <p>藏金·1745-实惠装</p>
                <span>可用库存:{{ canStockBag }}</span>
              </div>
            </li>
            <li @click="handleClick(1)">
              <div class="box" :class="{'selected':currIndex==1}">
                <img src="@/assets/images/product/bag.png" alt="" />
                <p>藏金·1745-礼盒装</p>
                <span>可用库存:{{ canStockBox }}</span>
              </div>
            </li>
          </div>

          <div v-if="ad && ad.id" @click="getaddress" class="item-ad clear">
            <p class="item-ad__name">
              <span>{{ ad.name }} </span><span class="tel">{{ ad.mobile }}</span>
            </p>
            <p class="item-ad__address">
              <van-icon name="location-o" size="14"/> <span>{{ ad.address }}</span>
            </p>
          </div>
          <div class="kongParents" v-else>
            <div @click="getaddress" class="kong clear">
              <img src="@/assets/images/product/add.png" alt="" />
              <span>添加地址</span>
            </div>
          </div>
        </div>
        <div class="product-footer clear">
          <div class="fl">
            <input-number :numb="num" :max="stock" @backNum="backNum" />
          </div>
          <div>
            <van-button
                type="info"
                class="fr"
                @click="placeOrder"
            >确认下单</van-button>
          </div>
        </div>
      </div>

      <!-- <div class="product-kucun clear">
        <ul>
          <li
            v-for="(pitem, index) in productList"
            :key="pitem + index"
            @click="changePitem(index)"
          >
            <div class="input-radio" :class="{ checked: radioV === pitem.id }">
              <div class="radio-box">
                <div class="radio-check"></div>
              </div>
            </div>

            <img :src="pitem.img" alt="" />
            <div>
              <p>{{ pitem.name }}</p>
              <p>
                可用库存：{{ pitem.stock }}{{ pitem.unit }}/{{ pitem.weight }}kg
              </p>
            </div>
          </li>
        </ul>
      </div> -->
    </div>
  </div>
</template>

<script>
import { Dialog } from "vant";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/product/index";
import { getGoodsUnit, getGoodsNum, getAd } from "@/utils/auth";
export default {
  mixins: [resToken],
  data() {
    return {
      select: "/static/img/select.bed55bd9.png",
      selectActive: "@/assets/images/product/select-active.png",
      currIndex: null,
      num: 0,
      total: 0,
      radioV: "",
      address: [],
      unitist: [],
      productList: [],
      stock: 0,
      ad: null,
      canStockBox: "",
      canStockBag: "",
      isce:''
    };
  },
  mounted() {
    this.ad = getAd() ? JSON.parse(getAd()) : {};
    this.num = isNaN(getGoodsNum()) ? 0 : parseInt(getGoodsNum());
    if (this.ad && this.ad.id) {
      this.initData(this.ad.id);
    } else {
      this.address = [];
    }
    this.getStock();
  },

  methods: {
    changePitem(index) {
      this.radioV = this.productList[index].id;
      this.stock = this.productList[index].stock;

      if (this.num > this.stock) {
        this.num = this.stock;
      }
    },
    async getaddress() {
      let numRes = await this.set_user_goodsNum(this.num);
      let unitRes = await this.set_user_goodsUnit(this.radioV);
      if (numRes && numRes.code === 0 && unitRes && unitRes.code === 0) {
        window.vm.$router.push({
          path: "/address",
          query: {
            id: this.ad && this.ad.id ? this.ad.id : "0",
          },
        });
      }
    },
    async getStock() {
      if (!this.loading) {
        let res = await asyncRequest.get_stock({});
        if (res && res.code == 0) {
          let { limit, stock } = res.data;
          if (!(limit && limit.length > 0)) {
            limit = [];
          }
          this.canStockBag = res.data.stock;
          this.canStockBox = res.data.stock;

          this.distributeGoods(limit);
          // await this.getUnitist(limit);
          this.total = parseInt(stock + "");
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.total = 0;
        }
      }
    },
    // async getUnitist(list) {
    //   if (!this.loading) {
    //     let res = await asyncRequest.unitist({});
    //     if (res && res.code == 0 && res.data && res.data.list) {
    //       this.unitist = res.data.list;
    //       this.distributeGoods();
    //     } else if (res && res.code >= 100 && res.code <= 104) {
    //       await this.logout();
    //     } else {
    //       this.unitist = [];
    //     }
    //   }
    // },
    distributeGoods(list) {
      if (list.length > 0) {
        this.productList = [];
        list.forEach((v1) => {
          let item = {
            id: v1.id,
            name: v1.id === "1" ? "藏金1745普通装" : "藏金1745礼盒",
            img: require(`@/assets/images/product/unit${v1.id}.png`),
            stock: parseInt(v1.limit_num + ""),
            weight: v1.weight,
            unit: v1.name,
          };
          this.productList.push(item);
        });
        let index = this.productList.findIndex((v1) => {
          return v1.id == getGoodsUnit() + "";
        });

        this.changePitem(index === -1 ? 0 : index);
      }
    },
    async initData(id) {
      const res = await asyncRequest.addrdetail({ id: id });
      if (res && res.code === 0) {
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$store.dispatch("user/set_ad", {}).then((res) => {});
      }
    },
    backNum(e) {
      console.log(e, "eeee");

      this.num = e;
      console.log(this.num);
    },
    async placeOrder() {
      if (!this.loading) {
        if (this.ad && this.ad.id) {
          this.num = this.num;
          if (isNaN(this.num)) {
            this.num = 0;
          }
          if (this.stock === 0) {
            this.show_title("当前商品库存为0，无法购买！");
            return;
          }

          if (this.num === 0) {
            this.show_title("购买数量不能为0！");
            return;
          }

          Dialog.confirm({
            title: "确认下单？",
          })
            .then(async () => {
              await this.placeHttpOrder();
            })
            .catch(() => {});
        } else {
          this.show_title("请选择收货地址！");
        }
      }
    },
    async placeHttpOrder() {
      this.loading = true;
      let model = {
        addrid: this.ad.id,
        num: this.num + "",
        unit_id: this.radioV,
      };
      let res = await asyncRequest.add(model);
      this.loading = false;
      if (res && res.code == 0) {
        let setRes = await this.set_user_goodsNum(0);
        if (setRes && setRes.code === 0) {
          window.vm.$router.push({
            path: "/orderRes",
            query: {
              id: res.data.orderid,
            },
          });
        }
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.show_title(res.msg);
      }
    },
    async set_user_goodsNum(num) {
      return new Promise((resolve, reject) => {
        this.$store.dispatch("user/set_goodsNum", num).then((res) => {
          resolve(res);
        });
      });
    },
    async set_user_goodsUnit(unit) {
      return new Promise((resolve, reject) => {
        this.$store.dispatch("user/set_goodsUnit", unit).then((res) => {
          resolve(res);
        });
      });
    },
    handleClick(e) {
      this.currIndex = e;
    },
  },
};
</script>

<style lang="scss" scoped>
.product {
  background: #FCFCF0!important;
  .bgImg {
    width: 100%;
    height: 50%;
    background: url("../../assets/images/product/bgImg.png") no-repeat center;
    background-size:100% 100%;
    position: relative;
    img {
      width: 100%;
    }
    .title {
      width: 50%;
      height: 100%;
      display: inline-block;
      font-size: 1.4em;
      font-family: Source Han Sans CN;
      font-weight: 500;
      color: #daac70;
      position: relative;
      p{
        position: absolute;
        top:50%;
        left:5%;
      }
    }
    .tags {
      width: 50%;
      height:100%;
      display: inline-block;
      position: relative;
      img {
        position: absolute;
        width: 50px;
        right: 10%;
        top: 10%;
      }
    }
  }

  .product-baozhuang {
    width:100%;
    position: absolute;
     top:40%;
     left:0;
  }
  .productPack {
    width: 90%;
    z-index: 100;
    margin: 0 auto;
    position: relative;
    margin-bottom: 100px;
  .packageBox{
    display: flex;
    justify-content: space-between;
  }

    li {
      list-style: none;
      width:calc(50% - 10px);
      display: inline-block;
      padding: 5px;
      .box{
        background: #E6E6DC;
        border-radius: 13px;
        padding: 5px;
        img {
          width: 100%;
        }
        p {
          margin: 10px 0 5px 10px;
          font-size:1.2em;
          font-family: Source Han Sans CN;
          font-weight: 600;
          color: #1A1A1A;
        }
        span {
          margin-left: 10px;
          font-size: 1em;
          font-family: Source Han Sans CN;
          font-weight: 400;
          color: #4D4D4D;
        }
      }
      .selected{
        background: linear-gradient(0deg, #A57D4B, #EFC180, #EFC180);
      }
    }

  }
  .product-kucun {
    width: 100%;
    padding: 0 0 30px 0;
    ul {
      float: left;
      width: 100%;
      li {
        float: left;
        width: 100%;
        padding: 24px 0 0 0;
        &:hover {
          cursor: pointer;
        }
        .input-radio {
          height: 80px;
          width: 15px;
          padding: 0;
          float: left;
          line-height: 80px;
          margin: 0 8px 0 0;
          padding: 0 1px;
          .radio-box {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            border: 1px solid #c0c0c0;
            padding: 2px;
            margin: 33px 0 0 0;
            box-sizing: border-box;
            .radio-check {
              width: 9px;
              height: 9px;
              background: transparent;
              box-sizing: border-box;
              border-radius: 50%;
            }
          }
          &.checked {
            .radio-box {
              border: 1px solid #f4d022;
              .radio-check {
                // background: #f4d022;
                background: linear-gradient(0deg, #f2c482, #a07947);
              }
            }
          }
        }

        input {
          float: left;
          height: 80px;
          width: 17px;
          padding: 0;
          margin: 0 8px 0 0;
        }
        img {
          float: left;
          height: 84px;
          width: 130px;
          box-sizing: border-box;
          padding: 0 5px;
        }
        p {
          float: left;

          width: calc(100% - 155px);
          margin: 0;
          box-sizing: border-box;
          padding: 0 0 0 10px;
          &:first-child {
            margin-top: 8px;
            font-size: 17px;
            color: #333;
            height: 38px;
            line-height: 38px;
            font-weight: bolder;
          }
          &:last-child {
            height: 28px;
            line-height: 28px;
            font-size: 12px;
            color: #666;
          }
        }
      }
    }
  }
  .item-ad {
    background-color: #e6e6dc;
    color: #999999;
    padding: 10px 16px;
    box-sizing: border-box;
    width: 100%;
    margin-top: 30px;
    opacity: 1;
    border-radius: 8px;
    .item-ad__name {
      font-size: 1.2em;
      font-family: Source Han Sans CN;
      font-weight: 600;
      color: #111111;
      margin: 5px 0;
    }

    .item-ad__address {
      text-align: justify;
      font-size: 1.2em;
      font-family: Source Han Sans CN;
      color:#999999;
      margin: 5px 0;
      span{
       margin-left: 5px;
      }
    }
  }
  .kongParents {
    width: calc(100vw - 30px);
    // top: 199.5px;
    margin-top: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .kong {
    top: 199.5px;
    left: 76px;
    height: 34px;
    line-height: 40px;
    color: #333;
    text-align: center;
    width: 180px;
    border-radius: 17px;
    background: linear-gradient(0deg, #f2c482, #a07947);
    img {
      width: 13px;
      height: 13px;
      vertical-align: -2px;
    }
    span {
      padding: 0 0 0 4px;
      width: 47.5px;
      height: 11px;
      font-size: 12px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #111111;
      line-height: 3px;
    }
  }
  .product-footer {
    position: fixed;
    width: 100%;
    background: #E6E6DC;
    left: 0;
    bottom: 50px;
    z-index: 3;
    .fl {

        font-size: 1.4em;
        padding: 10px;
        font-family: DIN;
        font-weight: 500;
        color: #111111;

    }
    button {
      background: #E6E6DC;
      color:#292929;
      padding: 0;
      border: 0px;
      line-height: 56px;
      height: 56px;
      width: 40vw;
      border-left: 2px solid #FCFCF0;
      //background: linear-gradient(0deg, #9e7746, #f4c583);
      //color: #333;
      //border: 1px solid #9e7746;
      //border-radius: 10px;
      //overflow: hidden;
      //margin: 0 16px 0 0;
      //list-style: none;
    }
  }

  //@media screen and (max-width: 320px) {
  //  .item-ad {
  //    width: 90vw;
  //    top: 160px;
  //    font-size: 12px;
  //    height: 70px;
  //    p {
  //      margin: 0;
  //    }
  //  }
  //  .kongParents {
  //    top: 174px;
  //  }
  //  // .product-baozhuang {
  //  //   margin: -15vh 0 0 0;
  //  // }
  //  .productPack {
  //    .box {
  //      p {
  //        margin: 8.5px 0 7px 6px;
  //      }
  //      span {
  //        margin: 12.5px 0 0px 6px;
  //      }
  //    }
  //    .bag {
  //      p {
  //        margin: 8.5px 0 7px 10px;
  //      }
  //    }
  //  }
  //}
  //@media screen and (max-width: 375px) and (max-width: 414px) {
  //  .product-baozhuang {
  //    margin: -80px 0 0 0;
  //  }
  //}
  //@media screen and (min-width: 414px) {
  //  .product-baozhuang {
  //    margin: -80px 0 0 0;
  //  }
  //}
}
</style>