<style>
    h1{font-size:22px;text-align:center;font-weight:700;}
</style>
<?php
$sid=$this->uri->segment('3');
$doc_id=$this->uri->segment('4');
$doc_type='Student-Bonafied-Certificate';
$doc_table='student_bonafied_certificate';
$acedemic_session=$this->session->userdata('acedemic_session');
$student_table='student';

$student_name='';
$father_name='';
$gender='';
$dob='';
$dob_in_words='';
$doc_fee='';
$issuing_date='';
$doc_copy_order='';

$page_title='';

$mother_name  =   "";

$active_course_id='';
$get_active_course_info=$this->CertificateModel->student_course_data($sid,$acedemic_session);
if($get_active_course_info){
    foreach($get_active_course_info as $course_info){
        $active_course_id=$course_info['course_id'];
    }
}


if($doc_id !=''){
    $page_title='Update Certificate';
    $get_student_doc_info=$this->CertificateModel->get_item_data($doc_id,$doc_table);    
    if($get_student_doc_info){
        foreach($get_student_doc_info as $doc_data){
            $student_name=$doc_data['student_name'];
            $father_name=$doc_data['father_name'];
            $gender=$doc_data['gender'];
            $dob=$doc_data['dob'];
            $dob_in_words=$doc_data['dob_in_words'];
            $doc_fee=$doc_data['fee'];
            $issuing_date=$doc_data['issuing_date'];
            $doc_copy_order=$doc_data['doc_copy_order'];
            $mother_name=$doc_data['mother_name'];
            
            if($mother_name == ""){
                
                $get_student_info=$this->CertificateModel->get_item_data($sid,$student_table);
                if($get_student_info){
                    foreach($get_student_info as $student_info){
                        // $student_name=$student_info['student_name'];
                        // $father_name=$student_info['father_name'];
                        // $gender=$student_info['gender'];
                        // $dob=date('Y-m-d',strtotime($student_info['dob']));
                        // $dob_in_words=$student_info['dob_in_words'];
                        $mother_name=$student_info['mother_name'];
                    }
                }
                
                
            }
        }
    }
}else{
    $page_title='Issue Certificate';
    $get_student_info=$this->CertificateModel->get_item_data($sid,$student_table);
    if($get_student_info){
        foreach($get_student_info as $student_info){
            $student_name=$student_info['student_name'];
            $father_name=$student_info['father_name'];
            $gender=$student_info['gender'];
            $dob=date('Y-m-d',strtotime($student_info['dob']));
            $dob_in_words=$student_info['dob_in_words'];
            $mother_name=$student_info['mother_name'];
        }
    }
}
$doc_count=1;
if($doc_copy_order !=''){
    $doc_count=$doc_copy_order;
}else{
    $get_docs=$this->CertificateModel->get_doc($sid,$doc_type,$doc_table);
    $doc_count=1;
    if($get_docs){
        foreach($get_docs as $docs){
            $doc_count=$doc_count + 1;
        }
    }
}
?>

