var type_page = 0;
var id_type_page = 0;
var id_emp = 0;
var type_mony = 0;
var type_login = 0;
var emp_acc = [];
var username = "";
var password = "";

$(document).ready(function () {
	get_page();
	if(type_login == 0)
	{
		$('#login_emp').modal('show');
		$('.wrapper').hide();
	}
});
function login()
{
	username = $('#username_login').val();
	password = $('#password_login').val();
	var txt = "select * from `emp` where `username` = '"+username+"' and `password` = '"+password+"' and `login_emp` = '1' and `active_emp` = '0'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:2},
		success:function(data)
		{
			if(data.num == 0)
			{
				id_emp = 0;
				emp_acc = [];
				$('#username_login').val('');
				$('#password_login').val('');
				sweetAlert("كلمة السر او اسم المستخدم غير صحيح","","error");
				push_emp_login("insert into `login_emp` (`id_emp`,`user`,`pass`,`date`) values('"+id_emp+"','"+username+"','"+password+"','"+date_today+"')");
			}else{
				active();
				page_index();
				$('.wrapper').show();
				$('#login_emp').modal('hide');
				var txt = "select * from `emp` where `username` = '"+username+"' and `password` = '"+password+"' and `login_emp` = '1' and `active_emp` = '0'";
				$.ajax({
					url:"model/class/class.php",
					type:"POST",
					dataType:"JSON",
					data:{txt:txt,type:1},
					success:function(data)
					{
						id_emp = data[0].id_emp;
						emp_acc[0] = data[0].c_1;
						emp_acc[1] = data[0].c_2;
						emp_acc[2] = data[0].c_3;
						emp_acc[3] = data[0].c_4;
						emp_acc[4] = data[0].c_5;
						emp_acc[5] = data[0].c_6;
						emp_acc[6] = data[0].c_7;
						emp_acc[7] = data[0].c_8;
						emp_acc[8] = data[0].c_9;
						emp_acc[9] = data[0].c_10;
						emp_acc[10] = data[0].c_11;
						emp_acc[11] = data[0].c_12;
						emp_acc[12] = data[0].c_13;
						emp_acc[13] = data[0].c_14;
						push_emp_login("insert into `login_emp` (`id_emp`,`user`,`pass`,`date`) values('"+id_emp+"','"+username+"','"+password+"','"+date_today+"')");
						push_acc();
					}
				})
			}
		}
	})
	//push_acc();
}
function page_index()
{
	$('#pg_body').html('');
	var txt = `   <div class="row">
	   <div class="alert card1" style="background-color:#f2f2f2;width:100%" role="alert">
	<table class="mdl-data-table dataTable" style="font-size:24px;width:100%;">
	  <tbody>
		<tr>
		  <td>اسم الشركة المنتجة</td>
		  <td>prog egypt</td>
		</tr>
		<tr>
		  <td>اسم المطور</td>
		  <td>abdallah ahmed</td>
		</tr>
		<tr>
		  <td>تاريخ تجديد البرنامج</td>
		  <td id="date_renew"></td>
		</tr>
		<tr>
		  <td>باقى الايام لانتهاء المدة</td>
		  <td id="day_renew"></td>
		</tr>	
		<tr>
		  <td>رقم الاتصال</td>
		  <td>01026714865</td>
		</tr>
		<tr>
		  <td>الموقع الالكترونى</td>
		  <td>www.progegy.com</td>
		</tr>	
		</tbody>
	</table>
	</div>
	<div class="alert card1" style="background-color:#f2f2f2" role="alert">
	  <center><h1 class="alert-heading">خدماتنا</h1>
	  <div class="card card1 text-white bg-success mb-3" style="">
	  <div class="card-header">تصميم المواقع</div>
	  <div class="card-body">
		<h5 class="card-title">تصميم جميع انواع المواقع</h5>
		<p class="card-text">نستطيع ان نصمم جميع انواع المواقع بافضل التصاميم وباقل الاسعار</p>
	  </div>
	</div>
	  <div class="card card1 text-white bg-success mb-3" >
	  <div class="card-header">تصميم تطبيقات الهواتف</div>
	  <div class="card-body">
		<h5 class="card-title"></h5>
		<p class="card-text">نستطيع ان نقوم بعمل جميع التطبيقات بتصاميم متناسقة و امكانيات عالية</p>
	  </div>
	</div>
	  <div class="card card1 text-white bg-success mb-3">
	  <div class="card-header">تصميم البرامج الادارية</div>
	  <div class="card-body">
		<h5 class="card-title"></h5>
		<p class="card-text">يوجد لدينا برامج لادارة الاماكن مثل الجيم و المطعم و الكافية ويمكننا ان نصمم لك اى برنامج </p>
	  </div>
	</div>
	  <div class="card card1 text-white bg-success mb-9" style="width:900px">
	  <div class="card-header">  </div>
	  <div class="card-body">
		<h5 class="card-title"></h5>
		<h1 class="card-text">اذا اردت تصميم خاص بك فى جميع المجالات فلا تتردد فى تصميمة عندنا </h1>
	  </div>
	</div>
	  <hr>
	  <p class="mb-0"></p>
	</div>`;
$('#pg_body').html(txt);
active();
get_page();
}
function get_select_cata(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var html_code = "<option value="+response[i].id_cata+">"+response[i].name_cata+"</option>";
				$('#'+id_select).append(html_code);
			}
		}
	})
}
function get_select_cat(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var html_code = "<option value="+response[i].id_cat+">"+response[i].name_cat+"</option>";
				$('#'+id_select).append(html_code);
			}
		}
	})
}
function get_select_member(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var html_code = "<option value="+response[i].id_member+">"+response[i].name_member+"</option>";
				$('#'+id_select).append(html_code);
			}
		}
	})
}
function push_option(value,id_select,id_model)  
{
    setTimeout( function(){
            $('#'+id_select).val(value);
            $('#'+id_model).modal('show');
    },150);
}
function insert_mony(id,type)
{
	$('.form-control').val('');
	$('#modal_mony').modal('show');
	id_type_page = id;
	type_mony = type;
	get_select_cata("select * from `cata` where 1 ","select_member_mony");
}
function get_select_cata(txt,id_select)
{
	$('#'+id_select).html('');
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(response)
		{
			var l = response.length;
			for(var i = 0;i < l;i++)
			{
				var html_code = "<option value="+response[i].id_cata+">"+response[i].name_cata+"</option>";
				$('#'+id_select).append(html_code);
			}
		}
	})
}
function save_mony()
{
	if(type_mony == 0)
	{
		var name_table = "exp";
	}else{
		var name_table = "inp";
	}
	var i_m = $('#select_member_mony').val();
	var value = $('#value_mony').val();
	var comment = $('#commant_mony').val();
	var txt = "insert into `"+name_table+"_mony` (`id_"+name_table+"`,`id_user`,`value_"+name_table+"`,`comment_"+name_table+"`,`date_"+name_table+"`, `id_member`) values ('"+id_type_page+"','"+id_emp+"','"+value+"','"+comment+"','"+date_today+"','"+i_m+"')";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success();
		}
	})
}
function push_emp_login(txt)
{
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{

		}
	})
}
function get_page()
{
	var txt = "select * from `sys` where `id_sys` = '1'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
			$('#ul_date').text(date_today);
			if(data[0].type_sys == 0)
			{
				$('body').css("background-color","#fff");
				$('.all').css("background-color","white");
				$('.modal-content').css("color","black");
				$('table').prop("class","table table-light");
				$('table').prop("style","");
				$('#sidebar').css("background-color","#7386D5");
				$('.modal-content').css("background-color","white");
				$('.card1').css("background-color","white");
				$('#nav2').prop("class","navbar navbar-expand-lg navbar-light bg-light");
				$('#sidebar').prop("style","background-color:#343A40");

			}else{
				$('.all').css("background-color","#444");
				$('#sidebar').css("background-color","#060f1f");
				$('body').css("color","#e4e4e4");
				$('.modal-content').css("background-color","#444");
				$('.modal-content').css("color","#e4e4e4");
				$('table').prop("class","table table-dark");
				$('table').prop("style","");
				$('body').prop("style","");
				$('#nav2').prop("class","navbar navbar-expand-lg navbar-dark bg-dark");
				$('#sidebar').prop("style","background-color:#343A40");
				$('.card1').css("background-color","#343A40");
			
			}
		}
	})
}
function push_acc()
{
	var length = emp_acc.length;
	for(var i = 0;i < length;i++)
	{
		var ii = i + 1;
		//alert(ii);
		if(emp_acc[i] == 0)
		{
			$('.c_'+ii).hide();
		}else{
			$('.c_'+ii).show();
		}
		if(emp_acc[5]  == 1 &&  emp_acc[6] == 1 && emp_acc[7] == 1)
		{
			
		}
		if(emp_acc[1] == 1 && emp_acc[2] == 1 && emp_acc[3] == 1)
		{
			$('.cus_c_1').show();
			$('.cus').show();
		}else{
			$('.cus_c_1').hide();
			$('.cus').hide();
		}
		if(emp_acc[1] == 1 || emp_acc[2] == 1  || emp_acc[3] == 1)
		{
			$('.cus').show();
		}
		if(emp_acc[4] == 1 && emp_acc[5] == 1 && emp_acc[6] == 1)
		{
			$('.pro_c_1').show();
		}else{
			$('.pro_c_1').hide();
		}
		if(emp_acc[4] == 1 || emp_acc[5] == 1 || emp_acc[6] == 1)
		{
			$('.pro').show();

		}else{
			$('.pro').hide();
		}
	}
}
function logout()
{
	type_login = 0;
	id_emp = 0;
	 emp_acc = [];
   username = "";
   password = "";
   $('.form-control').val('');
   $('#login_emp').modal('show');
   $('.wrapper').hide();
   page_index();
}
function active()
{
	$.ajax({
		url:"model/class/c_1.php",
		dataType:"JSON",
		success:function(data)
		{

			$('#date_renew').text(data.end);
			$('#day_renew').text(data.aa);
			if(data.a == 1)
			{

			}else{
				$('body').html('');
				$('body').html('<h1>يجب تجديد البرنامج</h1>')
			}
			if(data.b == 0)
			{
				$('body').html('');
				$('body').html('<h1>تم قرصنة البرنامج وذالك يعرضك للمسالة القانونية</h1>')			
			}
		}
	})
}
function del_table_1(id_type,id_table)
{
	id_type_page = id_type;
	if(id_table == 0)
	{
		var txt  = "update `jobs` set `active_job` = '1' where `id_job` = '"+id_type_page+"'";
	}
	if(id_table == 1)
	{
		var txt  = "update `branch` set `active_branch` = '1' where `id_branch` = "+id_type_page+"";
	}
		if(id_table == 2)
	{
		var txt  = "update `emp` set `active_emp` = '1' where `id_emp` = "+id_type_page+"";

	}
		if(id_table == 3)
	{
		var txt  = "update `cata` set `active_cata` = '1' where `id_cata` = "+id_type_page+"";

	}
		if(id_table == 4)
	{
		var txt  = "update `cat` set `active_cat` = '1' where `id_cat` = "+id_type_page+"";

	}
		if(id_table == 5)
	{
		var txt  = "update `pro` set `active_pro` = '1' where `id_pro` = "+id_type_page+"";

	}
		if(id_table == 6)
	{
		var txt  = "update `expen` set `active_expense` = '1' where `id_expense` = "+id_type_page+"";
	}
		if(id_table == 7)
	{
		var txt  = "update `inp` set `active_inp` = '1' where `id_inp` = "+id_type_page+"";

	}
	if(id_table == 9)
	{
		var txt  = "update `member` set `active_member` = '1' where `id_member` = "+id_type_page+"";

	}
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
			success()
		}
	})
}
function get_cus_info(id)
{
	id_type_page = id;
	$.ajax({
		url:"model/class/cus_s.php",
		type:"POST",
		dataType:"JSON",
		data:{id:id},
		success:function(data)
		{
			$('#view_cus').modal('show');
			$('#v_name').val(data.name);
			$('#card_id').val(data.card);
			$('#v_type').val(data.v_type);
			$('#v_mony').val(data.v_mony);
			$('#value_date_end').val(data.end);
		}
	})
}