<?php
require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(40, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

//insert.php

if(isset($_POST["contract_id"])){
    if($_POST['contract_id'] !=""){
          $contract_id     = $_POST['contract_id'];
          $operating_comm  = $_POST['operating_comm'];
          $ins_claim       = $_POST["ins_claim"];
          $addtion_desc    = $_POST["addtion_desc"];

$name ='/^[-a-zA-Z0-9_\p{Arabic}-+ـ\s]*$/u';
$dc = '/^[0-9]+(\.[0-9]{1,2})?$/' ;
$Number ="/^[0-9]+$/";

$data = array();
$error = array();

    if(!empty($operating_comm)){  $operating_comm = htmlspecialchars(strip_tags($operating_comm)) ;}else{
      $operating_comm = null ;
    }
    if(!empty($ins_claim)){ if(!preg_match($Number,$ins_claim)){ $error["ins_claim"] = "أرقام صحيحة فقط" ;}
    }else{$ins_claim = 0 ;}
    if(!empty($addtion_desc)){  $addtion_desc = htmlspecialchars(strip_tags($addtion_desc)) ;}
    else{$addtion_desc = null ;}

if(!empty($error)){
  $data["error"] = $error ;
}else {
    $contt=$db->prepare(" UPDATE contracts SET operating_comm = ? , ins_claim = ? , addtion_desc = ? WHERE contract_id = ? ");
   $contt->bindParam(1,$operating_comm);
   $contt->bindParam(2,$ins_claim);
   $contt->bindParam(3,$addtion_desc);
   $contt->bindParam(4,$contract_id);
   if($contt->execute()){
            $data["msg"]= "<div class='alert alert-success'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b>تم تحديث بيانات العقد بنجاح </b></div>" ;
             }
        else{ $data["msg"] = "<div class='alert alert-danger'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b></div> " ; }
    }
      echo json_encode($data);
}
}
//new newIssue
if(isset($_POST["newIssue"])){
          $contract_id     = $_POST['newIssue'];
$data = array();
    $contt=$db->prepare(" UPDATE contracts SET  cont_status = 4  WHERE contract_id = ? ");
   $contt->bindParam(1,$contract_id);
   if($contt->execute()){
 $data["msg"]= "<div class='alert alert-success'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b>تم رفع العقد للمحكمة </b></div>" ;
          }
 else{ $data["msg"] = "<div class='alert alert-danger'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b></div> " ; }
echo json_encode($data);
}
if(isset($_POST["debt"])){
      $contract_id     = $_POST['debt'];
      $data = array();
      $contt=$db->prepare(" UPDATE contracts SET  cont_status = 3  WHERE contract_id = ? ");
      $contt->bindParam(1,$contract_id);
if($contt->execute()){
$data["msg"]= "<div class='alert alert-success'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>تم تحويل العقد إلي عقود مديونة </b></div>" ;
  }
else{ $data["msg"] = "<div class='alert alert-danger'>
    <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
    <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b></div> " ; }
echo json_encode($data);
}
?>