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

use Elex\MultiVendor\Model\AdminPayment;
use Elex\MultiVendor\Model\SiteSettings;
?>
<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>

<body>

	<div class="elex-multi-vendor-for-woocommerce-wrap elex-multi-vendor-for-woocommerce-wrap-admin overflow-hidden d-flex position-relative">
		<!-- ===================side panel============ -->
		<!-- side panel open button -->
		<!-- <button class="btn btn-outline-primary bg-white text-primary rounded-circle position-absolute shadow mt-3 ms-3" id="elex-multi-vendor-for-woocommerce-side-panel-open-btn"><i class="fa-solid fa-bars"></i></button> -->

		<!--================end of side panel============= -->


		<!-- main-content -->
		<div class="elex-multi-vendor-for-woocommerce-main p-2 flex-1 me-4 position-relative d-flex flex-column">

		<img class="w-100 mb-2" src="<?php echo esc_url( ELEX_MULTI_VENDOR_MAIN_URL_PATH . 'images/top-banner.png' ); ?>" alt="">

			<!-- main content top link and search -->
			<div class="d-flex justify-content-between align-items-center mb-3">
					<h5 class="mb-0 ">Payments</h5>
				
			</div>


			<!-- main content top 2nd link section -->
			<div class="row justify-content-between">
				<div class="col-xl-9 col-lg-8 col-md-7 overflow-auto mb-2 elex-multi-vendor-for-woocommerce-product-filters">
					<div class="d-flex gap-2 flex-wrap p-md-0 px-2 py-4 elex-table-links-btn">
						<a href="<?php echo esc_url( add_query_arg( array( 'filter' => 'all' ) ) ); ?>" class="btn border border-secondary bg-secondary bg-opacity-10 text-secondary elex-multi-vendor-for-woocommerce-all-vendors text-nowrap 
						<?php 
						if ( empty( $GET_filter ) || 'all' === $GET_filter ) {
							echo esc_attr( 'active' ); }  
						?>
							" >All</a>
						<a href="<?php echo esc_url( add_query_arg( array( 'filter' => 'unsettled' ) ) ); ?>" class="btn border border-secondary bg-secondary bg-opacity-10 text-secondary elex-multi-vendor-for-woocommerce-unsettled-vendors text-nowrap
						<?php 
						if ( ! empty( $GET_filter ) && 'unsettled' === $GET_filter ) {
							echo esc_attr( 'active' ); }  
						?>
							" >Unsettled</a>
						<a href="<?php echo esc_url( add_query_arg( array( 'filter' => 'overdue' ) ) ); ?>" class="btn border border-secondary bg-secondary bg-opacity-10 text-secondary elex-multi-vendor-for-woocommerce-overdue-vendors text-nowrap
						<?php 
						if ( ! empty( $GET_filter ) && 'overdue' === $GET_filter ) {
							echo esc_attr( 'active' ); }  
						?>
							" >Overdue</a>
					</div>
				</div>
				<div class="col-xl-3 col-lg-4 col-md-5  mb-3 d-flex gap-2">
					<div class="input-group flex-nowrap h-100 border border-1 rounded-3">
						<select type="text"   value="<?php echo esc_attr( $GET_search_term ); ?>" placeholder="Search Vendor" class="form-control border-0 elex_mv_search_vendor_name p-2" id="elex_mv_search_admin_payments_vendor_name"></select>
						<?php
						if ( ! empty( $GET_search_term ) ) {
							?>
								<button class="input-group-text bg-transparent border-0" id="elex_mv_search_vendor_payment_cross_btn"><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 elex_mv_search_vendor_payment border-0" id="elex_mv_search_vendor_payment_btn" ><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>
					<button class="btn btn-outline-primary elex-multi-vendor-for-woocommerce-export-popup-open-btn">Export</button>
				</div>
			</div>


			<!-- product list table -->
			<div class="flex-fill overflow-auto">
				<div class="elex-multi-vendor-for-woocommerce-payment p-1">
					<div class="bg-light p-2 mb-3">
						<div class="row  ">
							<div style="width: 30px;"  ><input class="" type="checkbox" value="" ></div>
							
							<div class="col-2">Vendor</div>
							<div class="col-2">Due For Settlement(<?php echo esc_html( get_woocommerce_currency() ); ?>)</div>
							<div class="col-2">Settlement Status</div>
							<div class="col-2">Next Settlement Date</div>
							<div class="col">Actions</div>
						</div>
					</div>
					
					<?php
				   
					foreach ( $vendors as  $user_data ) {
						$settlement_status = AdminPayment::get_settlement_status( $user_data->ID );
						$vendor_earning    = AdminPayment::get_vendor_earning( $user_data->ID );
						$admin_earning     = AdminPayment::get_admin_earning( $user_data->ID );
						$settlement_date   = AdminPayment::get_settlement_date( $user_data->ID );
						$commission_amount = AdminPayment::get_commission_amount();
						$commission_type   = AdminPayment::get_commission_type();
						//$settlement_date = gmdate( 'd-m-y', strtotime( $settlement_date ) );
						$display_name = get_the_author_meta( 'display_name' , $user_data->post_author );

					

						?>
					<div class="shadow-sm mb-3 rounded-3 overflow-hidden">
						<div class="bg-white p-2">
							<div class="row align-items-center ">
							<div style="width: 30px;"  ><input class="" type="checkbox" value="" ></div>
							
							<div class="col-2">
								<a href="<?php echo esc_url( get_edit_user_link( $user_data->post_author ) ); ?>"><?php echo esc_html( $user_data->post_title ); ?></a>
								<div class="text-secondary"><small><?php echo esc_attr( $user_data->user_email ); ?></small></div>
							</div>
							<div class="col-2"><?php echo isset( $vendor_earning ) ? wp_kses_post( wc_price( $vendor_earning ) ) : 0 ; ?></div>
							<!-- <div class="col-2"><?php echo ! empty( $commission ) ? wp_kses_post( wc_price( $commission ) ) : 0 ; ?></div> -->
							<div class="col-2">
								<?php 
							
								if ( 'Overdue' === (string) $settlement_status ) { 
									?>
									<div class="btn btn-sm pe-none bg-red px-4 text-black">Overdue</div>
								<?php
								} else {
									?>
									<div class="btn btn-sm bg-orange pe-none px-4 text-black">Unsettled</div>
									<?php  
								}
								?>
								
								
							</div>
							<div class="col-2">
							<?php 
							$date_fomat = get_option( 'date_format' );
							echo esc_html( gmdate( $date_fomat , strtotime( sanitize_text_field( $settlement_date ) ) ) ); 
							?>
							</div>
							<?php
							if ( 0 >= $vendor_earning ) { 
								?>
							<div class="col">
								<button class="btn btn-outline-primary elex-multi-vendor-for-woocommerce-payment-popup-open-btn " data-attr-id="<?php echo esc_attr( $user_data->ID ); ?>" data-attr-vendor="<?php echo esc_attr( $display_name ); ?>" data-attr-commission="<?php echo esc_attr( $admin_earning ); ?>" data-commision-type = "<?php echo esc_attr( $commission_type ); ?>" data-commision-amount ="<?php echo esc_attr( $commission_amount ); ?>" data-attr-amount =" <?php echo esc_attr( $vendor_earning ); ?>" disabled>Mark as Settled</button>
							</div>
							<?php } else { ?>
							<div class="col">
								<button class="btn btn-outline-primary elex-multi-vendor-for-woocommerce-payment-popup-open-btn " data-attr-id="<?php echo esc_attr( $user_data->ID ); ?>" data-attr-vendor="<?php echo esc_attr( $display_name ); ?>" data-attr-commission="<?php echo esc_attr( $admin_earning ); ?>" data-commision-type = "<?php echo esc_attr( $commission_type ); ?>" data-commision-amount ="<?php echo esc_attr( $commission_amount ); ?>" data-attr-amount =" <?php echo esc_attr( $vendor_earning ); ?>">Mark as Settled</button>
							</div>
							<?php } ?>
						</div>
						</div>
						
						<div class="bg-white elex-multi-vendor-for-woocommerce-settlement">
							<div class="bg-light">
								<button class="btn btn-sm px-4 elex-multi-vendor-for-woocommerce-settlement-show-btn"  data-attr-id= <?php echo esc_attr( $user_data->ID ); ?>>Show Settlement History <i class="fa-solid fa-chevron-down"></i></button>
								<button class="btn btn-sm d-none elex-multi-vendor-for-woocommerce-settlement-hide-btn">Hide Settlement History <i class="fa-solid fa-chevron-up"></i></button>
							</div>
							
							<div class="px-3 d-none elex-multi-vendor-for-woocommerce-settlement-list">
								<table class="table  align-middle text-dark bg-white w-100 elex-multi-vendor-for-woocommerce-settlement-table">
									<thead class=" bg-info bg-opacity-10 rounded-2">
										<tr class="">
											<th scope="col">Settlement Date</th>
											<th scope="col">Settlement Mode</th>
											<th scope="col">Transaction Id</th>
											<th scope="col">Vendor Amount</th>
											<th scope="col">Note</th>
										</tr>
									</thead>
								</table>
							</div>
						</div>
					</div>
					<?php
						
					}
					?>
				   
				</div>


				<!-- 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_num' => $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_num' => $current_page + 1 ) ) ) . '"  class="btn ">Next<i class="fa-solid fa-chevron-right ms-1"></i></a>';
					}
					?>
				</div>
			</div>
			</div>
			


		   
			


		</div>




	   
		<?php require ELEX_MULTI_VENDOR_PLUGIN_PATH . 'templates/admin-payments/admin-settlement-form.php'; ?>

		<!-- export popup -->
		<div class="bg-dark bg-opacity-25 elex-multi-vendor-for-woocommerce-popup row justify-content-end w-100" id="elex-multi-vendor-for-woocommerce-export-popup">
			<div class="col-xl-4 col-lg-6 col-md-9 bg-white h-100 ms-auto overflow-auto">
				<div class="py-2 px-4 mt-5 text-center position-relative">
					<h5>Export</h5>
					<button class="btn rounded-circle position-absolute top-0 end-0 elex-multi-vendor-for-woocommerce-export-popup-close-btn">
						<i class="fa-solid fa-xmark"></i>
					</button>
				</div>
				<div class="px-5 py-3">
					<div class="form-group mb-3">
					<form id="elex-multi-vendor-for-woocommerce-export-vendor-settlement-csv-form" action="admin-ajax.php" method="post">
					<input type="hidden" name="action" value="elex_mv_export_settlement_vendor"></input>
					<?php wp_nonce_field( 'elex_mv_export_settlement_vendor', 'elex_mv_export_settlement_vendor_nonce' ); ?>
						<label for="">Vendor(s)</label>
						<select class="js-example-basic-multiple vendors_settlement_csv" name="vendors_settlement_csv[]" multiple="true" style="width: 100%"></select>
					</div>
					<!-- <div class="form-group mb-3">
						<label>File Format</label>
						<input type="text" value="CSV" name="elex-multi-vendor-for-woocommerce-vendor_file_format_csv" class="form-control elex-multi-vendor-for-woocommerce-vendor_file_format_csv"/>
					</div> -->
					<div class="form-group mb-3">
						<label>Date from</label>
						<input type="date" class="form-control" name="export_from_date" value="<?php echo esc_attr( gmdate( 'Y-m-d', strtotime( '-29 days' ) ) ); ?>">
					</div>
					<div class="form-group mb-4">
						<label>Date To</label>
						<input type="date" class="form-control" name="export_to_date" value="<?php echo esc_attr( gmdate( 'Y-m-d' ) ); ?>">
					</div>
					<button class="btn btn-primary elex-multi-vendor-for-woocommerce-vendor-export-csv w-100">Export as CSV</button>
					</form>
				</div>



			</div>
		</div>	
	</div>


</body>

</html>
