<script>
    $(function()
    {

        $(".checkbox").click(function(){
            $('#delete').prop('disabled',$('input.checkbox:checked').length == 0);
        });

    });
</script>
<?php
include "../../pidbco/pidbjoin.php";

if(!isset($_SESSION))
{
    session_start();
}

if(isset($_POST['btn_add_des_data']))
{
    $des_name = strip_tags(trim($_POST['des_name']));
    $status = strip_tags(trim($_POST['status']));

    if(!empty($des_name) && $status != "")
    {
       $qry1 = mysqli_query($con,"SELECT des_name FROM designation_det WHERE des_name = '$des_name'");
       $num_row = mysqli_num_rows($qry1);
       if($num_row > 0)
       {
        ?><script>alert("Name Exist..!");</script><?php
       }
       else
       {
           $qry = mysqli_query($con,"INSERT INTO designation_det(des_name, status) VALUES ('$des_name','$status')");


        ?>
        <script>
            $('#des_name').val('');
            show_des_data();
            window.location.href="Designation-Registration";
        </script>
       <?php
       }
    }
    else
    {
     ?>
        <script>alert('Error..!');</script>
    <?php
    }

 exit();
}
if(isset($_POST['btn_show_des_data']))
{

    ?>
    <div class="dataTables_wrapper">
        <table class="table table-striped table-bordered table-hover table-responsive" id="dataTables-example">
            <thead>
            <tr>
                <th width="5%">#</th>
                <th>Designation</th>
                <th>Status</th>
            </tr>
            </thead>
            <tbody>
            <?php
            $stmt = mysqli_query($con,"SELECT * FROM designation_det ORDER BY id DESC ");
            $i=1;
            while($res = mysqli_fetch_object($stmt))
            {
                ?>
                <tr class="odd gradeX" data-toggle="modal"  style="cursor: pointer;">
                    <td width="5%"><input type="checkbox" class="checkbox"  id="<?php echo $res->id; ?>" style="cursor: pointer;" /></td>
                    <td onclick="edt_designation(<?php echo $res->id; ?>);" ><?php echo $res->des_name; ?></td>
                    <td width="20%" onclick="edt_designation(<?php echo $res->id; ?>);" class="center"><?php if($res->status == 1){ echo 'Active'; }else{ echo 'In-Active'; } ?></td>
                </tr>
                <?php
                $i++;
            }
            ?>
            </tbody>
        </table>
    </div>
    <script>
        $(document).ready(function() {
            $('#dataTables-example').DataTable({
                responsive: true
            });
        });
    </script>
    <?php
    exit();
}
if(isset($_POST['btn_edt_des_data']))
{
    $id = strip_tags(trim(intval($_POST['id'])));

    $qry02 = mysqli_query($con,"SELECT * FROM designation_det WHERE id='$id'");
    $res = mysqli_fetch_object($qry02);
?>
    <form id="frm_1" name="frm_1" data-parsley-validate class="form-horizontal form-label-left">

        <div class="form-group">
            <div class="col-md-3 col-sm-3 col-xs-12">
                <input id="des_name" class="form-control col-md-7 col-xs-12" value="<?php echo $res->des_name; ?>" placeholder="Designation Name*" required="required"  type="text" name="des_name" >
                <input type="hidden" id="eid" value="<?php echo base64_encode($res->id); ?>">
            </div>
            <div class="col-md-3 col-sm-3 col-xs-12">
                <select id="status" class="form-control col-md-7 col-xs-12 " name="status"  required="required">
                    <?php
                    if($res->status == 1)
                    {
                    ?>
                        <option value="1">Active</option>
                        <option value="0">In-Active</option>
                    <?php
                    }
                    else
                    {
                    ?>
                        <option value="0">In-Active</option>
                        <option value="1">Active</option>
                    <?php
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-3 col-sm-3 col-xs-12 " id="btn_data">
                <button type="button" onclick="upd_designation()" class="btn btn-primary ">Update</button>
            </div>
        </div>

    </form>
<?php
 exit();
}
if(isset($_POST['btn_upd_des_data']))
{
    $des_name = strip_tags(trim($_POST['des_name']));
    $status = strip_tags(trim($_POST['status']));
    $eid = strip_tags(trim(base64_decode($_POST['eid'])));

    if(!empty($des_name) && $status != "" && !empty($eid))
    {
        $qry1 = mysqli_query($con,"SELECT des_name FROM designation_det WHERE des_name = '$des_name' AND id != '$eid'");
        $num_row = mysqli_num_rows($qry1);
        if($num_row > 0)
        {
            ?><script>alert("Name Exist..!");</script><?php
        }
        else
        {
            $qry = mysqli_query($con,"UPDATE designation_det SET des_name = '$des_name',status = '$status' WHERE id = '$eid'");

            ?>
            <script>
                $('#des_name').val('');
                $('#btn_data').html('<button type="button" onclick="add_designation()" class="btn btn-primary ">SAVE</button>');
                show_des_data();
                window.location.href="Designation-Registration";
            </script>
        <?php
        }
    }
    else
    {
        ?>
        <script>alert('Error..!');</script>
    <?php
    }

    exit();
}
if(isset($_POST['btn_delete_des_data']))
{
    $count = "";$i=0;
    $checked = $_POST['checked'];
    $count = count($checked);
    if($count > 0)
    {
        while($i<$count)
        {
            $del_id = $checked[$i];
            mysqli_query($con,"DELETE FROM designation_det WHERE id = '$del_id'");
            $i++;
        }
        ?>
        <script>
            show_des_data();
        </script>
    <?php
    }
    else
    {
        ?>
        <script>alert("Errorr..!");</script>
    <?php
    }

    exit();
}
?>
