// import test from '@/utils/test/index';
import toast from '@/utils/tool/toast';
import notice from '@/utils/notice/index';
import car from '@/utils/controls/car';
import modal from "../../../utils/tool/modal";

export default {

    data(){
      return {
          payModal:false,
          payFailModal:false,
          payFailMessage:''
      }
    },

    methods:{

        openPayModal(message){
          if (!this.payModal) {
              this.payModal = true;
              this.payFailMessage = message || '';
          }
        },

        paySubmit(){
            if (this.loadingStatus || this.submitStatus) return null;

            // 请求 data
            let requestData = {};

            // 查看是否有地址
            if (!this.address.id) {
                return toast.info('请选择收货地址');
            }
            requestData.address_id = this.address.id;

            // 设置周期购
            if (this.$params.cycle && this.$params.per_month) {
                requestData.cycle = this.$params.cycle;
                requestData.per_month = this.$params.per_month;
            }

            // 优惠券id
            let couponKeys = Object.keys(this.couponId);
            requestData.coupon_id = couponKeys.join(',');

            // 设置 买家留言
            requestData.desc = this.messageValue || '';

            // 设置产品信息
            requestData.products =JSON.stringify((this.info.productInfo || []).map((item)=>{
                return {
                    product_id: item.product_id,
                    number: item.number,
                    delivery_id: item.delivery_id,
                    sales_price: item.sales_price
                };
            }));

            modal.confirm({
                title: '支付确认',
                content: '是否支付该订单?',
                confirm:()=>{
                    this.openPayLoading();

                    // 如果存在订单号触发
                    if (this.orderNumber) {
                        return this.payOrder();
                    } else {
                        return this.createdOrder(requestData);
                    }
                }
            });

        },

        // 创建
        createdOrder(data){

          return this.$request({
              url:'order/createOrder',
              data:{
                  ...data,
                  cart_id: this.$params.choose_ids ? JSON.stringify((this.$params.choose_ids || '').split(',')) : ''
              },
              token:true,
              next:({status}) => {
                  this.submitStatus = status;
              },
              message:2,
              failMessage:true
          }).then((data)=>{
              this.closePayLoading();
              if (data.isSuccess) {
                  this.orderNumber = data.data;

                  notice.trigger('user',{
                      update:true
                  });

                  if (this.$params.is_cart === '1') {
                      notice.trigger('car',{
                          update:true
                      });

                      // 设置购物车数量
                      car.getCartNumber(true);

                  }

                  return this.payOrder();
              }
          }).catch(()=> this.closePayLoading());
        },

        // 执行支付订单
        payOrder(){
          return this.$request({
              url:'order/toPay',
              data:{
                  order_id: this.orderNumber
              },
              token:true,
              failMessage:true
          }).then((data)=>{
              this.closePayLoading();
              if (data.isSuccess) {
                  notice.trigger('user',{
                      update:true
                  });
                  return  this.openPayModal();
              } else {
                  return  this.openPayModal(data.msg);
              }
          }).catch(()=> this.closePayLoading())
        },

        openPayLoading(){
            this.payTime = setTimeout(()=>{
                toast.loading('处理中');
            },500);
        },

        closePayLoading(){
            clearTimeout(this.payTime);
            return toast.hideLoading();
        }

    }

}