var count_pro = 0;
var price = 0;
function open_pro()
{
    $('.form-control').val('');
	if(emp_acc[14] == 0){
		sweetAlert("لا يوجد صلاحيه","","error")
	}else{
		get_select_cat("select * from `cat` where `active_cat` = '0'","id_cat_pro");
		get_select_sup("select * from `sup` where `active_sup` = '0'","id_sup_pro");
		$('#model_pro').modal('show');
	}

}
function i_pro(id)
{
    id_type_page = id;
    $('.form-control').val('');
	$('#i_pro').modal('show');
	$('#i_c_pro').val('0');
}
function i_pro(id)
{
    id_type_page = id;
    $('.form-control').val('');
	$('#i_pro').modal('show');
	$('#i_c_pro').val('0');
}
function pro()
{
	var txt = `<div class="card-body">
	<a  onclick="open_pro()" style="font-family:bold;font-size:21px;color:white;backgroundColor:#007bff;" class="btn bg-success">
	<i class="fas fa-user-plus"></i></a>
  <table id="example1" class="table_image table table-bordered table-striped">
  <thead style="color:white;background-color:#007bff;font-family:bold;font-size:19px">
  <th>م</th>
  <th>اسم الصنف</th>
  <th>المجموعة</th>
  <th>المورد</th>
  <th>السعر</th>
  <th>الكمية</th>
  <th></th>
  </thead>
  <tbody>


  </tbody>

</table>
	 </div>`;
	 $('#pg_body').html(txt);
	 $(function () {

	   var table = $("#example1").DataTable({
			   dom: 'Bfrtip',
			   //'paging':false,
			   "responsive": true, "lengthChange": false, "autoWidth": false,
			   buttons: ["colvis","copy", "excel", "pdf", "print"],
			   
					   "processing": true,
					   "serverSide": true,
					   "ajax":{
						url :"model/class/pro.php",
						type: "post",  
						dataType:"JSON",
						data:{i:emp_acc[16],o:emp_acc[17],u:emp_acc[15]},
					   }
					   
		   }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
	   
	 });
	get_page();
}
function save_pro()
{
    var name = $('#name_pro').val();
	var id_cat = $('#id_cat_pro').val();
    var price = $('#price_pro').val();
    if(id_type_page == 0)
    {
        var txt = "insert into `pro` (`name_pro`,`id_cat`,`price_pro`) values ('"+name+"','"+id_cat+"','"+price+"')";

    }else{
        var txt = "update `pro` set `name_pro` = '"+name+"' , `id_cat` = '"+id_cat+"', `price_pro` = '"+price+"' where `id_pro` = '"+id_type_page+"'";
    }
    
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:0},
        success:function()
        {
            success();
        }
    })
}
function get_pro(id)
{
    id_type_page = id;
    var txt = "select * from `pro` where `id_pro` = '"+id+"'";
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:1},
        success:function(data)
        {
            get_select_cat("select * from `cat` where `active_cat` = '0'","id_cat_pro");
            get_select_sup("select * from `sup` where `active_sup` = '0'","id_sup_pro");
            name = $('#name_pro').val(data[0].name_pro);
            price = $('#price_pro').val(data[0].price_pro); 
            push_option(data[0].id_cat,"id_cat_pro","model_pro")           
            push_option(data[0].id_sup,"id_sup_pro","model_pro")           
        }
    })
}

function in_pro()
{
	var c = parseFloat($('#i_c_pro').val());
    var txt = "select * from `pro` where `id_pro` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
        success:function(data)
        {
            var old_stock = parseFloat(data[0].stock_pro);
            var new_stock = parseFloat(old_stock + c);
            var txt = "update  `pro` set `stock_pro` = '"+new_stock+"' where `id_pro` = '"+id_type_page+"'";
            $.ajax({
                url:"model/class/class.php",
                type:"POST",
                dataType:"JSON",
                data:{txt:txt,type:0},
                success:function()
                {
                    success();
                }
            })
		}
	});
}
function count_pro_c()
{
	var count = $('#o_c_pro').val();
    count_pro = Number(count_pro);
    if(count <= count_pro)
	{
			var pr = price;
			var tax = $('#s_m_pro').val();
			var total_b = count * pr;
			$('#o_p_pro').val(total_b);
			if(tax == 0)
			{
				$('#total_pro').val(total_b);
			}else{
				var sub = total_b * (tax / 100);
				var total_bb = total_b - sub;
				$('#total_pro').val(total_bb);
			}
    }else{
		sweetAlert("","الكمية لا تكفى", "error");
		$('#o_c_pro').val(count_pro);
		count_pro_c();
	}
}
function save_o_pro()
{
	var q = $('#o_c_pro').val();
	var price_b = $('#o_p_pro').val();
	var tax = $('#s_m_pro').val();
	var price_f = $('#total_pro').val();
    var c = $('#comment_pro').val();

    var new_stock = parseFloat(count_pro - Number(q));
    var txt = "INSERT INTO `sales`(`id_pro`, `id_emp`, `q`, `p_b`, `tax`, `p_f`, `c`, `date`)VALUES ('"+id_type_page+"','"+id_emp+"','"+q+"','"+price_b+"','"+tax+"','"+price_f+"','"+c+"','"+date_today+"'); ";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:0},
		success:function()
		{
            var txt = "update `pro` set `stock_pro` = '"+new_stock+"' where `id_pro` = '"+id_type_page+"';";
            $.ajax({
                url:"model/class/class.php",
                type:"POST",
                dataType:"JSON",
                data:{txt:txt,type:0},
                success:function()
                {
                    success();
                }
            })
		}
	});
}
function o_pro(id)
{
	id_type_page = id;
	count_pro = 0;
	price = 0;
	$('#tax_2').hide();
	$('#o_c_pro').val('0');
	$('#o_p_pro').val('0');
	$('#s_m_pro').val('0');
	$('#name_o_pro').val('');
	$('#total_pro').val('0');
    $('#comment_pro').val('');
    var txt = "select * from `pro` where `id_pro` = '"+id_type_page+"'";
	$.ajax({
		url:"model/class/class.php",
		type:"POST",
		dataType:"JSON",
		data:{txt:txt,type:1},
		success:function(data)
		{
				price = data[0].price_pro;
				count_pro = data[0].stock_pro;
				if(count_pro < 1)
				{
					$('#o_pro').modal('hide');
					sweetAlert("","الكمية لا تكفى", "error");
				}else{
					$('#o_pro').modal('show');
					$('#name_o_pro').val(data[0].name_pro);
					$('#o_c_pro').val('1');
					$('#o_p_pro').val(price);
					$('#total_pro').val(price);
				}
		}
	});
}
function radio_c(x)
{
	if(x == 1)
	{
		$('#tax_1').hide();
		$('#tax_2').show();
	}else{
		$('#tax_2').hide();
		$('#tax_1').show();
	}
}
function count_pro_c_1()
{
	var x = parseFloat($('#o_p_pro').val());
	var z = parseFloat($('#s_m_pro_1').val());
	if(z == "")
	{
		$('#total_pro').val(x);
		$('#s_m_pro_1').val(0)
	}
	var t = parseFloat(x - z);
	if(x >= z)
	{
		$('#total_pro').val(t);
	}
}