<?php
if (
	!isset($permissions['suppliers']['view'])
	|| isset($permissions['suppliers']['view'])
	&& $permissions['suppliers']['view'] == 0
) redirect('admin/dashboard');
?>
<div id="main" role="main">
	<div id="content" style=" padding-top: 0px; ">
		<div class="mx-auto w-50 row mt-3">
			<div class="col-lg-6">
				<h2><?php echo $this->lang->line('update_supplier'); ?></h2>
			</div>
			<div class="col-lg-6 text-align-right">
				<div class="btn-group">
					<a class="btn btn-success" href="<?php print site_url() ?>admin/suppliers/suppliersPrice/<?php echo $supplier->supplier_id; ?>" target="_blank">
						<?php echo $this->lang->line('card_supplier'); ?>
					</a>
				</div>
			</div>
			<div class="col-lg-12">
				<hr />
			</div>
			<div class="col-lg-6 col-md-12 col-sm-12">
				<div class="card card-body">
					<form id="addSupplier-form" novalidate="novalidate" method="post" action="<?php print site_url(); ?>admin/suppliers/EditSupplier">
						<input type="hidden" name="supplier_id" id="supplier_id" value="<?php echo $supplier->supplier_id; ?>" />
						<fieldset>
							<div class="row">
								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('name'); ?></label>
									<input type="text" name="name" value="<?php echo $supplier->name; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('domain'); ?></label>
									<select class="select2" name="domain" class="form-select">
										<?php foreach ($suppliers_tags as $tag) { ?>
											<option <?php if ($supplier->domain == $tag->name) {
														echo 'selected="selected"';
													}; ?> value="<?php echo $tag->name; ?>"><?php echo $tag->name; ?></option>
										<?php }; ?>
									</select>
									</label>
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('numper_p'); ?></label>
									<input type="text" name="vat_number" value="<?php echo $supplier->vat_number; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('email'); ?></label>
									<input type="text" name="email" value="<?php echo $supplier->email; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('telephone'); ?></label>
									<input type="text" name="phone_number" value="<?php echo $supplier->phone_number; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('address'); ?></label>
									<input type="text" name="address" value="<?php echo $supplier->address; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('contact_person'); ?></label>
									<input type="text" name="main_contact" value="<?php echo $supplier->main_contact; ?>" class="form-control" />
								</div>

								<div class="form-group col-lg-6">
									<label><?php echo $this->lang->line('systemic_name'); ?></label>
									<input type="text" readonly name="program_name" value="<?php echo $supplier->program_name; ?>" class="form-control" />
								</div>


								<div class="form-group col-lg-12 mt-2">
									<button type="submit" id="add_supplier_btn" class="btn w-100 btn-success">
										<?php echo $this->lang->line('save'); ?>
									</button>
								</div>
							</div>
						</fieldset>
					</form>
				</div>
			</div>
			<div>
				<div class="card card-body">
					<h1>
					<?php echo $this->lang->line('Automatic_invoice'); ?>
					</h1>
					<div>
						<button class="delete-btn btn btn-danger col col-2 float-start" id="delete-btn"><i class="fa fa-trash"></i></button>
						<button type="button" class="btn btn-info col col-2 float-start" data-bs-toggle="modal" data-bs-target="#newModal"><i class="fa fa-edit"></i></button>
					</div>
					<table class="table table-bordered table-hover smart-form">
						<thead>
							<tr>
								<th></th>
								<th><?php echo $this->lang->line('days'); ?></th>
								<th><?php echo $this->lang->line('products'); ?></th>
								<th><?php echo $this->lang->line('total'); ?></th>
							</tr>
						</thead>
						<tbody id="cronJob_body">
							<?php foreach ($cron_job as $cron) { ?>
								<tr rel-id="<?php echo $cron->id; ?>">
									<td><?php echo $cron->id; ?></td>
									<td><?php echo $cron->days; ?></td>
									<td><?php echo getItemNameById($cron->product_id); ?></td>
									<td><?php echo $cron->total; ?></td>
								</tr>
							<?php }; ?>
						</tbody>
					</table>
				</div>
				<div class="card card-body mt-3">
					<h1> <?php echo $this->lang->line('Automatic_invoice'); ?>
					</h1>
					<form action="<?php echo site_url(); ?>admin/invoices/supplierInvoiceAuto/<?php echo $supplier->supplier_id; ?>" method="POST" id="createInvoiceForm" autocomplete="OFF">
						<div id="invoice_table">
							<table id="invoice_table">
								<thead>
									<tr>
										<th><?php echo $this->lang->line('days'); ?></th>
										<th><?php echo $this->lang->line('products'); ?></t>
										<th><?php echo $this->lang->line('total'); ?></th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td class="w-50"><select class="cron_days" name="days[]" multiple="multiple" value="<?php echo $supplier->cron_days; ?>" id="days" required="required">
												<option value="1">1</option>
												<option value="2">2</option>
												<option value="3">3</option>
												<option value="4">4</option>
												<option value="5">5</option>
												<option value="6">6</option>
												<option value="7">7</option>
												<option value="8">8</option>
												<option value="9">9</option>
												<option value="10">10</option>
												<option value="11">11</option>
												<option value="12">12</option>
												<option value="13">13</option>
												<option value="14">14</option>
												<option value="15">15</option>
												<option value="16">16</option>
												<option value="17">17</option>
												<option value="18">10</option>
												<option value="10">19</option>
												<option value="20">20</option>
												<option value="21">21</option>
												<option value="22">22</option>
												<option value="23">23</option>
												<option value="24">24</option>
												<option value="25">25</option>
												<option value="26">26</option>
												<option value="27">27</option>
												<option value="28">28</option>
												<option value="29">29</option>
												<option value="30">30</option>
											</select></td>
										<td class="w-25">
											<select class="product_id form-control" id="product_id" name="product_id" required="required">
												<optgroup>
													<?php foreach ($products as $prod) { ?>
														<option value="<?php echo $prod->id; ?>"><?php echo $prod->name; ?></option>
													<?php }; ?>
												</optgroup>
											</select>
										</td>
										<td><input class="invoice_total form-control w-20 text-center fw-bold text-primary" name="invoice_total" id="invoice_total" required="required">
										</td>
									</tr>
								</tbody>
							</table>
						</div>
						<button type="submit" id="save_invoice" class="btn btn-block w-60 btn-success mt-lg-4">
							<?php echo $this->lang->line('save'); ?>
						</button>

					</form>
				</div>

			</div>
		</div>
	</div>
