import notice from "@/utils/notice/index";

import toast from '@/utils/tool/toast';
import modal from "../../../utils/tool/modal";

import car from '@/utils/controls/car';

export default {

    data(){
        return {
            edit:false,
            loadingStatus:false,
            selectObject:{},
            selectArray:[]
        }
    },

    computed:{
      allSelect(){
          let data = this.getCarShowData();
          return !this.empty && data.length === this.selectArray.length;
      },
    empty(){
        let data = this.getCarShowData();
        return data.length <= 0;
    },
      total(){
          let price = 0;
          this.selectArray.map((item)=>{

              let index = this.flatIdGETIndex(item);
              if (index !== undefined) {
                  item = this.flatGetData(index);
                  if (item) {
                      price += item.sales_price * item.number;
                  }

              }
          });

          return price.toFixed(2);
      }
    },

    methods:{

        routerHome(){
          return this.$router.switchTab('index');
        },

        fetch(obj){
            return this.$request({
                url:'product/getCartList',
                token:true,
                page:obj
            }).then((data)=>{
                obj.success(data.data);
                return this.installSelectObject();
            }).catch(obj.fail);
        },

        // 设置安装
        installSelectObject(){
            let selectObject = {};
            this.selectArray.map((item)=>{
                if (this.flatIdGETIndex(item) !== undefined) {
                    selectObject[item] = 1;
                }
            });

            let selectArray = Object.keys(selectObject);
            if (selectArray !== this.selectArray) {
                this.selectArray = selectArray;
                this.selectObject = selectObject;
            }

        },

        triggerEdit(){

            if (this.loadingStatus) return null;

            this.edit = !this.edit;

        },

        triggerSelect(item){

            if (this.loadingStatus) return ;

            if (this.selectObject[item.id] === undefined) {
                this.$set(this.selectObject,item.id,1);
            } else {
                this.$set(this.selectObject,item.id,undefined);
                delete this.selectObject[item.id];
            }

            this.selectArray = Object.keys(this.selectObject);

        },

        triggerInput(item){

           let index = this.flatIdGETIndex(item.item.id);

            item.item.number = item.value;

            this.flatSaveData(index,item.item);

        },

        triggerChange(item){
            this.$request({
                url:'product/editCartInfo',
                data:{
                    cart_id: item.item.id,
                    number: item.value
                },
                token:true
            })
        },

        triggerAllSelect(){

            if (this.allSelect) {
                this.selectObject = {};
                this.selectArray = [];
            } else {
                this.getCarShowData().map((item,index)=>{
                    this.selectObject[item.id] = 1;
                });
                this.selectArray = Object.keys(this.selectObject);
            }

        },

        getCarShowData(){

            let data = this.base_flat_data.data || [];

            let resultData = [];

            data.map((item)=>{
                if (!this.base_flat_remove[item.id]) {
                    resultData.push(item);
                }
            })

            return resultData;

        },

        deleteCheckShop(){

            if (this.loadingStatus) return ;
            if (this.selectArray.length <= 0) return toast.info('请选择商品');

            modal.confirm({
                title:'移除确认',
                content:'是否移除选择商品?',
                confirmText:'移除',
                confirmColor:'#FC3538',
                confirm:()=>{
                    return  this.$request({
                        url:'product/delCart',
                        data:{
                            cart_ids: this.selectArray.join(',')
                        },
                        token:true,
                        message:true,
                        failMessage:true,
                        next:({status})=> this.loadingStatus = status
                    }).then((data)=>{

                        if (data.isSuccess) {

                            // 设置购物车数量
                            car.setCartNumber(car.number - this.selectArray.length);
                            car.getCartNumber();

                            this.selectArray.map((item)=>{
                                this.flatSplice(this.flatIdGETIndex(item));
                            });
                            this.selectObject = {};
                            this.selectArray = [];
                        }

                    });
                }
            })


        },

        triggerOrder(){
            if (this.loadingStatus) return ;
            if (this.selectArray.length <= 0) return toast.info('请选择商品');

            return this.$router.navigateTo({
                name:'confirm-order',
                params:{
                    is_cart:1,
                    choose_ids: this.selectArray.join(',')
                }
            });


        }

    },

    onShow(){
        if (this.useNoticeItem && this.useNoticeItem.update) {
            this.useNoticeItem = undefined;
            this.selectObject = {};
            this.selectArray = [];
            return this.flatReload(1,true);
        }
    },

    created(){
        notice.on('car',(item)=>{
            console.log(item);
            this.useNoticeItem = item;
        },'car');
    }

}