<?php
$db =& get_instance();
$userdetails=$db->userdetails();
$pro_setting = $this->Product_model->getSettings('productsetting');
$form_setting = $this->Product_model->getSettings('formsetting');
?>
<?php foreach($data_list as $index => $product){ ?>
    <?php
    $display_class = '';
    if ($product['on_store'] == 0 && $product['product_created_by'] == 1) {
        $display_class = 'd-none';
    }else{
        $display_class = '';
    }
    ?>
    <?php if(isset($product['is_form'])){ ?>
        <div class="card border-0 shadow-sm mb-3 <?= $display_class ?>">
            <div class="card-header bg-light border-0 py-2">
                <div class="d-flex justify-content-between align-items-center">
                    <h6 class="mb-0 text-dark fw-semibold">
                        <i class="fas fa-wpforms me-2"></i><?= __('user.campaign') ?>
                    </h6>
                    <button class="btn btn-outline-dark btn-sm" type="button" data-bs-toggle="collapse" data-bs-target="#formDetails<?= $product['form_id'] ?>" aria-expanded="false">
                        <?= __('user.details') ?>
                    </button>
                </div>
            </div>
            <div class="card-body p-3">
                <!-- Top Row: Icon, Title, Type -->
                <div class="row align-items-center mb-3">
                    <div class="col-auto">
                        <div class="position-relative">
                            <div class="d-flex align-items-center justify-content-center bg-light border rounded" style="width: 40px; height: 40px; overflow: hidden;">
                                <img class="w-100 h-100" style="object-fit: cover;" src="<?php echo base_url($product['fevi_icon']) ?>" alt="<?= __('user.form_icon') ?>" loading="lazy">
                            </div>
                            <span class="position-absolute top-0 start-100 translate-middle badge bg-secondary rounded-pill">
                                <i class="fas fa-file-alt fa-2xs"></i>
                            </span>
                        </div>
                    </div>
                    <div class="col">
                        <h5 class="mb-1 fw-bold text-dark"><?= $product['title'] ?></h5>
                        <div class="d-flex align-items-center gap-2 flex-wrap">
                            <span class="badge bg-light text-dark rounded-pill">
                                <i class="fas fa-wpforms me-1"></i><?= __('user.campaign') ?>
                            </span>
                            <small class="text-muted"><?= __('user.high_converting') ?></small>
                        </div>
                    </div>
                </div>

                <!-- Middle Row: Stats -->
                <div class="row g-3 mb-3">
                    <!-- Views -->
                    <div class="col-md-3 col-6">
                        <div class="bg-primary text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?php echo $product['view_statistics'] ?></div>
                            <small><i class="fas fa-eye me-1"></i><?= __('user.total_views') ?> <span class="badge bg-light text-dark" style="font-size: 0.5rem;"><?php echo $product['my_view_statistics'] ?? 0 ?> <?= __('user.my_views') ?></span></small>
                        </div>
                    </div>
                    <!-- Sale Rate -->
                    <div class="col-md-3 col-6">
                        <?php 
                        $ordercountratio=0;
                        if($product['view_statistics']>0)
                           $ordercountratio=$product['all_count_commission']*100/$product['view_statistics'];
                        $ordercountratio = is_float($ordercountratio) ==1 ? number_format((float)$ordercountratio, 2, '.', '') : $ordercountratio ;
                        ?>
                        <div class="bg-success text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $ordercountratio ?>%</div>
                            <small><i class="fas fa-shopping-cart me-1"></i><?= __('user.sale') ?> <?= __('user.rate') ?></small>
                        </div>
                    </div>
                    <!-- Click Rate -->
                    <div class="col-md-3 col-6">
                        <?php
                        $clickratio=0;
                        $comissionclickcount=(int)$product['all_commition_click_count']  ; 
                        if($product['view_statistics']>0)
                           $clickratio=$comissionclickcount*100/$product['view_statistics'];
                        $clickratio = is_float($clickratio) ==1 ? number_format((float)$clickratio, 2, '.', '') : $clickratio;
                        ?>
                        <div class="bg-info text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $clickratio ?>%</div>
                            <small><i class="fas fa-mouse-pointer me-1"></i><?= __('user.click') ?> <?= __('user.rate') ?></small>
                        </div>
                    </div>
                    <!-- Earnings -->
                    <div class="col-md-3 col-6">
                        <div class="bg-light border rounded p-2 h-100">
                            <div class="text-center">
                                <small class="text-muted d-block mb-1">
                                    <i class="fas fa-coins text-warning me-1"></i><?= __('user.earnings') ?>
                                </small>
                                <div class="d-flex flex-column gap-1">
                                    <?php
                                    if($product['sale_commision_type'] == 'default'){
                                        $commissionType = $form_default_commission['product_commission_type'];
                                        if($form_default_commission['product_commission_type'] == 'percentage'){
                                            if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $form_default_commission['product_commission'])
                                                $product_commission = $userComission['value'];
                                            else
                                                $product_commission = $form_default_commission['product_commission'];
                                         echo '<span class="badge bg-success">'.$product_commission .'% '.__('user.per_sale').'</span>';
                                        }
                                        else if($form_default_commission['product_commission_type'] == 'Fixed'){
                                         echo '<span class="badge bg-success metric-badge">'.c_format($form_default_commission['product_commission']) .' '.__('user.per_sale').'</span>';
                                        }
                                    }
                                    else if($product['sale_commision_type'] == 'percentage'){
                                        if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $product['sale_commision_value'])
                                            $sale_commision_value = $userComission['value'];
                                        else
                                            $sale_commision_value = $product['sale_commision_value'];
                                     echo '<span class="badge bg-success metric-badge">'.$sale_commision_value .'% '.__('user.per_sale').'</span>';
                                    }
                                    else if($product['sale_commision_type'] == 'fixed'){
                                     echo '<span class="badge bg-success metric-badge">'.c_format($product['sale_commision_value']) .' '.__('user.per_sale').'</span>';
                                    }

                                    if($product['click_commision_type'] == 'default'){
                                        if((int)$product['vendor_id']){
                                            $vendor_setting = $this->db->query("SELECT * FROM vendor_setting WHERE user_id=". (int)$product['vendor_id'] ." ")->row();
                                         echo '<span class="badge bg-info metric-badge">'.c_format($vendor_setting->form_affiliate_click_amount) .'/'.(int)$vendor_setting->form_affiliate_click_count .' '.__('user.clicks').'</span>';
                                        } else {
                                            $commissionType = $form_default_commission['product_commission_type'];
                                            if($form_default_commission['product_commission_type'] == 'percentage'){
                                             echo '<span class="badge bg-info metric-badge">'.c_format($form_default_commission['product_ppc']) .'/'. $form_default_commission['product_noofpercommission'] .' '.__('user.clicks').'</span>';
                                            }
                                            else if($form_default_commission['product_commission_type'] == 'Fixed'){
                                             echo '<span class="badge bg-info metric-badge">'.c_format($form_default_commission['product_ppc']) .'/'. $form_default_commission['product_noofpercommission'] .' '.__('user.clicks').'</span>';
                                            }
                                        }
                                    }
                                    else if($product['click_commision_type'] == 'custom') {
                                     echo '<span class="badge bg-info metric-badge">'.c_format($product['click_commision_per']) .'/'. $product['click_commision_ppc'] .' '.__('user.clicks').'</span>';
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Bottom Row: URL and Actions -->
                <div class="row align-items-center">
                    <div class="col-lg-8 col-md-7 mb-2 mb-md-0">
                        <?php
                        if($product['slug']) {
                            $shareUrl = base_url($product['slug']);
                        }else{
                            $shareUrl = $product['public_page'];
                        }
                        ?>
                        <div class="input-group input-group-sm">
                            <input readonly="readonly" value="<?= $shareUrl ?>" class="form-control bg-light border-0 text-truncate input-form-url-<?= $product['form_id'] ?>">
                            <button class="btn btn-outline-secondary" type="button" copyToClipboard="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.copied') ?>">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-5">
                        <div class="d-flex gap-1 justify-content-end">
                            <button class="btn btn-outline-danger btn-sm" data-type="form" data-related-id="<?= $product['form_id'] ?>" data-input-class="input-form-url-<?= $product['form_id'] ?>" data-bs-toggle="tooltip" title="<?= __('user.slug_settings') ?>">
                                <i class="fas fa-cog fa-sm"></i>
                            </button>
                            <a class="btn btn-outline-primary btn-sm" target="_blank" href="<?= $product['public_page'] ?>" data-bs-toggle="tooltip" title="<?= __('user.preview') ?>">
                                <i class="fas fa-external-link-alt fa-sm"></i>
                            </a>
                            <button class="btn btn-outline-success action-btn" onclick="generateCodeForm(<?php echo $product['form_id'];?>,this);" data-bs-toggle="tooltip" title="<?= __('user.get_code') ?>">
                                <i class="fas fa-code fa-sm"></i>
                            </button>
                            <button class="btn btn-outline-info action-btn get-downloads" onclick="downloadCode(this, <?= $product['form_id'] ?>, 'form')" data-bs-toggle="tooltip" title="<?= __('user.download_cam_pack') ?>">
                                <i class="fas fa-download fa-sm"></i>
                            </button>
                            <button class="btn btn-outline-secondary action-btn qrcode" data-id="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.qrcode') ?>">
                                <i class="fas fa-qrcode fa-sm"></i>
                            </button>
                            <button class="btn btn-outline-warning action-btn" data-social-share data-share-url="<?= $shareUrl ?>" data-share-title="<?= $product['title'];?>" data-share-desc="<?= $product['description'];?>" data-bs-toggle="tooltip" title="<?= __('user.share_campaign') ?>">
                                <i class="fas fa-share-from-square fa-sm"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Recurring Badge -->
                <?php if($product['form_recursion_type']){ ?>
                <div class="mt-2">
                    <?php
                    if($product['form_recursion_type'] == 'custom'){
                        if($product['form_recursion'] != 'custom_time'){
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': ' . __('user.'. $product['form_recursion']).'</span>';
                        } else {
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. timetosting($product['recursion_custom_time']).'</span>';
                        }
                    } else{
                        if($form_setting['form_recursion'] == 'custom_time' ){
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. timetosting($form_setting['recursion_custom_time']).'</span>';
                        } else {
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. __('user.'. $form_setting['form_recursion']).'</span>';
                        }
                    }
                    ?>
                </div>
                <?php } ?>
            </div>
            
            <!-- Collapsible Details -->
            <div class="collapse" id="formDetails<?= $product['form_id'] ?>">
                <div class="card-footer bg-light border-0">
                    <div class="row g-3">
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded p-3 text-center">
                                <h6 class="text-success mb-1"><?= $product['all_count_commission'] ?></h6>
                                <small class="text-muted"><?= __('user.sale_count') ?></small>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded p-3 text-center">
                                <h6 class="text-success mb-1"><?= c_format($product['all_count_commission'] * ($product['sale_commision_value'] ?? 10)) ?></h6>
                                <small class="text-muted"><?= __('user.sale_amount') ?></small>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded p-3 text-center">
                                <h6 class="text-info mb-1"><?= $product['all_commition_click_count'] ?></h6>
                                <small class="text-muted"><?= __('user.click_count') ?></small>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="bg-success text-white rounded p-3 text-center">
                                <h6 class="mb-1"><?= c_format($product['all_commition_click_count'] * ($product['click_commision_ppc'] ?? 1)) ?></h6>
                                <small><?= __('user.click_amount') ?></small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
       
    <?php } else if(isset($product['is_product'])) { ?>
        <?php 
        if($product['is_campaign_product']) {
            $af_id = _encrypt_decrypt($userdetails['id']."-".$product['product_id']);
            $productLink = addParams($product['product_url'],"af_id",$af_id);
        } else {
            $productLink = base_url('store/'. base64_encode($userdetails['id']) .'/product/'.$product['product_slug'] );
        }
        ?>
        <div class="card border-0 shadow-sm mb-3 <?= $display_class ?>">
            <div class="card-header bg-light border-0 py-2">
                <div class="d-flex justify-content-between align-items-center">
                    <h6 class="mb-0 text-dark fw-semibold"><i class="fas fa-box me-2"></i><?= __('user.campaign') ?></h6>
                    <button class="btn btn-outline-dark btn-sm" type="button" data-bs-toggle="collapse" data-bs-target="#productCollapse<?= $index ?>" aria-expanded="false" aria-controls="productCollapse<?= $index ?>">
                        <?= __('user.details') ?>
                    </button>
                </div>
            </div>
            
            <div class="card-body p-3">
                <!-- Top Row: Product Info -->
                <div class="row align-items-center mb-3">
                    <div class="col-auto">
                        <div class="position-relative">
                            <?php
                            $imagePath = $product['product_featured_image'];
                            $isExternal = preg_match('/^https?:\/\//', $imagePath);
                            $imageSrc = $isExternal ? $imagePath : base_url('assets/images/product/upload/thumb/' . $imagePath);
                            if (empty($imagePath)) {
                           $imageSrc = base_url('assets/images/no_product_image.png');
                            }
                            ?>
                            <div class="d-flex align-items-center justify-content-center bg-light border rounded" style="width: 40px; height: 40px; overflow: hidden;">
                                <img class="w-100 h-100" style="object-fit: cover;" src="<?= $imageSrc ?>" alt="<?= __('user.product_image') ?>" loading="lazy">
                            </div>
                            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-success">
                                <i class="fas fa-box fa-2xs"></i>
                            </span>
                        </div>
                    </div>
                    <div class="col">
                        <h5 class="mb-1 fw-bold text-dark"><?php echo $product['product_name'];?></h5>
                        <small class="text-muted"><i class="fas fa-box me-1"></i><?= __('user.campaign') ?></small>
                    </div>
                </div>

                <!-- Stats Row -->
                <div class="row g-3 mb-3">
                    <div class="col-md-3 col-6">
                        <div class="bg-primary text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $product['view_statistics'] ?></div>
                            <small><i class="fas fa-eye me-1"></i><?= __('user.total_views') ?> <span class="badge bg-light text-dark" style="font-size: 0.5rem;"><?= $product['my_view_statistics'] ?? 0 ?> <?= __('user.my_views') ?></span></small>
                        </div>
                    </div>
                    <div class="col-md-3 col-6">
                        <?php 
                        $ordercountratio=0;
                        if($product['view_statistics']>0)
                            $ordercountratio=$product['all_order_count']*100/$product['view_statistics'];
                        $ordercountratio = is_float($ordercountratio) ==1 ? number_format((float)$ordercountratio, 2, '.', '') : $ordercountratio;
                        ?>
                        <div class="bg-success text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $ordercountratio ?>%</div>
                            <small><i class="fas fa-shopping-cart me-1"></i><?= __('user.sales') ?></small>
                        </div>
                    </div>
                    <div class="col-md-3 col-6">
                        <?php
                        $clickratio=0;
                        $comissionclickcount=(int)$product['all_commition_click_count'] ;
                        if($product['view_statistics']>0)
                            $clickratio=$comissionclickcount*100/$product['view_statistics'];
                        $clickratio = is_float($clickratio) ==1 ? number_format((float)$clickratio, 2, '.', '') : $clickratio;
                        ?>
                        <div class="bg-warning text-dark rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $clickratio ?>%</div>
                            <small><i class="fas fa-mouse-pointer me-1"></i><?= __('user.clicks') ?></small>
                        </div>
                    </div>
                    <div class="col-md-3 col-6">
                        <div class="bg-light border rounded p-2 h-100">
                            <div class="text-center">
                                <small class="text-muted d-block mb-1">
                                    <i class="fas fa-coins text-warning me-1"></i><?= __('user.earnings') ?>
                                </small>
                                <div class="d-flex flex-column gap-1">
                                    <?php 
                                    if($product['seller_id']){
                                        $seller = $this->Product_model->getSellerFromProduct($product['product_id']);
                                        $seller_setting = $this->Product_model->getSellerSetting($seller->user_id);

                                        $commnent_line = "";
                                        if($seller->affiliate_sale_commission_type == 'default'){ 
                                            if($seller_setting->affiliate_sale_commission_type == ''){
                                                $commnent_line .= __('user.warning_default_commission_not_set');
                                            }
                                            else if($seller_setting->affiliate_sale_commission_type == 'percentage'){
                                                if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $seller_setting->affiliate_commission_value)
                                                    $affiliate_commission_value = $userComission['value'];
                                                else
                                                    $affiliate_commission_value = (float) $seller_setting->affiliate_commission_value;
                                                $commnent_line .= $affiliate_commission_value .'% '.__('user.per_sale');
                                            }
                                            else if($seller_setting->affiliate_sale_commission_type == 'fixed'){
                                                $commnent_line .= c_format($seller_setting->affiliate_commission_value).' '.__('user.per_sale');
                                            }
                                        } else if($seller->affiliate_sale_commission_type == 'percentage'){
                                            if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $seller->affiliate_commission_value)
                                                $affiliate_commission_value = $userComission['value'];
                                            else
                                                $affiliate_commission_value = (float) $seller->affiliate_commission_value;
                                            $commnent_line .=  $affiliate_commission_value .'% '.__('user.per_sale');
                                        } else if($seller->affiliate_sale_commission_type == 'fixed'){
                                            $commnent_line .= c_format($seller->affiliate_commission_value).' '.__('user.per_sale');
                                        } 
                                   echo '<span class="badge bg-success">' .$commnent_line.'</span>';

                                        $commnent_line = "";
                                        if($seller->affiliate_click_commission_type == 'default'){ 
                                            $commnent_line .= c_format($seller_setting->affiliate_click_amount) ."/". (int)$seller_setting->affiliate_click_count ." ".__('user.clicks');
                                        } else{
                                            $commnent_line .= c_format($seller->affiliate_click_amount) ."/". (int)$seller->affiliate_click_count ." ".__('user.clicks');
                                        } 
                                   echo '<span class="badge bg-info">' .$commnent_line.'</span>';
                                   } else { 
                                        if($product['product_commision_type'] == 'default'){
                                            if($default_commition['product_commission_type'] == 'percentage'){
                                                if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $default_commition['product_commission'])
                                                    $product_commission = $userComission['value'];
                                                else
                                                    $product_commission = $default_commition['product_commission'];
                                           echo '<span class="badge bg-success">'.$product_commission. "% ".__('user.per_sale').'</span>';
                                            } else {
                                           echo '<span class="badge bg-success">'.c_format($default_commition['product_commission']) ." ".__('user.per_sale').'</span>';
                                            }
                                        } else if($product['product_commision_type'] == 'percentage'){
                                            if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $product['product_commision_value'])
                                                $product_commision_value = $userComission['value'];
                                            else
                                                $product_commision_value = $product['product_commision_value'];
                                       echo '<span class="badge bg-success">'.$product_commision_value. "% ".__('user.per_sale').'</span>';
                                        } else{
                                       echo '<span class="badge bg-success">'.c_format($product['product_commision_value']) ." ".__('user.per_sale').'</span>';
                                        }
                                        if($product['product_click_commision_type'] == 'default'){
                                       echo '<span class="badge bg-info">'.c_format($default_commition['product_ppc']) ."/". $default_commition['product_noofpercommission'] ." ".__('user.clicks').'</span>';   
                                        } else{
                                       echo '<span class="badge bg-info">'.c_format($product['product_click_commision_ppc']) ."/". $product['product_click_commision_per'] ." ".__('user.clicks').'</span>';
                                        }
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- URL and Actions Row -->
                <div class="row align-items-center">
                    <div class="col-lg-8 col-md-7 mb-2 mb-md-0">
                        <?php
                        if($product['slug']) {
                            $shareUrl = base_url($product['slug']);
                        }else{
                            $shareUrl = $productLink;
                        }
                        ?>
                        <div class="input-group input-group-sm">
                            <input readonly="readonly" value="<?= $shareUrl ?>" class="form-control bg-light border-0 text-truncate input-product-url-<?= $product['product_id'] ?>">
                            <button class="btn btn-outline-secondary" type="button" copyToClipboard="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.copied') ?>">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-5">
                        <div class="d-flex gap-1 justify-content-end">
                            <a class="btn btn-outline-danger action-btn btn-model-slug" data-type="product" data-related-id="<?= $product['product_id'] ?>" data-input-class="input-product-url-<?= $product['product_id'] ?>" href="javascript:void(0);" data-bs-toggle="tooltip" title="<?= __('user.slug_settings') ?>">
                                <i class="fas fa-cog fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-primary action-btn" target='_blank' href="<?= $productLink ?>" data-bs-toggle="tooltip" title="<?= __('user.preview') ?>">
                                <i class="fas fa-external-link-alt fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-success action-btn" onclick="generateCode(<?php echo $product['product_id'];?>,this);" href="javascript:void(0);" data-bs-toggle="tooltip" title="<?= __('user.get_code') ?>">
                                <i class="fas fa-code fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-info action-btn get-downloads" href="javascript:void(0)" onclick="downloadCode(this, <?= $product['product_id'] ?>, 'form')" data-bs-toggle="tooltip" title="<?= __('user.download_cam_pack') ?>">
                                <i class="fas fa-download fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-secondary action-btn qrcode" href="javascript:void(0)" data-id="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.qrcode') ?>">
                                <i class="fas fa-qrcode fa-sm"></i>
                            </a>
                            <a href="javascript:void(0);" class="btn btn-outline-warning action-btn" data-social-share data-share-url="<?= $shareUrl ?>" data-share-title="<?= $product['product_name'];?>" data-share-desc="<?= $product['product_short_description'];?>" data-bs-toggle="tooltip" title="<?= __('user.share_campaign') ?>">
                                <i class="fas fa-share-from-square fa-sm"></i>
                            </a>
                        </div>
                    </div>
                </div>

                <!-- Recurring Badge -->
                <?php if($product['product_recursion_type']){ ?>
                <div class="mt-2">
                    <?php
                    if($product['product_recursion_type'] == 'custom'){
                        if($product['product_recursion'] != 'custom_time'){
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': ' . __('user.'.$product['product_recursion']).'</span>';
                        } else {
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. timetosting($product['recursion_custom_time']).'</span>';
                        }
                    } else{
                        if($pro_setting['product_recursion'] == 'custom_time' ){
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. timetosting($pro_setting['recursion_custom_time']).'</span>';
                        } else {
                     echo '<span class="badge bg-warning text-dark"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. __('user.'.$pro_setting['product_recursion']).'</span>';
                        }
                    }
                    ?>
                </div>
                <?php } ?>
            </div>
            
            <!-- Collapsible Details -->
            <div class="collapse" id="productCollapse<?= $index ?>">
                <div class="card-footer bg-light border-0">
                    <div class="row g-3">
                        <div class="col-md-2 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-primary mb-1 fw-bold"><?php echo c_format($product['product_price']); ?></h6>
                                <small class="text-muted"><strong><?= __('admin.price') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-info mb-1 fw-bold"><?php echo $product['product_sku'];?></h6>
                                <small class="text-muted"><strong><?= __('admin.sku') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-success mb-1 fw-bold"><?php echo $product['order_count'];?> / <?php echo c_format($product['commission']) ;?></h6>
                                <small class="text-muted"><strong><?= __('admin.sales_/_commission') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-warning mb-1 fw-bold"><?php echo (int)$product['commition_click_count'];?> / <?php echo c_format($product['commition_click']) ;?></h6>
                                <small class="text-muted"><strong><?= __('admin.clicks_/_commission') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="<?= $product['on_store'] == '1' ? 'text-success' : 'text-danger' ?> mb-1 fw-bold"><?= $product['on_store'] == '1' ? __('user.yes') : __('user.no') ?></h6>
                                <small class="text-muted"><strong><?= __('admin.display') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-2 col-6">
                            <div class="bg-success bg-gradient text-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="mb-1 fw-bold"><?php echo c_format((float)$product['commition_click'] + (float)$product['commission']); ?></h6>
                                <small><strong><?= __('admin.total') ?></strong></small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
           
    <?php } else{ ?>
        <?php 
        $productLink = base_url('store/'. base64_encode($userdetails['id']) .'/product/'.$product['product_slug'] );
        ?>
        <div class="card border-0 shadow-sm mb-3 <?= $display_class ?>">
            <div class="card-header bg-light border-0 py-2">
                <div class="d-flex justify-content-between align-items-center">
                    <h6 class="mb-0 text-dark fw-semibold"><i class="fas fa-tools me-2"></i><?= __('user.campaign') ?></h6>
                    <button class="btn btn-outline-dark btn-sm" type="button" data-bs-toggle="collapse" data-bs-target="#toolCollapse<?= $index ?>" aria-expanded="false" aria-controls="toolCollapse<?= $index ?>">
                        <?= __('user.details') ?>
                    </button>
                </div>
            </div>
            
            <div class="card-body p-3">
                <!-- Top Row: Tool Info -->
                <div class="row align-items-center mb-3">
                    <div class="col-auto">
                        <div class="position-relative">
                            <div class="d-flex align-items-center justify-content-center bg-white border rounded p-1" style="width: 50px; height: 50px; overflow: hidden;">
                                <?php 
                                    $tool_img = base_url('assets/images/no_product_image.png');
                                    if(!empty($product['featured_image']) && file_exists(FCPATH.'assets/images/product/upload/thumb/'.$product['featured_image'])) {
                                        $tool_img = base_url('assets/images/product/upload/thumb/'. $product['featured_image']);
                                    } elseif(!empty($product['featured_image']) && preg_match('/^https?:\/\//', $product['featured_image'])) {
                                        $tool_img = $product['featured_image'];
                                    }
                                ?>
                                <img class="w-100 h-100" style="object-fit: contain;" src="<?= $tool_img ?>" alt="<?= __('user.product_image') ?>" loading="lazy">
                            </div>
                            <span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-info">
                                <i class="fas fa-tools fa-2xs"></i>
                            </span>
                        </div>
                    </div>
                    <div class="col">
                        <h5 class="mb-1 fw-bold text-dark"><?= $product['name'] ?></h5>
                        <small class="text-muted"><i class="fas fa-tools me-1"></i><?= __('user.campaign') ?></small>
                    </div>
                </div>

                <!-- Stats Row -->
                <div class="row g-3 mb-3">
                    <div class="col-md-3 col-6">
                        <div class="bg-primary text-white rounded p-3 text-center">
                            <div class="h4 mb-1 fw-bold"><?= $product['total_trigger_count'] ?></div>
                            <small><i class="fas fa-chart-line me-1"></i><?= __('user.total_triggers') ?> <span class="badge bg-light text-dark" style="font-size: 0.5rem;"><?= $product['my_view_statistics'] ?? 0 ?> <?= __('user.my_views') ?></span></small>
                        </div>
                    </div>
                    <div class="col-md-3 col-6">
                        <?php 
                        if($product['_tool_type'] == 'program' && $product['sale_status']){
                            $conversionratio=0;
                            $totalratiocount=(int)$product['all_sale_count'];
                            if($product['total_trigger_count']>0)
                            $conversionratio=(int)($totalratiocount*100/$product['total_trigger_count']);
                            $conversionratio = is_float($conversionratio) ==1 ? number_format((float)$conversionratio, 2, '.', '') : $conversionratio;
                            echo '<div class="bg-success text-white rounded p-3 text-center"><div class="h4 mb-1 fw-bold">'. $conversionratio . '%</div><small><i class="fas fa-shopping-cart me-1"></i>'.__('user.sales').'</small></div>';
                        } else if($product['_tool_type'] == 'action' || $product['_tool_type'] == 'single_action'){
                            $conversionratio=0;
                            $totalratiocount=(int)$product['all_action_click_count'];
                            if($product['total_trigger_count']>0)
                                $conversionratio=(int)($totalratiocount*100/$product['total_trigger_count']);
                            $conversionratio = is_float($conversionratio) ==1 ? number_format((float)$conversionratio, 2, '.', '') : $conversionratio;
                            echo '<div class="bg-warning text-dark rounded p-3 text-center"><div class="h4 mb-1 fw-bold">'. $conversionratio . '%</div><small><i class="fas fa-hand-pointer me-1"></i>'.__('user.actions').'</small></div>';
                        } else if($product['_tool_type'] == 'general_click'){
                            $conversionratio=0;
                            $totalratiocount=(int)$product['all_general_click_count'];
                            if($product['total_trigger_count']>0)
                                $conversionratio=(int)($totalratiocount*100/$product['total_trigger_count']);
                            $conversionratio = is_float($conversionratio) ==1 ? number_format((float)$conversionratio, 2, '.', '') : $conversionratio;
                            echo '<div class="bg-info text-white rounded p-3 text-center"><div class="h4 mb-1 fw-bold">'. $conversionratio . '%</div><small><i class="fas fa-mouse-pointer me-1"></i>'.__('user.clicks').'</small></div>';
                        } else {
                            echo '<div class="bg-secondary text-white rounded p-3 text-center"><div class="h4 mb-1 fw-bold">0%</div><small><i class="fas fa-chart-bar me-1"></i>'.__('user.conversion').'</small></div>';
                        }
                        ?>
                    </div>
                    <div class="col-md-3 col-6">
                        <?php
                        if($product['_tool_type'] == 'program' && $product['click_status']){
                            $conversionratio=0;
                            $totalratiocount=(int)$product['all_click_count'];
                            if($product['total_trigger_count']>0)
                                $conversionratio=(int)($totalratiocount*100/$product['total_trigger_count']);
                            $conversionratio = is_float($conversionratio) ==1 ? number_format((float)$conversionratio, 2, '.', '') : $conversionratio;
                            echo '<div class="bg-secondary text-white rounded p-3 text-center"><div class="h4 mb-1 fw-bold">'. $conversionratio . '%</div><small><i class="fas fa-external-link-alt me-1"></i>'.__('user.p_clicks').'</small></div>';
                        } else {
                            echo '<div class="bg-light text-dark rounded p-3 text-center"><div class="h4 mb-1 fw-bold">-</div><small><i class="fas fa-minus me-1"></i>'.__('user.n_a').'</small></div>';
                        }
                        ?>
                    </div>
                    <div class="col-md-3 col-6">
                        <div class="bg-light border rounded p-2 h-100">
                            <div class="text-center">
                                <small class="text-muted d-block mb-1">
                                    <i class="fas fa-coins text-warning me-1"></i><?= __('user.earnings') ?>
                                </small>
                                <div class="d-flex flex-column gap-1">
                                    <?php 
                                    // Sale Commission
                                    if($product['_tool_type'] == 'program' && $product['sale_status']) {
                                    $comm = '';
                                    if($product['commission_type'] == 'percentage'){
                                        if($award_level_status == 1 && $userComission['status'] && $userComission['value'] && $userComission['value'] < $product['commission_sale'])
                                            $commission_sale = $userComission['value'];
                                        else
                                            $commission_sale = $product['commission_sale'];
                                        $comm = $commission_sale.'% '.__('user.per_sale'); 
                                    } else if($product['commission_type'] == 'fixed'){ 
                                        $comm = c_format($product['commission_sale']).' '.__('user.per_sale'); 
                                    }
                                        echo '<span class="badge bg-success">'.$comm.'</span>';
                                    }
                                    
                                    // Click Commission for Program Tools
                                    if($product['_tool_type'] == 'program' && $product['click_status']) {
                                       echo '<span class="badge bg-info">'.c_format($product["commission_click_commission"]). "/". $product['commission_number_of_click'] ." ".__('user.clicks').'</span>';
                                    }
                                    
                                    // General Click Commission
                                    if($product['_tool_type'] == 'general_click') {
                                       echo '<span class="badge bg-info">'.c_format($product["general_amount"]). "/". $product['general_click'] ." ".__('user.clicks').'</span>';
                                    }
                                    
                                    // Action Commission
                                    if($product['_tool_type'] == 'action' || $product['_tool_type'] == 'single_action') {
                                       echo '<span class="badge bg-warning text-dark">'.c_format($product["action_amount"]). "/". $product['action_click'] ." ".__('user.actions').'</span>';
                                    }
                                    ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- URL and Actions Row -->
                <div class="row align-items-center">
                    <div class="col-lg-8 col-md-7 mb-2 mb-md-0">
                        <?php
                        if($product['slug']) {
                            $shareUrl = base_url($product['slug']);
                        }else{
                            $shareUrl = $product['redirectLocation'][0];
                        }
                        ?>
                        <div class="input-group input-group-sm">
                            <input readonly="readonly" value="<?= $shareUrl ?>" class="form-control bg-light border-0 text-truncate input-<?= $product['_tool_type'] ?>-url-<?= $product['id'] ?>">
                            <button class="btn btn-outline-secondary" type="button" copyToClipboard="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.copied') ?>">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                    </div>
                    <div class="col-lg-4 col-md-5">
                        <div class="d-flex gap-1 justify-content-end">
                            <a class="btn btn-outline-danger action-btn btn-model-slug" data-type="<?= $product['_tool_type'] ?>" data-related-id="<?= $product['id'] ?>" data-input-class="input-<?= $product['_tool_type'] ?>-url-<?= $product['id'] ?>" href="javascript:void(0);" data-bs-toggle="tooltip" title="<?= __('user.slug_settings') ?>">
                                <i class="fas fa-cog fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-primary action-btn get-terms" data-id="<?= $product['id'] ?>" href="javascript:void(0);" data-bs-toggle="tooltip" title="<?= __('user.terms') ?>">
                                <i class="fas fa-external-link-alt fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-success action-btn get-code" data-id="<?= $product['id'] ?>" href="javascript:void(0);" data-bs-toggle="tooltip" title="<?= __('user.get_code') ?>">
                                <i class="fas fa-code fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-info action-btn get-downloads" href="javascript:void(0)" onclick="downloadCode(this, <?= $product['id'] ?>, 'tool')" data-bs-toggle="tooltip" title="<?= __('user.download_cam_pack') ?>">
                                <i class="fas fa-download fa-sm"></i>
                            </a>
                            <a class="btn btn-outline-secondary action-btn qrcode" href="javascript:void(0)" data-id="<?= $shareUrl ?>" data-bs-toggle="tooltip" title="<?= __('user.qrcode') ?>">
                                <i class="fas fa-qrcode fa-sm"></i>
                            </a>
                            <a href="javascript:void(0);" class="btn btn-outline-warning action-btn" data-social-share data-share-url="<?= $shareUrl ?>" data-share-title="<?= $product['name'];?>" data-bs-toggle="tooltip" title="<?= __('user.share_campaign') ?>">
                                <i class="fas fa-share-from-square fa-sm"></i>
                            </a>
                        </div>
                    </div>
                </div>

                <!-- Recurring Badge -->
                <?php if($product['recursion']){ ?>
                <div class="mt-2">
                    <?php
                    if($product['recursion'] != 'custom_time'){
                           echo '<span class="badge bg-secondary"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': ' . __('user.'.$product['recursion']).'</span>';
                    } else {
                           echo '<span class="badge bg-secondary"><i class="fas fa-sync-alt me-1"></i>'. __('user.recurring') .': '. timetostring($product['recursion_custom_time']).'</span>';
                       }
                    ?>
                </div>
                <?php } ?>
            </div>
            
            <!-- Collapsible Details -->
            <div class="collapse" id="toolCollapse<?= $index ?>">
                <div class="card-footer bg-light border-0">
                    <div class="row g-3">
                        <?php if($product['_tool_type'] == 'program' && $product['sale_status']){ ?>
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-success mb-1 fw-bold"><?php echo (int)$product['total_sale_count']; ?></h6>
                                <small class="text-muted"><strong><?= __('user.sale_count') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-success mb-1 fw-bold"><?php echo c_format((int)$product['total_sale_amount']); ?></h6>
                                <small class="text-muted"><strong><?= __('user.sale_amount') ?></strong></small>
                            </div>
                        </div>
                        <?php } if($product['_tool_type'] == 'program' && $product['click_status']){?>
                        <div class="col-md-3 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-info mb-1 fw-bold"><?php echo (int)$product['total_click_count'];?></h6>
                                <small class="text-muted"><strong><?= __('user.click_count') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-3 col-6">
                            <div class="bg-success bg-gradient text-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="mb-1 fw-bold"><?php echo (int)$product['total_click_amount'];?></h6>
                                <small><strong><?= __('user.click_amount') ?></strong></small>
                            </div>
                        </div>
                        <?php } if($product['_tool_type'] == 'general_click'){?>
                        <div class="col-md-6 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-info mb-1 fw-bold"><?php echo (int)$product['total_general_click_count'];?></h6>
                                <small class="text-muted"><strong><?= __('user.general_count') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-6 col-6">
                            <div class="bg-success bg-gradient text-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="mb-1 fw-bold"><?php echo $product['total_general_click_amount'];?></h6>
                                <small><strong><?= __('user.general_amount') ?></strong></small>
                            </div>
                        </div>
                        <?php } if($product['_tool_type'] == 'action' || $product['_tool_type'] == 'single_action'){?>
                        <div class="col-md-6 col-6">
                            <div class="bg-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="text-warning mb-1 fw-bold"><?= (int)$product['total_action_click_count'] ?></h6>
                                <small class="text-muted"><strong><?= __('user.action_count') ?></strong></small>
                            </div>
                        </div>
                        <div class="col-md-6 col-6">
                            <div class="bg-success bg-gradient text-white rounded-3 p-3 shadow-sm text-center">
                                <h6 class="mb-1 fw-bold"><?= $product['total_action_click_amount']; ?></h6>
                                <small><strong><?= __('user.action_amount') ?></strong></small>
                            </div>
                        </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
   <?php } ?>
<?php } ?>