</div>
<div class="modal" id="newModal" role="dialog" title="edit invoice">
	<div class="modal-dialog modal-xl">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-bs-dismiss="modal">&times;</button>
				<h4 class="modal-title"><?php echo $this->lang->line('edit_invoice'); ?></h4>
			</div>
			<div class="modal-body">
				<div class="row">
					<div class="col-lg-12">
						<form action="<?php echo site_url(); ?>admin/suppliers/updateInvoice/<?php echo $supplier->supplier_id ?>" method="POST" id="updateRow" autocomplete="OFF">
							<div class="row">
								<div class="col col-lg-12 col-md-12 col-sm-12">
									<h2><?php echo $this->lang->line('edit_invoice'); ?></h2>
								</div>
								<div class="col col-lg-12 col-md-12 col-sm-12" id="invoice_details">
									<table id="updateTable" class="display">
										<thead>
											<th></th>
												<th><?php echo $this->lang->line('days'); ?></th>
												<th><?php echo $this->lang->line('products'); ?></t>
												<th><?php echo $this->lang->line('total'); ?></th>
										</thead>
										<tbody>
											<tr class="edit">
												<td><input id="id" name="id" rel-id="<?php echo $cron->id; ?>" value="<?php echo $cron->id; ?>" readonly> </td>
												<td class="col col-3"><select class="cron" name="days[]" multiple="multiple" value="<?php echo $supplier->cron_days; ?>" id="cron" required="required">
														<option value="1">1</option>
														<option value="2">2</option>
														<option value="3">3</option>
														<option value="4">4</option>
														<option value="5">5</option>
														<option value="6">6</option>
														<option value="7">7</option>
														<option value="8">8</option>
														<option value="9">9</option>
														<option value="10">10</option>
														<option value="11">11</option>
														<option value="12">12</option>
														<option value="13">13</option>
														<option value="14">14</option>
														<option value="15">15</option>
														<option value="16">16</option>
														<option value="17">17</option>
														<option value="18">10</option>
														<option value="10">19</option>
														<option value="20">20</option>
														<option value="21">21</option>
														<option value="22">22</option>
														<option value="23">23</option>
														<option value="24">24</option>
														<option value="25">25</option>
														<option value="26">26</option>
														<option value="27">27</option>
														<option value="28">28</option>
														<option value="29">29</option>
														<option value="30">30</option>
													</select></td>
												<td class="col col-3">
													<select class="product_id form-control" id="product_id" name="product_id" required="required">
														<optgroup>
															<?php foreach ($products as $prod) { ?>
																<option value="<?php echo $prod->id; ?>"><?php echo $prod->name; ?></option>
															<?php }; ?>
														</optgroup>
													</select>
												</td>
												<td class="col col-3">
													<input class="total form-control w-20 text-center fw-bold text-primary" value="<?php echo $cron->total; ?>" name="total" id="total" required="required">
												</td>
											</tr>
										</tbody>
									</table>
								</div>
								<div class="col col-lg-12 col-lg-12 col-sm-12">
									<div class="row">
										<div class="col-lg-5"></div>
										<div class="col-lg-5"></div>
										<div class="col-lg-2">
											<button type="button" class="btn btn-block btn-success mt-5" id="save_update">
												<?php echo $this->lang->line('confirm'); ?>
											</button>
										</div>
									</div>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-bs-dismiss="modal"><?php echo $this->lang->line('close'); ?></button>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
