</body>
<!--   Core JS Files   -->
<script src="<?php echo base_url() ?>assets/js/core/jquery.min.js" type="text/javascript"></script>
<!-- Control Center for Argon UI Kit: parallax effects, scripts for the example pages etc -->
<!--   Core JS Files   -->
<script src="<?php echo base_url() ?>assets/js/core/jquery.min.js" type="text/javascript"></script>
<script src="<?php echo base_url() ?>assets/js/core/popper.min.js" type="text/javascript"></script>
<script src="<?php echo base_url() ?>assets/js/core/bootstrap.min.js" type="text/javascript"></script>
<!-- Control Center for Argon UI Kit: parallax effects, scripts for the example pages etc -->
<script src="<?php echo base_url() ?>assets/js/argon-design-system.min.js?v=1.2.2" type="text/javascript"></script>
<!--   Core JS Files   -->
<script src="<?php echo base_url()?>assetsadmin/vendor/jquery/dist/jquery.min.js"></script>
<!-- <script src="<?php echo base_url()?>assetsadmin/vendor/bootstrap/dist/js/bootstrap.bundle.min.js"></script> -->
<script src="<?php echo base_url()?>assetsadmin/vendor/js-cookie/js.cookie.js"></script>
<script src="<?php echo base_url()?>assetsadmin/vendor/jquery.scrollbar/jquery.scrollbar.min.js"></script>
<script src="<?php echo base_url()?>assetsadmin/vendor/jquery-scroll-lock/dist/jquery-scrollLock.min.js"></script>
<!-- Optional JS -->
<script src="<?php echo base_url()?>assetsadmin/vendor/chart.js/dist/Chart.min.js"></script>
<script src="<?php echo base_url()?>assetsadmin/vendor/chart.js/dist/Chart.extension.js"></script>
<script src="<?php echo base_url()?>assetsadmin/vendor/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js">
</script>
<!-- Argon JS -->
<script src="<?php echo base_url()?>assetsadmin/js/argon.js?v=1.2.0"></script>
<!-- Control Center for Argon UI Kit: parallax effects, scripts for the example pages etc -->
<!--  Google Maps Plugin    -->
<script src="https://cdn.trackjs.com/agent/v3/latest/t.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap4.min.js"></script>

<script>
	$(document).ready(function () {
		$('#datatable_card').DataTable();
		$('#datatable_reward').DataTable();
	});

</script>
<script>
	$(document).ready(function () {
		$('#datatable_users').DataTable({
			"processing": true,
			"serverSide": true,
			"ajax": "<?php echo base_url('/Admin/Server_processing') ?>"
		});
	});

</script>
<script>
	$(document).on('submit', '#addcate_form', function () {
		$.post("<?=base_url('Admin/Category/submit')?>", $("#addcate_form").serialize(),
			function (data) {
				d = JSON.parse(data)
				if (d.status == 1) {
					Swal.fire({
						icon: "success",
						text: "เพิ่มหมวดหมู่ลงระบบเรียบร้อย",
					})
					setTimeout(function () {
						location.href = '<?=base_url("Admin/Category")?>'
					}, 2000);
				} else if (d.status == 0) {

					Swal.fire({
						icon: "error",
						text: "หมวดหมู่นี้มีในระบบแล้ว",

					});
				}
			}
		);

		event.preventDefault();
	});

