var count_sales_parson = 0;
// var price = 0;
function open_sales_ads()
{
    id_type_page = 0;
    $('.form-control').val('');
    $('#modal_sales_ads').modal('show');
}

function sales_ads()
{
 	var txt = `<div class="row">
				<button style="font-size:25px;background-color:#fff;border-radius:50%" type="button" class=" c_5 btn btn-outline-success btn-floating waves-effect waves-light btn-large" onclick="open_sales_ads()" >
				 +  
				</button>
				  <table id="Table1" class="mdl-data-table dataTable" cellspacing="0" width="100%">
				   <thead style="">
						  <tr>
							  <th>م</th>
							  <th>الاسم</th>
							  <th>الهاتف</th>
							  <th></th>
						  </tr>
					  </thead>
						<tbody>

						</tbody>
				  </table>
			   </div>`;
    $('#pg_body').html(txt);
    
    var dataTable = $('#Table1').DataTable( {
                columnDefs: [
        {
            targets: [ 0, 1, 2 ],
            className: 'mdl-data-table__cell--non-numeric'
    }],
        "processing": true,
        "serverSide": true,
        "ajax":{
            url :"model/class/sales_ads.php",
            type: "post",  
			dataType:"JSON",
            error: function(){
                
            }
        }
    } );
	$('#Table1').DataTable();
	get_page();
}
function save_sales_ads()
{
    var name = $('#name_sales_ads').val();
	var phone = $('#phone_sales_ads').val();
    var comment = $('#comment_sales_ads').val();
    if(id_type_page == 0)
    {
        var txt = "insert into `parson_ads` (`name_parson`,`phone_parson`,`comment_ads`) values ('"+name+"','"+phone+"','"+comment+"')";

    }else{
        var txt = "update `parson_ads` set `name_parson` = '"+name+"' , `phone_parson` = '"+phone+"', `comment_ads` = '"+comment+"' where `id_parson` = '"+id_type_page+"'";
    }
    
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:0},
        success:function()
        {
            success();
        }
    })
}
function get_sales_ads(id)
{
    id_type_page = id;
    var txt = "select * from `parson_ads` where `id_parson` = '"+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_sales_ads').val(data[0].name_parson);
            price = $('#phone_sales_ads').val(data[0].phone_parson); 
            price = $('#comment_sales_ads').val(data[0].comment_ads); 
            $('#modal_sales_ads').modal('show');

            // push_option(data[0].id_cat,"id_cat_pro","model_pro")           
            // push_option(data[0].id_sup,"id_sup_pro","model_pro")           
        }
    })
}

function area(){
    type_1(8)
}



