<template>
  <view>
    <view class="shopping" v-if="list.length==0">
      <shoppingCartnull picurl="../../static/img/shoppingnull.png" ale="购物车暂无商品"></shoppingCartnull>
    </view>
    <view v-else>
      <shoppingCarttittle @getyhq="getyhq"></shoppingCarttittle>
      <shoppingCartshop
        v-for="(i,j) in list"
        :key="j"
        @del="del"
        :shoppic="i.imgpaththumb"
        :money="i.price"
        :shopname="i.goods_name"
        :num="i.num"
        :no="j"
        @selected="ifselected"
        :ifxz="choose[j]"
      ></shoppingCartshop>
      <!-- <shoppingCartshop shoppic="../../static/img/cartshop.png" money="154.05"  shopname="360记录仪M301plus+4G卡" num=9 no=1 @selected="ifselected" :ifxz="choose[1]"></shoppingCartshop>
      <shoppingCartshop shoppic="../../static/img/cartshop.png" money="154.05"  shopname="360记录仪M301plus+4G卡" num=9 no=2 @selected="ifselected" :ifxz="choose[2]"></shoppingCartshop>-->

      <shoppingCartbottom
        class="heji"
        :price="bottom[1]"
        :sum="bottom[2]"
        :all="bottom[3]"
        @selectAll="selectAll"
        @jiesuan="jiesuan"
      ></shoppingCartbottom>
    </view>
    <view class="youhui" v-if="ifyh">
      <youhui :ifyouhui="ifyh" v-on:off="off" :yhqall="yhqall"></youhui>
    </view>
  </view>
</template>

<script>
import shoppingCartnull from "../../components/shopping/cartnull";
import shoppingCarttittle from "../../components/shopping/carttittle";
import shoppingCartshop from "../../components/shopping/cartshop";
import shoppingCartbottom from "../../components/shopping/cartbottom";
import youhui from "../../components/ordersub/ordersub-youhui";
import { requestP } from "../../static/js/request";
export default {
  components: {
    shoppingCartnull,
    shoppingCarttittle,
    shoppingCartshop,
    shoppingCartbottom,
    youhui
  },
  data() {
    return {
      choose: [],
      order: 0,
      list: [],
      bottom: [0, 0, 0, 0],
      ifyh: false,
      yhqall: [],
      goodsids:[]
    };
  },
  methods: {
    youhui() {
      this.ifyh = true;
    },
    off(e) {
      this.ifyh = e;
    },
    getyhq(e) {
      this.ifyh = e;
    },
    del(no) {
      console.log(this.list[no].cart_id);
      requestP({
        url: "shop_cart/del",
        data: {
          cart_id: this.list[no].cart_id
        }
      }).then(res => {
        console.log(res.data.code, "code");
        if (res.data.code) {
          wx.showToast({
            title: "删除成功",
            icon: "success",
            duration: 2000 //持续的时间
          });
          requestP({
            url: "shop_cart/get_list"
          }).then(res => {
            console.log(res);
            this.list = res.data.data;
            console.log(res.data.data.length == 0, 585);
            for (let i in this.list) {
              this.choose[i] = false;
              this.list[i].price = parseFloat(this.list[i].price);
            }
          });
        }
      });
    },
    selectAll(all) {
      this.bottom = [0, 0, 0, 0];
      this.$set(this.bottom, 3, all);
      for (let i in this.choose) {
        this.$set(this.choose, i, all);
        if (all) {
          this.$set(
            this.bottom,
            1,
            this.bottom[1] + this.list[i].price * 100 * this.list[i].num
          );
          this.$set(this.bottom, 2, this.bottom[2] + this.list[i].num);
        }
      }
    },
    ifselected(e) {
      // this.choose=e

      this.$set(this.choose, e[1], e[0]);

      // console.log(i)
      this.statistics();
      console.log(this.choose, "xuanzew");
      //    for (let i = 0; i < this.list.length; i++) {
      //     if (this.choose[i]==true) {
      //       // console.log(this.list[i].goods_id, "goodsid");
      //       this.goodsids.push(this.list[i].goods_id)
      //   }
      // }
      // this.$set(this.tag, i, this.tag[i]=="tag"?"tag1":"tag");
      // this.$set(this.choose,"choose",e)
      // console.log(this.xuanze,98)
    },
    statistics() {
      var count = 0;
      this.bottom = [0, 0, 0, 0];
      for (let i in this.choose) {
        if (this.choose[i]) {
          count++;
          this.$set(
            this.bottom,
            1,
            this.bottom[1] + this.list[i].price * 100 * this.list[i].num
          );
          this.$set(this.bottom, 2, this.bottom[2] + this.list[i].num);
        }
      }
      if (count == this.choose.length) {
        this.$set(this.bottom, 3, true);
      }
    },
    order1(e) {
      console.log(e, "asd1");
      this.order = e;
    },
    jiesuan() {
      // for (let i = 0; i < this.list.length; i++) {
      //     if (this.choose[i]==true) {
      //       // console.log(this.list[i].goods_id, "goodsid");
      //       this.goodsids.push(this.list[i].goods_id)
      //   }
      // }
      // this.choose.
      let arr = []
      for(let i in this.choose){
        if(this.choose[i]){
          arr.push(this.list[i].cart_id)
        }
      }
      requestP({
        url:"pay/pay_cart",
        data:{
          cart_ids:arr.join(",")
        }
      }).then((res)=>{
      uni.setStorageSync("ordersubinfo",res.data)
      uni.setStorageSync("cart_ids",arr)

        uni.navigateTo({
        url:"../order-sub/order-sub?shoptype=" + 3
      })
      })
      
    //  console.log(this.goodsids)
    }
  },
  mounted() {
    requestP({
      url: "shop_cart/get_list"
    }).then(res => {
      console.log(res);
      this.list = res.data.data;
      for (let i in this.list) {
        this.choose[i] = false;
        this.list[i].price = parseFloat(this.list[i].price);
      }
    });

    requestP({
      url: "youhuiquan/get_list_all"
    }).then(res => {
      console.log(res, "youhui");
      this.yhqall = res.data.data;
    });
  },
  onShow(){
    this.choose = [],
      this.order = 0,
      this.list = [],
      this.bottom = [0, 0, 0, 0],
      this.ifyh = false,
      this.yhqall = [],
      this.goodsids =[]
    requestP({
      url: "shop_cart/get_list"
    }).then(res => {
      console.log(res);
      this.list = res.data.data;
      for (let i in this.list) {
        this.choose[i] = false;
        this.list[i].price = parseFloat(this.list[i].price);
      }
    });

    requestP({
      url: "youhuiquan/get_list_all"
    }).then(res => {
      console.log(res, "youhui");
      this.yhqall = res.data.data;
    });
  }
};
// isnull 判断购物车是否为空   true 为空，false不为空
</script>

<style lang="scss">
page {
  background-color: #f6f6f6;
}
.heji {
  width: 100%;
  position: fixed;
  bottom: 0;
}
</style>