<?php
            $url = '';
            if( $this->session->userdata('site_lang') == 'hebrew' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
            }else if( $this->session->userdata('site_lang') == 'arabic' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Arabic.json';
            }else if( $this->session->userdata('site_lang') == 'english' ){
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/English.json';
            }else{
                $url = '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Hebrew.json';
            }
        ?>
	$(document).ready(function() {

		$(document).ready(function() {
			$(".cron_days").select2({
				theme: "bootstrap-5"
			});
		});

		let itemAddition = 0;
	});
</script>
<script type="text/javascript">
	$(document).ready(function() {

		$(document).ready(function() {
			$(".cron").select2({
				theme: "bootstrap-5"
			});
		});

		let itemAddition = 0;
	});
</script>
<script type="text/javascript">
	$('#save_update').on('click', function() {
		$('#updateRow').submit();
	});
	$(document).ready(function() {
		$(document).on('change', '#cron_toggle', function() {
			if (this.checked) {
				$('.cron-container').show();
			} else {
				$('.cron-container').hide();
			}
		});
	});

	$(document).on('click', '.add_item_view', function() {
		var $row = $('#invoice_table tbody tr:first').clone();
		$row.find('input').val("");
		$row.find('select.cron_days').attr("name", "days[]");
		$row.find('input.invoice_total').attr("name", "invoice_total");
		$('#invoice_table tbody').append($row);
	});
	$('#save_invoice').on('click', function() {
		$('#createInvoiceForm').submit();
	});
	let checkArray = [];
	let checkFlag = false;
	let inputArray = $('#createInvoiceForm input');

	$.each(inputArray, function(index, el) {
		if (el.value > 1) {
			el.style.border = '2px solid green';
			checkArray.push(el.value);

		} else {
			checkArray.push(false);
			el.style.border = '2px solid red';
		}
	});

	console.log(checkArray);
	if ($.inArray(false, checkArray) !== -1) {
		console.log('you forget an input');
	}
	//let checkArray = [];
	//let checkFlag = false;
	//let inputArray = $('#createInvoiceForm select');

	$.each(inputArray, function(index, el) {
		if (el.value > 1) {
			el.style.border = '2px solid green';
			checkArray.push(el.value);

		} else {
			checkArray.push(false);
			el.style.border = '2px solid red';
		}
	});

	console.log(checkArray);
	if ($.inArray(false, checkArray) !== -1) {
		console.log('you forget an input');
	}
	$(document).ready(function() {
		var oTbl1 = $('#dt_basic').DataTable({
			"sDom": "<'dt-toolbar'<'col-xs-12 col-sm-8'f><'dt-buttons col-sm-4 col-xs-12 hidden-xs'l>r>" +
				"t" +
				"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>",
			"buttons": ['copyHtml5', 'excelHtml5', 'csvHtml5', 'colvis'],
			"processing": true,
			"bStateSave": true,
			"dom": 'Blfrtip',
			"pagingType": "full_numbers",
			"lengthMenu": [
				[10, 25, 50, -1],
				[10, 25, 50, "<?php echo $this->lang->line('Everything'); ?>"]
			],
			"pageLength": 20,
			"sPaginationType": "full_numbers",
			"iDisplayLength": 10,
			"language": {
                    "url": "<?php echo $url; ?>"
                },
			orderCellsTop: true,
			fixedHeader: true
		});
		oTbl1.buttons().container().appendTo('.dataTables_filter');
	});

	$(document).on("click", "#cronJob_body tr", function(event) {
		$('tr').removeClass('active');
		$(this).addClass('active');
		let elem = $(this);
		let rowAttr = elem.attr('rel-id');
		$('#delete-btn').attr('rel-id', rowAttr);
	});
</script>
<style>
	tr.active {
		background: #00cfd5
	}

	;
</style>