</script>
<script>
	function Editcategory(id) {
		Swal.fire({
			title: 'กรุณากรอกชื่อหมวดหมู่ที่ต้องการแก้ไข',
			input: 'text',
			inputAttributes: {
				autocapitalize: 'off'
			},
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (text) => {

				$.post("<?=base_url('Admin/Category/edit')?>", {
						id: id,
						text: text
					},
					function (data) {
						console.log(data)
						d = JSON.parse(data)
						var test = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "แก้ไขหมวดหมู่เรียบร้อย",
							})
							setTimeout(function () {
								location.href = '<?=base_url("Admin/Category")?>'
							}, 1000);
						}

					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}

</script>
<script>
	function Deletecategory(id) {
		Swal.fire({
			title: 'คุณต้องการลบหมวดหมู่นี้ ?',
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (login) => {
				$.post("<?=base_url('Admin/Category/delete/')?>" + id,
					function (data) {
						d = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "ลบรายการนี้เรียบร้อย",
							})
							setTimeout(function () {
								location.reload(true);
							}, 1000);
						}else{
              Swal.fire({
								icon: "error",
								text: "ไม่สามารถลบหมวดหมู่นี้ได้เนื่องจากมีสินค้าในหมวดหมู่",
							})
            }
					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}

</script>
<script>
	$(document).on('click', '#submit', function () {
		var fd = new FormData();    
		var files = $('#file')[0].files;
		fd.append('image',files[0]);
		fd.append( 'cate', $('#cate').val());
		fd.append( 'namepro', $('#namepro').val());
		fd.append( 'price', $('#price').val());
		$.ajax({
			type: "post",
			url: "<?=base_url('Admin/Product/submit')?>",
			data: fd,
			processData: false,
			contentType: false,
			success: function (response) {
				d = JSON.parse(response)
				if(d.status == 1)
				{
					Swal.fire({
						icon: "success",
						text: "เพิ่มสินค้าเสร็จสิ้น",
					})
					setTimeout(function () {
								location.reload(true);
							}, 1000);
				}else{
					Swal.fire({
						icon: "error",
						text: "มีสินค้านี้ในระบบแล้ว",
					})
				}
			}
		});
		return false;
		event.preventDefault();
		
	});
</script>
<script>
	function Editproduct(id) {
		Swal.fire({
			title: 'กรุณากรอกชื่อสินค้าที่ต้องการแก้ไข',
			input: 'text',
			inputAttributes: {
				autocapitalize: 'off'
			},
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (text) => {

				$.post("<?=base_url('Admin/Product/edit')?>", {
						id: id,
						text: text
					},
					function (data) {
						console.log(data)
						d = JSON.parse(data)
						var test = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "แก้ไขสินค้าเรียบร้อย",
							})
							setTimeout(function () {
								location.href = '<?=base_url("Admin/Product")?>'
							}, 1000);
						}

					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}
</script>
<script>
	function Deleteproduct(id) {
		Swal.fire({
			title: 'คุณต้องการลบสินค้านี้ ?',
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (login) => {
				$.post("<?=base_url('Admin/Product/delete/')?>" + id,
					function (data) {
						d = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "ลบรายการนี้เรียบร้อย",
							})
							setTimeout(function () {
								location.reload(true);
							}, 1000);
						}else{
              Swal.fire({
								icon: "error",
								text: "ไม่สามารถลบสินค้านี้ได้",
							})
            }
					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}
</script>
<script>
	function Deletecode(id) {
		Swal.fire({
			title: 'คุณต้องการลบไอดีนี้ ?',
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (login) => {
				$.post("<?=base_url('Admin/ManageProduct/delete/')?>" + id,
					function (data) {
						d = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "ลบรายการนี้เรียบร้อย",
							})
							setTimeout(function () {
								location.reload(true);
							}, 1000);
						}else{
              			Swal.fire({
								icon: "error",
								text: "ไม่สามารถลบไอดีนี้ได้",
							})
           				 }
					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}
</script>
<script>
	$(document).ready(function () {
		$('.editcode').on('click', function() {
			$tr = $(this).closest('tr');
			var data = $tr.children("td").map(function() {
				return $(this).text();
			}).get();
			console.log(data[0].replace('\t','').trim());
			
			$('#username').val(data[1].replace('\t','').trim());
		});
	});
</script>
<script>
	$(document).on('submit', '#addcode_form', function () {
		$.post("<?=base_url('Admin/ManageProduct/Addcode')?>", $("#addcode_form").serialize(),
			function (data) {
				d = JSON.parse(data)
				if (d.status == 1) {
					Swal.fire({
						icon: "success",
						text: "เพิ่มรหัสลงระบบเรียบร้อย",
					})
					setTimeout(function () {
								location.reload(true);
							}, 1000);
				} else if (d.status == 0) {

					Swal.fire({
						icon: "error",
						text: "รหัส"+d.msg+"ถูกเพิ่มเข้าระบบแล้ว",

					});
				}
			}
		);

		event.preventDefault();
	});
</script>
<script>
	$(document).on('submit', '#adddiscount_form', function () {
		$.post("<?=base_url('Admin/Code/Addcode')?>", $("#adddiscount_form").serialize(),
			function (data) {
				d = JSON.parse(data)
				if (d.status == 1) {
					Swal.fire({
						icon: "success",
						text: "เพิ่มโค้ดลงระบบเรียบร้อย",
					})
					setTimeout(function () {
								location.reload(true);
							}, 1000);
				} else if (d.status == 0) {

					Swal.fire({
						icon: "error",
						text: "โค้ดนี้มีในระบบแล้ว",

					});
				}
			}
		);

		event.preventDefault();
	});
</script>
<script>
	function Deletediscount(id) {
		Swal.fire({
			title: 'คุณต้องการลบโค้ดนี้ ?',
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (login) => {
				$.post("<?=base_url('Admin/Code/delete/')?>" + id,
					function (data) {
						d = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "ลบรายการนี้เรียบร้อย",
							})
							setTimeout(function () {
								location.reload(true);
							}, 1000);
						}else{
              			Swal.fire({
								icon: "error",
								text: "ไม่สามารถลบโค้ดนี้ได้",
							})
           				 }
					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}
</script>
<script>
	function DeleteUsers(id) {
		Swal.fire({
			title: 'คุณต้องการลบผู้ใช้นี้ ?',
			showCancelButton: true,
			confirmButtonText: 'ยืนยัน',
			cancelButtonText: 'ยกเลิก',
			showLoaderOnConfirm: true,
			preConfirm: (login) => {
				$.post("<?=base_url('Admin/EditUser/delete/')?>" + id,
					function (data) {
						d = JSON.parse(data)
						if (d.status == 1) {
							Swal.fire({
								icon: "success",
								text: "ลบรายการนี้เรียบร้อย",
							})
							setTimeout(function () {
						location.href = '<?=base_url("Admin/ManageUser")?>'
					}, 2000);
						}else{
              Swal.fire({
								icon: "error",
								text: "ไม่สามารถผู้ใช้นี้",
							})
            }
					}
				);

			},
			allowOutsideClick: () => !Swal.isLoading()
		}).then((result) => {
			if (result.value) {

			}
		})
	}
</script>
<script>
	$(document).on('submit', '#withdrawad', function () {
		$.post("<?=base_url('Admin/Withdrawad/submit')?>", $("#withdrawad").serialize(),
			function (data) {
				d = JSON.parse(data)
				if (d.status == 1) {
					Swal.fire({
						icon: "success",
						text: "ถอนเงินเสร็จสิ้นกรุณารีเฟชรเพื่อดูยอดเงิน",
					})
				} else if (d.status == 0) {

					Swal.fire({
						icon: "error",
						text: d.msg,
					});
					console.log(d.msg)
				}
			}
		);

		event.preventDefault();
	});
</script>