function open_ads()
{
    $('.form-control').val('');
    $('#modal_ads').modal('show');
}
function ads(s){
    if(s == "s"){
        var alert_code = `
        <div class="alert alert-primary" role="alert">
            جميع الحملات
        </div>
      `;
    }
    if(s == 1){
        var alert_code = `
        <div class="alert alert-success" role="alert">
            النشط
        </div>
      `;
    }
    if(s == 0){
        var alert_code = `
        <div class="alert alert-danger" role="alert">
            الغير نشط
        </div>
      `;
    }
    var txt = `<div class="row">
    <div class="col-12">
        `+alert_code+`
    </div>

  
        <button style="font-size:25px;background-color:#fff;border-radius:50%" type="button" class=" c_5 btn btn-outline-success btn-large btn-floating waves-effect waves-light" onclick="open_ads()" >
        +  
       </button>

        <button type='button' class='btn-lg btn btn-success'  onclick='ads(1)'>النشط</button>


        <button type='button' class='btn-larg btn btn-danger'  onclick='ads(0)'>الغير نشط</button>



        <button type='button' class='btn-lg btn btn-primary'  onclick='ads("s")'>جميع الحملات</button>



      <table id="Table1" class="mdl-data-table dataTable" cellspacing="0" width="100%">
       <thead style="">
              <tr>
                  <th>م</th>
                  <th>اسم البوست</th>
                  <th>الرقم التعريفي</th>
                  <th>المنصه</th>
                  <th>حالة الاعلان</th>
                  <th></th>
              </tr>
          </thead>
            <tbody>

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

var dataTable = $('#Table1').DataTable( {
    columnDefs: [
{
targets: [ 0, 1, 2 ],
className: 'mdl-data-table__cell--non-numeric'
}],
"processing": true,
"serverSide": true,
"ajax":{
url :"model/class/ads.php",
data:{type:s},
type: "post",  
dataType:"JSON",
error: function(){
    
}
}
} );
$('#Table1').DataTable();
get_page();    
}

function save_ads()
{
    var name = $('#name_ads').val();
	var none = $('#none_ads').val();
	var paltform = $('#paltform_ads').val();
    var comment = $('#comment_ads').val();
    if(id_type_page == 0)
    {
        var txt = "insert into `ads` (`name_ads`,`none_ads`,`paltform_ads`,`comment_ads`) values ('"+name+"','"+none+"','"+paltform+"','"+comment+"')";

    }else{
        var txt = "update `ads` set `name_ads` = '"+name+"' , `none_ads` = '"+none+"', `paltform_ads` = '"+paltform+"' , `comment_ads` = '"+comment+"' where `id_ads` = '"+id_type_page+"'";
    }
    
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:0},
        success:function()
        {
            success();
        }
    })    
}


function get_ads(id)
{
    id_type_page = id;
    var txt = "select * from `ads` where `id_ads` = '"+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_ads').val(data[0].name_ads);
            price = $('#none_ads').val(data[0].none_ads); 
            price = $('#paltform_ads').val(data[0].paltform_ads); 
            price = $('#comment_ads').val(data[0].comment_ads); 
            $('#modal_ads').modal('show');

            // push_option(data[0].id_cat,"id_cat_pro","model_pro")           
            // push_option(data[0].id_sup,"id_sup_pro","model_pro")           
        }
    })
}


function start_ads(id){
    $('.form-control').val('');
    $('#modal_ads_start').modal('show');

    id_type_page = id;
}
function save_ads_stat(){
    var name = $('#name_ads_stat').val()
    var mony = $('#mony_ads_stat').val()
    var comment = $('#comment_ads_stat').val()
    var txt = "update `ads` set type_ads = '1', start_ads = '"+date_today+"' where `id_ads` = '"+id_type_page+"'";

    var start_date = date_today;



    $.ajax({
        url:'model/class/class.php',
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:0},
        success:function(data){
            var txt = "insert into `ads_stat` (`id_ads`,`start_ads`,`name_ads_stat`,`mony_ads_stat`,`comment_ads_stat`) values ('"+id_type_page+"','"+start_date+"','"+name+"','"+mony+"','"+comment+"')";
            $.ajax({
                url:'model/class/class.php',
                type:"POST",
                dataType:"JSON",
                data:{txt:txt,type:0},
                success:function(data){
                    success()
                }
            })
        }
    })
}

function end_ads(id){
    var x = confirm("هل تريد ايقاف الحمله")
    if(x){


        var txt = "update `ads` set type_ads = '0'  where `id_ads` = '"+id+"'";
        $.ajax({
            url:"model/class/class.php",
            type:'POST',
            dataType:'JSON',
            data:{txt:txt,type:0},
            success:function(data){
                var end_date = date_today;
                var txt = "update `ads_stat` set `end_ads` = '"+end_date+"' , `type_ads_stat` = '1' where `id_ads` = '"+id+"' and `type_ads_stat` = '0'";
                $.ajax({
                    url:'model/class/class.php',
                    type:"POST",
                    dataType:"JSON",
                    data:{txt:txt,type:0},
                    success:function(data){
                        success()
                    }
                })
            }
        })
            
        
    }else{
        sweetAlert("لم يتم الايقاف","","")
    }

    

}

function ads_cus()
{
        var txt = `<div class="row">
        <button style="font-size:25px;background-color:#fff;border-radius:50%" type="button" class=" c_5 btn btn-outline-success btn-floating waves-effect waves-light btn-large" onclick="open_cus_ads()" >
        +  
        </button>
        <table id="Table1" class="mdl-data-table dataTable" cellspacing="0" width="100%">
        <thead style="">
                <tr>
                    <th>م</th>
                    <th>الاسم</th>
                    <th>الهاتف</th>
                    <th>الاعلان</th>
                    <th>ID</th>
                    <th>الملاحظات</th>
                    <th></th>
                </tr>
            </thead>
                <tbody>

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

    var dataTable = $('#Table1').DataTable( {
        columnDefs: [
    {
    targets: [ 0, 1, 2 ],
    className: 'mdl-data-table__cell--non-numeric'
    }],
    "processing": true,
    "serverSide": true,
    "ajax":{
    url :"model/class/cus_ads.php",
    type: "post",  
    dataType:"JSON",
    error: function(){
        
    }
    }
    } );
    $('#Table1').DataTable();
    get_page();    
}

function open_cus_ads()
{
    id_type_page = 0;
    $('.form-control').val('');
    select_ads()
    $('#modal_cus_ads').modal('show');    
}

function select_ads()
{
    $('#post_cus_ads').html('')
    var txt = "select * from `ads` where `type_ads` = '1' and `active_ads` = '0'";
    $.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_ads+`'>`+response[i].name_ads+`</option>
                `;
                $('#post_cus_ads').append(html_code)
            }

        }
    })
}
function save_cus_ads()
{
    var name = $('#name_cus_ads').val()
    var phone = $('#phone_cus_ads').val()
    var post = $('#post_cus_ads').val()
    var comment = $('#comment_cus_ads').val()
    if(id_type_page == 0)
    {
        var txt = "insert into `cus_ads` (`name_cus_ads`,`phone_cus_ads`,`comment_cus_ads`,`id_ads`,`date_cus_ads`) values ('"+name+"','"+phone+"','"+comment+"','"+post+"','"+date_today+"')"
    }else{
        var txt = "update `cus_ads` set `name_cus_ads` = '"+name+"',`phone_cus_ads` = '"+phone+"' ,`comment_cus_ads` = '"+comment+"',`id_ads` = '"+post+"' where `id_cus_ads` = '"+id_type_page+"'"
    }
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:0},
        success:function(data)
        {
            success()
        }
    })
}

function get_cus_ads(id)
{
    select_ads()
    id_type_page = id;
    var txt = "select * from `cus_ads` where `id_cus_ads` = '"+id+"'"
    $.ajax({
        url:"model/class/class.php",
        type:"POST",
        dataType:"JSON",
        data:{txt:txt,type:1},
        success:function(data)
        {
            var name = $('#name_cus_ads').val(data[0].name_cus_ads)
            var phone = $('#phone_cus_ads').val(data[0].phone_cus_ads)
            var post = $('#post_cus_ads').val(data[0].id_ads)
            var comment = $('#comment_cus_ads').val(data[0].comment_cus_ads)      
            $('#modal_cus_ads').modal('show');    
        }
    })
}