<?php if(!empty($pagination)): ?>
<div class="d-flex justify-content-center mt-4">
    <nav aria-label="Page navigation">
        <?php echo $pagination?>
    </nav>
</div>
<?php endif; ?>
                    
<style>
/* Minimal custom CSS - only for z-index fixes */
.modal {
    z-index: 1050 !important;
}

.modal-backdrop {
    z-index: 1040 !important;
}
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Initialize tooltips  
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"], [title]'));
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });

    // Animate counters
    function animateCounters() {
        const counters = document.querySelectorAll('.counter');
        
        counters.forEach(counter => {
            const target = parseInt(counter.getAttribute('data-count'));
            const duration = 2000;
            const increment = target / (duration / 16);
            let current = 0;
            
            const timer = setInterval(() => {
                current += increment;
                if (current >= target) {
                    counter.textContent = target.toLocaleString();
                    clearInterval(timer);
                } else {
                    counter.textContent = Math.floor(current).toLocaleString();
                }
            }, 16);
        });
    }

    // Trigger counter animation when cards are visible
    const observerOptions = {
        threshold: 0.3,
        rootMargin: '0px 0px -50px 0px'
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                animateCounters();
                observer.unobserve(entry.target);
            }
        });
    }, observerOptions);

    // Observe all campaign cards
    document.querySelectorAll('.campaign-card').forEach(card => {
        observer.observe(card);
    });

    // Add stagger animation to cards
    const cards = document.querySelectorAll('.campaign-card');
    cards.forEach((card, index) => {
        card.style.animationDelay = `${index * 0.1}s`;
    });
});
</script>