<?php
$must_login              = true ;
$require_path            = '../../../' ;
$require_sub             = '../../' ;
require( $require_sub.'header.php' ) ;

$status                  = '300' ;

$branch_id               = $array['branch_id'] ;
$branch_name             = $array['branch_name'] ;
$staff_idno              = $array['idno'] ;
$staff_name              = $array['name'] ;
$staff_email             = $array['email'] ;
$staff_mobileno          = $array['mobile'] ;

$department_id           = '5' ;

if ( $branch_id != '' && $staff_idno != '' && $staff_name != '' ){
    $status              = '244' ;

    // check branch id exists or not first
    $select_branch       = $mysqli->query( "SELECT * FROM branch WHERE branch_id = '".$branch_id."' LIMIT 1" ) ;
    if ( $select_branch->num_rows == 0 ){
        if ( $branch_name != '' ){
            $mysqli->query( "INSERT INTO branch
                             ( branch_id, branch_name, created_at, updated_at ) VALUES
                             ( '".$branch_id."', '".$branch_name."', '".TODAYDATE."', '".TODAYDATE."' )" ) ;
        }
    }

    // check staffidno exists
    $staff_id            = '' ;
    $select_staff        = $mysqli->query( "SELECT staff_id FROM staff 
                                            WHERE deleted_at IS NULL AND staff_idno = '".$staff_idno."' LIMIT 1" ) ;
    if ( $select_staff->num_rows == 0 ){
        $status          = '203' ;

        if ( $mysqli->query( "INSERT INTO staff
                              ( branch_id, staff_idno, staff_name, staff_email, staff_mobileno, staff_tier ) VALUES
                              ( '".$branch_id."', '".$staff_idno."', '".$staff_name."', '".$staff_email."', '".$staff_mobileno."', '9' )" ) ){
            $staff_id    = $mysqli->insert_id ;

            $status      = '200' ;
        }
    }else{
        $status          = '202' ;

        $row_staff       = $select_staff->fetch_assoc() ;

        if ( $mysqli->query( "UPDATE staff SET
                              branch_id      = '".$branch_id."',
                              staff_name     = '".$staff_name."',
                              staff_email    = '".$staff_email."',
                              staff_mobileno = '".$staff_mobileno."'
                              WHERE staff_id = '".$row_staff['staff_id']."'" ) ){

            $staff_id    = $row_staff['staff_id'] ;

            $status      = '200' ;
        }
    }

    if ( $staff_id != '' ){
        $select_department   = $mysqli->query( "SELECT staff_department_id FROM staff_department 
                                                WHERE deleted_at IS NULL AND staff_id = '".$staff_id."' LIMIT 1" ) ;
        if ( $select_department->num_rows == 0 ){
            $mysqli->query("INSERT INTO staff_department
                            ( staff_id, department_id, created_at, updated_at ) VALUES
                            ( '".$staff_id."', '".$department_id."', '".TODAYDATE."', '".TODAYDATE."')" ) ;
        }
    }

}

require( $require_sub.'footer.php' ) ;
?>