<div class="container mt-3 mb-3 pt-3 pb-3 bg-white">
    <div class="row">
        <div class="col-sm-12">
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Doc No</th>
                            <th>Date</th>
                            <th>Issued By</th>
                            <th>Copy</th>
                            <th>View</th>
                            
                        </tr>
                    </thead>
                    <tbody>
                        <?php 
                        $all_docs=$this->CertificateModel->get_doc($sid,$doc_type,$doc_table);
                        if($all_docs){
                            foreach($all_docs as $docs){
                                $tid=$docs['cat_by'];
                                $staff_table='school_staff';
                                $staff_name='';
                                $staff_info=$this->CertificateModel->get_item_data($tid,$staff_table);
                                if($staff_info){
                                    foreach($staff_info as $staffinfo){
                                        $staff_name=$staffinfo['name'];
                                    }
                                }
                        ?>
                        <tr>
                            <td><?php echo $docs['id'];?></td>
                            <td><?php echo date('d-M-Y',strtotime($docs['issuing_date']));?></td>
                            <td><?php echo $staff_name;?></td>
                            <td><?php echo $docs['doc_copy_order'];?></td>
                            <td><a href="<?php echo base_url();?>Certificate/student_certificate/<?php echo $docs['id'];?>" class="btn btn-sm btn-info" target="_blank">View</a></td>
                            
                        </tr>
                        <?php
                            }
                        }else{
                        ?>
                        <tr>
                            <td colspan="5" class="text-center text-danger">No data found</td>
                        </tr>
                        <?php
                        }
                        ?>
                        
                    </tbody>
                </table>
            </div>
            
            <form method="post" action="#">
                <h1 class="main-heading mt-3 ">Issue Student Bonafied Certificate</h1>
                <?php 
                if($success){
                ?>
                <div class="alert alert-success"><?php echo $success;?></div>    
                <?php
                }
                if($error){
                ?>
                <div class="alert alert-danger"><?php echo $error;?></div>
                <?php
                }
                
                ?>
                <hr>
                <input type="hidden" name="doc_id" value="<?php echo $doc_id; ?>">
                <div class="form-group fgwf">
                    <label>Student Name</label>
                    <input type="hidden" name="sid" value="<?php echo $sid;?>">
                    <input type="text" name="student_name" class="form-control" value="<?php echo $student_name; ?>" readonly>
                </div>
                <div class="form-group fgwf">
                    <label>Gender</label>
                    <select class="form-control" name="gender" required>
                        <option value="">-- select gender --</option>
                        <option value="male" <?php if($gender == "male"){echo "selected";}?>>Male</option>
                        <option value="female" <?php if($gender == "female"){echo "selected";}?>>Female</option>
                    </select>
                </div>
                <div class="form-group fgwf">
                    <label>Salutation</label>
                    <select class="form-control" name="salutation">
                        <option value="Mr.">Mr.</option>
                    </select>
                </div>
                <div class="form-group fgwf">
                    <label>Father's Name</label>
                    
                    <input type="text" name="father_name" class="form-control" value="<?php echo $father_name;?>" readonly>
                </div>
                
                <div class="form-group fgwf">
                    <label>Salutation</label>
                    <select class="form-control" name="mother_name_salutation">
                        <option value="Mrs.">Mrs.</option>
                    </select>
                </div>
                <div class="form-group fgwf">
                    <label>Mother's Name</label>
                    
                    <input type="text" name="mother_name" class="form-control" value="<?php echo $mother_name; ?>" readonly>
                </div>
                
                <div class="form-group fgwf">
                    <label>Course</label>
                    <select class="form-control" name="course" required>
                        <?php
                        $courses_table='courses';
                        $all_courses=$this->CertificateModel->get_data($courses_table);
                        if($all_courses){
                            foreach($all_courses as $courses){
                        ?>
                        <option value="<?php echo $courses['course']; ?>" <?php if($active_course_id == $courses['id']){echo "selected";}?>><?php echo $courses['course']; echo " "; echo $courses['subject_stream'];?></option> 
                        <?php
                            }
                        }
                        ?>
                    </select>
                </div>
                
                <div class="form-group fgwf">
                    <label>Document Type</label>
                    <select class="form-control" name="doc_type">
                        <option value="Student-Bonafied-Certificate">Student Bonafied Certificate</option>
                    </select>
                </div>
                <div class="form-group fgwf">
                    <label>Document Copy</label>
                    <select class="form-control" name="doc_copy_order">
                        <option value="<?php echo $doc_count;?>"><?php echo $doc_count;?> Copy</option>
                    </select>
                </div>
                    
                <div class="form-group fgwf">
                    <label>D.O.B. :</label>
                    <input type="date" name="dob" value="<?php echo $dob; ?>" class="form-control" required> 
                </div>
                 
                 <div class="form-group fgwf">
                     <label>D.O.B. in words</label>
                     <input type="text" name="dob_in_words" class="form-control" value="<?php echo $dob_in_words; ?>" placeholder="Date of birth in words">
                 </div>
                <div class="form-group fgwf">
                    <label>Issuing Date</label>
                    <input type="date" class="form-control" name="issuing_date" value="<?php if($issuing_date !=''){ echo date('Y-m-d',strtotime($issuing_date)); }else{ echo date('Y-m-d'); } ?>" required>
                </div> 
                
                <div class="form-group fgwf">
                    <label>Acedemic Session</label>
                    <select class="form-control" name="acedemic_session" required>
                        <?php
                        $acedemic_session_table='academic_session_details';
                        $all_ac_sessions=$this->CertificateModel->get_data($acedemic_session_table);
                        if($all_ac_sessions){
                            foreach($all_ac_sessions as $ac_sessions){
                        ?>
                        <option value="<?php echo $ac_sessions['id'];?>" <?php if($ac_sessions['id'] == $acedemic_session){echo "selected";}?>><?php echo $ac_sessions['year_long'];?></option>
                        <?php
                            }
                        }
                        ?>
                    </select>
                </div>
                
                <div class="form-group fgwf">
                    <label>Doc. Fee</label>
                    <input type="number" class="form-control" name="doc_fee" placeholder="Doc. Fee" value="<?php echo $doc_fee; ?>" required>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-sm btn-info" name="issue_student_bonafied_certificate"><?php echo $page_title; ?></button>
                    
                </div>        
            </form>
        </div>
    </div>
</div>
<?php
 
?>