<?php 
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

use Elex\MultiVendor\Model\Vendor;
use Elex\MultiVendor\Model\Coupon;
?>
	<!-- =====================main-content================== -->
	<div class="elex-multi-vendor-for-woocommerce-main p-2 pr-4 ">


			<!-- ===========side panel open button========= -->
			<div class="d-flex mb-3 gap-4 align-items-center elex-multi-vendor-for-woocommerce-side-panel-open-div d-md-none">
				<!-- side panel open button -->
				<button class=" btn p-1 shadow-none fs-5   bg-white text-primary position-relative " id="elex-multi-vendor-for-woocommerce-side-panel-open-btn"><svg height="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M0 96C0 78.3 14.3 64 32 64l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 128C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32l384 0c17.7 0 32 14.3 32 32s-14.3 32-32 32L32 288c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32L32 448c-17.7 0-32-14.3-32-32s14.3-32 32-32l384 0c17.7 0 32 14.3 32 32z"/></svg></button>
				<div class="fs-5 fw-semi-bold m-0 ">Coupons</div>
			</div>

			<!-- main content top link and search -->
			<div class="row justify-content-between mb-3">



				<div class="col-lg-8 col-md-8 col-sm-12">
					<div class="row ">
					<?php 
			
					if ( get_post_meta( $store_id, 'elex_mv_vendor_to_generate_coupons', true ) ) { 
						?>
						<div class="col-lg-7 mb-lg-0 mb-3">
							<div class="form-group d-flex gap-2 h-100 ">
								<select name="" id="" class="form-select">
									<option value="">Select</option> 
									<option value="delete">Delete</option> 
								</select>
								<button class="btn btn-primary px-4 elex-delete-coupon-bulk-action">Apply</button>
							</div>
						</div>
						<?php
					}
					?>
						<div class="col-lg-5 mb-lg-0 mb-3">
							<div class="input-group h-100 border border-1 rounded-3">
								<input type="text" value="<?php echo esc_attr( ! empty( get_query_var( 'search_term' ) ) ? get_query_var( 'search_term' ) : '' ); ?>" placeholder="Search Coupon" class="form-control border-0 p-2" id='elex-search-coupon-vendor'>
								<?php 
								if ( ! empty( get_query_var( 'search_term' ) ) ) {
									?>
								<button class="input-group-text  bg-transparent border-0" id='elex-search-coupon-vendor-button-cross' ><svg xmlns="http://www.w3.org/2000/svg" id="Outline" viewBox="0 0 24 24" width="16" height="16" style="stroke-width:1;stroke:#000"><path d="M23.707.293h0a1,1,0,0,0-1.414,0L12,10.586,1.707.293a1,1,0,0,0-1.414,0h0a1,1,0,0,0,0,1.414L10.586,12,.293,22.293a1,1,0,0,0,0,1.414h0a1,1,0,0,0,1.414,0L12,13.414,22.293,23.707a1,1,0,0,0,1.414,0h0a1,1,0,0,0,0-1.414L13.414,12,23.707,1.707A1,1,0,0,0,23.707.293Z"/></svg></button>
								<?php
								}
								?>
								
								<button class="input-group-text border-0" id='elex-search-coupon-vendor-button' onclick="searchCoupon()"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg></button>
							</div>
						</div>
					</div>

				</div>
				<?php if ( get_post_meta( $store_id, 'elex_mv_vendor_to_generate_coupons', true ) ) { ?>
				<div class="col-lg-3 col-md-4 col-sm-12 d-flex justify-content-end">
					<button class="btn btn-sm btn-primary elex-multi-vendor-for-woocommerce-generate-coupon-btn"><i class="fa-solid fa-plus me-1"></i>Generate Coupon</button>
				</div>
				<?php 
				}
				?>




			</div>



			<div class="d-flex flex-column align-items-between ">
				<!-- order list table -->
				<div class="table-responsive p-1">
				 
					<table class=" table table-borderless align-middle text-dark elex-multi-vendor-for-woocommerce-table  text-center">
						<thead class="table-secondary bg-opacity-10 rounded-2 ">
							<tr class=" ">
								<th scope="col " width="30px " class="align-middle"><input type="checkbox" class="form-check-input"></th>
								<th scope="col " class="text-start align-middle">Coupon Details</th>
								<th scope="col " class="align-middle">Coupon Type & Amount</th>
								<th scope="col " class="align-middle">Usage / Limit</th>
								<th scope="col " class="align-middle">Validity</th>
								<th scope="col " class="text-start align-middle">Actions</th>
							</tr>
						
						</thead>
				

						<?php
						if ( empty( $vendor_coupons ) ) {
							if ( ! get_post_meta( $store_id, 'elex_mv_vendor_to_generate_coupons', true ) ) { 
								echo '<tbody><tr><td colspan="6" ><span> No coupons available yet.</span></td></tr></tbody>';

							} else {
								echo '<tbody><tr><td colspan="6" ><span> No coupons available yet.</span></td></tr></tbody>';

							}                       
						}

						if ( ! empty( $vendor_coupons ) ) {
							foreach ( $vendor_coupons as $coupon_post ) {
								$coupon_data   = new \WC_Coupon( $coupon_post->ID );
								$author_id     = get_post_field( 'post_author', $coupon_post->ID );
								$coupon_model  = new Coupon();
								$store_id      = $coupon_data->get_meta( 'elex_mv_store_id' );
								$coupon_action = get_post_meta( $store_id, 'elex_mv_vendor_to_generate_coupons', true );
							   
								$valid_from        = ! empty( $coupon_data->get_meta( 'elex_mv_validity_from' ) ) ? $coupon_model->coupon_date( $coupon_data->get_meta( 'elex_mv_validity_from' ) ) : '';
								$valid_till        = ! empty( $coupon_data->get_date_expires() ) ? $coupon_model->coupon_date( $coupon_data->get_date_expires()->date( 'Y-m-d' ) ) : '';
								$coupon_created_at = ! empty( $coupon_data->get_date_created() ) ? $coupon_model->coupon_date( $coupon_data->get_date_created() ) : '';
								$discount_type     = $coupon_model->coupon_discount_type( $coupon_data->get_discount_type() );
								$date_fomat        = get_option( 'date_format' );
							
								?>
										<tbody>
						<tr class="elex-multi-vendor-for-woocommerce-table-box-shadow rounded-3 "> 

								<th class="align-middle "><input type="checkbox" id=<?php echo esc_attr( $coupon_post->ID ); ?> class="form-check-input elex-delete-bulk-chk"></th>

								<td class="text-start">
									<div class="fs-6 mb-1 fw-bold "><?php echo esc_attr( $coupon_post->post_title ); ?></div>
									<div class="text-secondary "><small><?php echo esc_attr( $coupon_data->get_meta( 'elex_mv_coupon_title' ) ); ?></small></div>
									<div class=" "><small>By:<?php echo esc_attr( get_the_author_meta( 'display_name' , $coupon_post->post_author ) ); ?></small></div>
									<div class=""><small><?php echo esc_attr( gmdate( $date_fomat, strtotime( $coupon_created_at ) ) ); ?></small></div>
								</td>

								<td class="text-center">	
									<div class="rounded-3 bg-info bg-opacity-10"><small class="text-nowrap"><?php echo esc_attr( $discount_type ); ?></small></div>
									<div class=""><small><?php echo esc_attr( $coupon_data->get_amount() ); ?></small></div>
								</td>
								<td class="text-center">
									<div><small><?php echo ! empty( $coupon_data->get_usage_count() ) ? esc_attr( $coupon_data->get_usage_count() ) : 0 ; ?>/<?php echo ! empty( $coupon_data->get_usage_limit() ) ? esc_attr( $coupon_data->get_usage_limit() ) : '&infin;'; ?> </small></div>
								</td>
								<td class="text-center">
									<div><small><?php echo ! empty( $coupon_data->get_meta( 'elex_mv_validity_from' ) ) ? esc_attr( gmdate( $date_fomat, strtotime( $valid_from ) ) ) : esc_attr( gmdate( $date_fomat, strtotime( $coupon_created_at ) ) ); ?>/<?php echo ! empty( $valid_till ) ? esc_attr( gmdate( $date_fomat, strtotime( $valid_till ) ) ) : '-'; ?></small></div>
								</td>
								<?php 
								if ( (string) get_current_user_id() === (string) $author_id && $coupon_action ) {
									?>
								<td class="text-start">
									<button class="btn btn-sm btn-outline-primary border-0 d-flex gap-1 align-items-center elex-multi-vendor-for-woocommerce-coupon-open-edit-btn" data-coupon-id = <?php echo esc_attr( $coupon_post->ID ); ?> onclick="editFormData(<?php echo esc_attr( $coupon_post->ID ); ?>)"><i class="fa-regular fa-pen-to-square me-1"></i>Edit</button>
									<button class="btn btn-sm btn-outline-primary border-0 d-flex gap-1 align-items-center elex-multi-vendor-for-woocommerce-coupon-open-delete-btn" data-coupon-id = <?php echo esc_attr( $coupon_post->ID ); ?> onclick="deleteCouponFunc(<?php echo esc_attr( $coupon_post->ID ); ?>)"><i class="fa-regular fa-trash-can me-1"></i>Delete</button>
								</td>
								<?php
								}
								?>

						<?php
							}                       
						}
					
					
						?>
													</tr>

						</tbody>

					</table>
									</div>



				<!-- pagination -->
				<!-- previous & Next -->
			<div class="position-sticky  bottom-0 left-0">
				<div class="w-100 d-flex justify-content-center gap-3 ">
					<?php
				// Previous page
					if ( $current_page > 1 ) {
						echo '<a href="' . esc_url( add_query_arg( array( 'page_coupon' => $current_page - 1 ) ) ) . '" class="btn"><i class="fa-solid fa-chevron-left me-1"></i>Previous</a>';
					}
				// Next page
				
					if ( $current_page < $num_pages ) {

						echo '<a href="' . esc_url( add_query_arg( array( 'page_coupon' => $current_page + 1 ) ) ) . '"  class="btn ">Next<i class="fa-solid fa-chevron-right ms-1"></i></a>';
					}
					?>
				</div>
			</div>
			</div>
		</div>

				<!-- Delete coupon message popup -->
		<div  style="z-index:99;" class="toast p-3 position-fixed top-0 start-50 translate-middle-x bg-white mt-3" id="elex_delete_coupon" style="min-width: 450px;">
			<div class="toast-header border-0 border-bottom p-0 pb-2">
				<strong class="mx-auto">
					<svg xmlns="http://www.w3.org/2000/svg" width="18" height="20" viewBox="0 0 20 22" class="me-2">
						<g id="Icon_feather-trash-2" data-name="Icon feather-trash-2" transform="translate(-3.5 -2)">
							<path id="Path_648" data-name="Path 648" d="M4.5,9h18" transform="translate(0 -2)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_649" data-name="Path 649" d="M21.5,7V21a2,2,0,0,1-2,2H9.5a2,2,0,0,1-2-2V7m3,0V5a2,2,0,0,1,2-2h4a2,2,0,0,1,2,2V7" transform="translate(-1 0)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_650" data-name="Path 650" d="M15,16.5v6" transform="translate(-3.5 -4.5)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
							<path id="Path_651" data-name="Path 651" d="M21,16.5v6" transform="translate(-5.5 -4.5)" fill="none" stroke="#dc3545" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/>
						</g>
						</svg>
						Confirm Delete</strong>
				<button type="button" class="btn-close" data-bs-dismiss="toast"></button>
			</div>
			<div class="toast-body p-0">
				<div class="fs-6 my-2">Are you sure, you want to delete item?</div>

				<div class="d-flex gap-3 justify-content-center ">
					<button class="btn px-4 btn-outline-secondary" data-bs-dismiss="toast">Cancel</button>
					<button class="btn px-4 btn-primary" id="elex-multi-vendor-for-woocommerce-delete-coupon-confirm-btn" onclick="confirmDelete()">
						Confirm
					</button>
				</div>
				</div>
			 
		</div>
