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

if ( $boolean_login ){
    $status             = '303' ;
    
    $boolean_permission = false ;
    if ( $staff_info['staff_tier_is_task'] == 'yes' ){
        $boolean_permission = true ;
    }

    if ( $boolean_permission ){
        $status             = '300' ;

        $push_staffid       = [] ;
        $is_updated         = false ;

        $task_id            = $array['task_id'] ;
        $joinstaffs         = ( checkExists( $array['joinstaffs'] ) ? $array['joinstaffs'] : [] ) ;

        if ( $array['task_type'] != '' && $array['title'] != '' && $array['difficulty'] != '' && $array['department_id'] != '' && $array['date_start'] != '' && $array['date_end'] != '' ){
            $status         = '260' ;

            if ( $array['assigned_by'] > 0 || count( $joinstaffs ) > 0 ){
                $status          = '303' ;

                $staff_tier      = [] ;
                $staff_tier      = getRelatedTierID( 'no', $staff_info['staff_tier_level'] ) ;


                $related_staffs = [] ;
                if ( $array['assigned_by'] > 0 ){
                    $related_staffs[$array['assigned_by']] = $array['assigned_by'] ;
                }
                if ( count( $joinstaffs ) > 0 ){
                    foreach ( $joinstaffs as $k => $v ){
                        $related_staffs[$v['id']] = $v['id'] ;
                    }
                }

                $select_staff   = $mysqli->query( "SELECT a.staff_id, a.staff_idno, a.staff_name, a.staff_shortname, a.staff_tier FROM staff a
                                                   WHERE a.deleted_at IS NULL AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL ) AND staff_id IN ( '".implode("', '", $related_staffs)."' ) AND staff_tier IN ( '".implode("', '", $staff_tier)."' )" ) ;

                if ( $select_staff->num_rows == count( $related_staffs ) ){

                    $status          = '201' ;

                    $count_todo_list = 0 ;
                    $count_todo_done = 0 ;
                    if ( checkExists($array['todo_list']) ){
                        if ( count($array['todo_list']) > 0 ){
                            foreach ( $array['todo_list'] as $k => $v ){
                                if ( $v['is_delete'] == 'no' ){
                                    $count_todo_list++ ;

                                    if ( $v['is_done'] == 'yes' ){
                                        $count_todo_done++ ;
                                    }
                                }
                            }
                        }
                    }

                    // check update status
                    $task_status     = 'pending' ;
                    if ( $array['assigned_by'] > 0 && count( $joinstaffs ) > 0 ){
                        $task_status = 'assigned' ;
                    }

                    // check if task id exsits
                    $boolean_submit  = false ;
                    if ( $task_id > 0 ){

                        $select = $mysqli->query( "SELECT * FROM task
                                                   WHERE deleted_at IS NULL AND task_id = '".$task_id."' AND created_by = '".$staff_info['staff_id']."' AND status IN ( 'pending', 'assigned', 'resubmit', 'progress', 'completed', 'confirmed' ) LIMIT 1" ) ;
                        if ( $select->num_rows > 0 ){
                            $status  = '202' ;

                            $row     = $select->fetch_assoc() ;

                            if ( $mysqli->query( "UPDATE task SET
                                                  task_type      = '".$array['task_type']."',
                                                  title          = '".$array['title']."',
                                                  department_id  = '".$array['department_id']."',
                                                  assigned_by    = '".$array['assigned_by']."',
                                                  difficulty     = '".$array['difficulty']."',
                                                  date_start     = '".$array['date_start']."',
                                                  date_end       = '".$array['date_end']."',
                                                  remark         = '".$array['remark']."',
                                                  incentive      = '".$array['incentive']."',
                                                  todo_list      = '".$count_todo_list."',
                                                  todo_done      = '".$count_todo_done."'
                                                  WHERE task_id  = '".$task_id."'" ) ) {
                                $boolean_submit = true ;
                                $is_updated     = true ;

                                $push_staffid[$array['assigned_by']] = $array['assigned_by'] ;
                            }
                        }

                    }else{
                        $status  = '203' ;

                        if ( $mysqli->query( "INSERT INTO task
                                          ( `task_type`, `title`, `department_id`, `created_branch_id`, `created_by`, `assigned_by`, `difficulty`, `date_start`, `date_end`, `remark`, `incentive`, `todo_list`, `todo_done`, `status` ) VALUES
                                          ( '".$array['task_type']."', '".$array['title']."', '".$array['department_id']."', '".$array['branch_id']."', '".$staff_info['staff_id']."', '".$array['assigned_by']."', '".$array['difficulty']."', '".$array['date_start']."', '".$array['date_end']."', '".$array['remark']."', '".$array['incentive']."', '".$count_todo_list."', '".$count_todo_done."', '".$task_status."' )" ) ){
                            $boolean_submit = true ;
                            $task_id        = $mysqli->insert_id ;

                            $task_so        = 'PT'.strPad( 6, $task_id ) ;
                            $mysqli->query( "UPDATE task SET
                                             task_so       = '".$task_so."'
                                             WHERE task_id = '".$task_id."'" ) ;

                            $push_staffid[$staff_info['staff_id']] = $staff_info['staff_id'] ;
                            $push_staffid[$array['assigned_by']]   = $array['assigned_by'] ;
                        }

                    }

                    if ( $boolean_submit ){
                        $status     = '203' ;

                        // deleted all related staff
                        $mysqli->query( "DELETE FROM `task_joinstaff` WHERE task_id = '".$task_id."'" ) ;
                        if ( checkExists($joinstaffs) ){
                            foreach ( $joinstaffs as $k => $v ){
                                $mysqli->query( "INSERT INTO `task_joinstaff` ( `task_id`, `staff_id` ) VALUES ( '".$task_id."', '".$v['id']."' )" ) ;
                                $push_staffid[$v['id']] = $v['id'] ;
                            }
                        }

                        
                        if ( checkExists($array['todo_list']) ){
                            if ( count($array['todo_list']) > 0 ){
                                foreach ( $array['todo_list'] as $k => $v ){
                                    if ( $v['is_delete'] == 'no' ){
                                        if ( $v['id'] > 0 ){
                                            $mysqli->query( "UPDATE task_todo SET title = '".$v['title']."' WHERE todo_id = '".$v['id']."'" ) ;
                                        }else{
                                            $mysqli->query( "INSERT INTO task_todo ( `task_id`, `title`, `sortable` ) VALUES ( '".$task_id."', '".$v['title']."', '9999' )" ) ;
                                        }
                                    }else{
                                        if ( $v['id'] > 0 ){
                                            $mysqli->query( "UPDATE task_todo SET deleted_at    = '".TODAYDATE."' WHERE todo_id = '".$v['id']."'" ) ;
                                        }
                                    }
                                }
                            }
                        }

                        $status      = '200' ;

                        // push to notification
                        foreach ( $push_staffid as $k => $v ){
                            if ( $is_updated ){
                                pushToUserCron( 'task', $task_id, $v, 'Task Updated', 'Task ( '.$row['task_so'].' ) has been updated' ) ;
                            }else{
                                pushToUserCron( 'task', $task_id, $v, 'Task Created', 'New task ( '.$task_so.' ) appoint to you' ) ;
                            }
                        }

                    }
                }

            }
        }

    }
}

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