<?php
global $current_user, $wpdb;
$wpttarUserRole = $this->WPTTARGetCurrentUserCall();
$projectID = '';
if (isset($_REQUEST['projectID']) && !empty($_REQUEST['projectID'])) :
    $projectID = $_REQUEST['projectID'];
    $edit_mode = true;
    $wpttarTableResults = $this->WPTTARGetTableDataByID($projectID, $this->WpttarProjectsTable, 'id');
    if (!empty($wpttarTableResults)) {
        $developersname = $this->WPTTARSetUnSerialize($wpttarTableResults->members);
    }
    $wpttarDocuments = $this->WPTTARGetTableDataByID($projectID, $this->WpttarAttachmentsTable, 'projectID');
    if (!empty($wpttarDocuments)) {
        $attechmentArray = $this->WPTTARSetUnSerialize($wpttarDocuments->attachmentLink);
        $documentnameArray = $this->WPTTARSetUnSerialize($wpttarDocuments->documentName);
    }
endif;
if (isset($projectID) && $projectID != '') {
    $wpttarMessagesProjectSettings = $this->WPTTARGetMessage(4);
} else {
    $wpttarMessagesProjectSettings = $this->WPTTARGetMessage(3);
}
$wpttarTeamMemberInfo = $this->WPTTARGetSettings('wpttarTeamMemberInfo');
?>
<div class="preloader">  <span class="preloader-custom-gif"></span></div>
<input type="hidden" id="current-role-member" value="<?php echo esc_attr($wpttarUserRole); ?>">
<div style="padding-left: 0px; padding-right:20px; padding-top:0px;" class="col-md-12">
    <h3 class="page-title"><?php
        if (isset($projectID) && $projectID != '') {
            _e('Edit Project', 'wpttar');
        } else {
            _e('Add Project', 'wpttar');
        }
        ?></h3>
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li>
            <li><a href="?page=wpttar-projects"><i class="fa fa-home"></i><?php _e('Home', 'wpttar'); ?><i class="fa fa-angle-right"></i></a></li>
            </li>
            <li><?php
                if (isset($projectID) && $projectID != '') {
                    _e('Edit Project', 'wpttar');
                } else {
                    _e('Add Project', 'wpttar');
                }
                ?></li>
        </ul>   
    </div>
    <div class="row">
        <div class="col-md-12">
            <a class="btn green " href="?page=wpttar-projects"><?php _e('Back', 'wpttar'); ?><i class="fa fa-arrow-left"></i></a>
            <!-- BEGIN VALIDATION STATES-->
            <div class="portlet box green">
                <div class="portlet-title">
                    <div class="caption">
                        <?php if (isset($projectID) && $projectID != '') { ?>
                            <i class="fa fa-edit"></i><?php _e('Edit Project', 'wpttar'); ?>

                        <?php } else { ?>
                            <i class="fa fa-adn"></i><?php _e('Add Project', 'wpttar'); ?>     
                        <?php } ?>
                    </div>
                </div>
                <div class="portlet-body form">
                    <form action="#" id="wpttar-project-form" class="form-horizontal ">
                        <div id="static"  class="modal fade bs-modal-lg in" tabindex="-1" data-backdrop="static" data-keyboard="false">
                            <div class="modal-dialog modal-lg">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                                        <h4 class="modal-title"><strong><?php _e('Add Documents', 'wpttar'); ?></strong></h4>
                                    </div>
                                    <div class="modal-body">
                                        <div class="portlet-body form">
                                            <div class="form-body">
                                                <div class="alert alert-danger display-hide" id="document-validation-error">
                                                    
                                                    <button class="close" data-close="alert"></button>
                                                </div>
                                                <?php
                                                $i = 1;
                                                if (!empty($documentnameArray['documentlink'])) {
                                                    foreach ($documentnameArray['documentlink'] as $keylink => $attechmentlink) {
                                                        foreach ($documentnameArray['documentName'] as $keyname => $documentName) {
                                                            foreach ($documentnameArray['documenturl'] as $keyurl => $dacumenturl) {
                                                                $keyValue = get_post_meta($attechmentlink, '_wp_attached_file', true);
                                                                if ($keyname == $keyurl && $keylink == $keyurl) {
                                                                    ?>
                                                                    <div class="form-group" id="new-documentation<?php echo esc_attr($i) ?>"><div class="col-md-2">
                                                                            <input  type="text" name="documentsname[]" id="documentsname0" placeholder="<?php _e('Documents Name', 'wpttar'); ?>" value="<?php echo esc_attr($documentName); ?>" class="form-control"/></div>
                                                                        <div class="col-md-3">
                                                                            <input  type="text" class="form-control" name="documenturl[]" id="documenturl0" placeholder="<?php _e('Documents Url', 'wpttar'); ?>" value="<?php echo esc_attr($dacumenturl); ?>"/></div>
                                                                        <div class="col-md-6">
                                                                            <div data-provides="fileinput" class="fileinput fileinput-new">
                                                                                <div class="input-group input-large">
                                                                                    <div data-trigger="fileinput" class="form-control uneditable-input">
                                                                                        <i class="fa fa-file fileinput-exists"></i>&nbsp; 

                                                                                        <span class="fileinput-filename"><?php
                                                                                            if (isset($keyValue) && $keyValue != '') :
                                                                                                $keyValueExt = pathinfo($keyValue, PATHINFO_EXTENSION);
                                                                                                echo esc_html(mb_strimwidth($keyValue, 0, 10) . '....' . $keyValueExt);
                                                                                            endif;
                                                                                            ?></span>

                                                                                    </div>
                                                                                    <span class="input-group-addon btn default btn-file">
                                                                                        <span class="fileinput-new">
                                                                                            <?php _e('Select file', 'wpttar'); ?> </span>
                                                                                        <span class="fileinput-exists">
                                                                                            <?php _e("Change", "wpttar"); ?> </span>
                                                                                        <input type="file" class="document-validation"  name="wpttar-project-document[]" value="">
                                                                                 
                                                                                    </span>
                                                                                    <a data-dismiss="fileinput" class="input-group-addon btn red fileinput-exists" href="#">
                                                                                        <?php _e('Remove', 'wpttar'); ?> </a>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                        <div class="col-md-1">
                                                                            <a href="#" class="btn red" onclick="WPTTARDeleteProjectDocuments('<?php
                                                                            if (isset($projectID)) : echo esc_js($projectID);
                                                                            endif;
                                                                            ?>', '<?php
                                                                            if (isset($attechmentlink)) : echo esc_js($attechmentlink);
                                                                            endif;
                                                                            ?>', '<?php echo esc_js($i) ?>');"><i class="fa fa-trash" aria-hidden="true"></i></a>
                                                                        </div></div>
                                                                    <?php
                                                                }
                                                            }
                                                            $i++;
                                                        }
                                                    }
                                                }else {
                                                    ?>
                                                    <div class="form-group" id="new-documentation"><div class="col-md-2">
                                                            <input  type="text" name="documentsname[]" id="documentsname0" placeholder="<?php _e('Documents Name', 'wpttar'); ?>" class="form-control"/></div>
                                                        <div class="col-md-3">
                                                            <input  type="text" class="form-control" id="documenturl0" name="documenturl[]" placeholder="<?php _e('Documents Url', 'wpttar'); ?>" /></div>
                                                        <div class="col-md-6">
                                                            <div data-provides="fileinput" class="fileinput fileinput-new">
                                                                <div class="input-group input-large">
                                                                    <div data-trigger="fileinput" class="form-control uneditable-input">
                                                                        <i class="fa fa-file fileinput-exists"></i>&nbsp; <span class="fileinput-filename"></span>
                                                                    </div>
                                                                    <span class="input-group-addon btn default btn-file">
                                                                        <span class="fileinput-new">
                                                                            <?php _e('Select file', 'wpttar'); ?> </span>
                                                                        <span class="fileinput-exists">
                                                                            <?php _e("Change", "wpttar"); ?> </span>
                                                                        <input type="file"  class="document-validation" name="wpttar-project-document[]">
                                                                    </span>
                                                                    <a data-dismiss="fileinput" class="input-group-addon btn red fileinput-exists" href="#">
                                                                        <?php _e('Remove', 'wpttar'); ?> </a>
                                                                </div>
                                                            </div>
                                                        </div>
                                                        <div class="col-md-1">
                                                            <a href="#" class="btn red" onclick="WPTTARDeleteProjectDocuments('<?php
                                                            if (isset($projectID)) : echo esc_js($projectID);
                                                            endif;
                                                            ?>', '<?php
                                                            if (isset($attechmentlink)) : echo esc_js($attechmentlink);
                                                            endif;
                                                            ?>', '<?php echo esc_js($i) ?>');"><i class="fa fa-trash" aria-hidden="true"></i></a>
                                                        </div> </div>
                                                <?php }
                                                ?>

                                                <div id="add-project-documents"></div>  
                                                <button type="button" id="add-project-documents-file" class="btn btn-sm green form-control-inline add-project-documents-file">
                                                    <i class="fa fa-plus"></i><?php _e('Add More', 'wpttar'); ?>
                                                </button>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button"  class=" btn blue" id="add-document-submit"> <i class="fa fa-plus"></i><?php _e('Add', 'wpttar'); ?></button>
                                                <button type="button" data-dismiss="modal" id="dismiss" aria-hidden="true" class="btn red"><i class="fa fa-times"></i><?php _e('Cancel', 'wpttar'); ?></button>
                                            </div>

                                        </div>
                                    </div>
                                </div></div></div>


                        <input name="wpttar-project-id" type="hidden" id="wpttar-project-id" value="<?php
                        if (isset($projectID)) : echo esc_attr($projectID);
                        endif;
                        ?>" />
                        <div class="form-body">
                            <div class="alert alert-success display-hide" id="add-project-success"><i class='fa fa-check-square' aria-hidden='true'></i> <?php echo esc_html($wpttarMessagesProjectSettings); ?>
                                <button class="close" data-close="alert"></button>
                            </div>
                            <div class="alert alert-danger display-hide" id="add-project-error">
                                <button class="close" data-close="alert"></button>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-3"><?php _e('Project Name', 'wpttar'); ?></label>
                                <div class="col-md-8">
                                    <input name="wpttar-project-name" type="text" id="wpttar-project-name" value="<?php
                                    if (isset($wpttarTableResults->projectName)) : echo esc_attr($wpttarTableResults->projectName);
                                    endif;
                                    ?>" class="form-control" required/>
                                </div>
                            </div> 
                            <div class="form-group">
                                <label class="control-label col-md-3"><?php _e('Client Name', 'wpttar');
                                    ?></label>
                                <div class="col-md-8">
                                    <input  name="wpttar-client-name" type="text" id="wpttar-client-name" value="<?php
                                    if (isset($wpttarTableResults->clientName)) : echo esc_attr($wpttarTableResults->clientName);
                                    endif;
                                    ?>" class="form-control" required/>
                                </div>
                            </div>
                           
                            <div class="form-group">
                                <label class="control-label col-md-3"><?php _e('Team Members', 'wpttar') ?></label>
                                <div class="col-md-8">
                                    <p><select id="my-select" name="wpttar-assign-developers-name[]" id="wpttar-assign-developers-name" multiple="multiple" required="required">
                                            <?php
                                            $wpttarGetUser = get_users();
                                            if (!empty($projectID)) {
                                                if (!empty($wpttarGetUser)) {
                                                    foreach ($wpttarGetUser as $key => $userdata) {
                                                        $role_check = count($userdata->roles);
                                                        $wpttarUserRoles = implode(',', $userdata->roles);
                                                        if($role_check > 1){
                                                             $wpttarUserRole_1 = explode(',', $wpttarUserRoles);
                                                             $wpttarUserRole = $wpttarUserRole_1[0];
                                                        }else{
                                                            $wpttarUserRole =$wpttarUserRoles;
                                                        }
                                                       if(in_array($wpttarUserRole, $wpttarTeamMemberInfo)){
                                                        if (in_array($userdata->ID, $developersname)) {
                                                            ?> <option value="<?php
                                                            if (isset($userdata->ID)): echo esc_attr($userdata->ID);
                                                            endif;
                                                            ?>" selected="selected"><?php
                                                                        if (isset($userdata->user_nicename)): echo esc_html($userdata->user_nicename);
                                                                        endif;
                                                                        ?></option>    

                                                        <?php }else { ?><option value="<?php
                                                            if (isset($userdata->ID)): echo esc_attr($userdata->ID);
                                                            endif;
                                                            ?>"><?php
                                                                        if (isset($userdata->user_nicename)): echo esc_html($userdata->user_nicename);
                                                                        endif;
                                                                        ?></option>    
                                                            <?php
                                                        }
                                                        }
                                                    }
                                                }
                                            }else {
                                                foreach ($wpttarGetUser as $key => $userdata) {

                                                 if(isset($userdata->roles) && !empty($userdata->roles)){
                                                   $wpttarUserRole  = implode(',', $userdata->roles);
                                                    if((isset($wpttarUserRole) && !empty($wpttarUserRole)) && (isset($wpttarTeamMemberInfo) && !empty($wpttarTeamMemberInfo))){
                                                    if(in_array($wpttarUserRole, $wpttarTeamMemberInfo)){
                                                    ?> <option value="<?php
                                                        if (isset($userdata->ID)): echo esc_attr($userdata->ID);
                                                        endif;
                                                        ?>"><?php
                                                            if (isset($userdata->user_nicename)): echo esc_html($userdata->user_nicename);
                                                            endif;
                                                            ?></option>    

                                                    <?php
                                                }
                                                }
                                                 }
                                                  }
                                                
                                            }
                                            ?>  
                                        </select></p>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-md-3"><?php _e('Dev. Server Details', 'wpttar') ?></label>
                                <div class="col-md-8">
                                    <?php
                                    if (isset($wpttarTableResults->description) && $wpttarTableResults->description != '') {
                                        $desc = $wpttarTableResults->description;
                                    } else {
                                        $desc = '';
                                    }
                                    wp_editor(stripslashes($desc), 'wpttar-project-dev-server-details', array('media_buttons' => false, 'editor_height' => 100, 'editor_width' => 100));
                                    ?>
                                </div>
                            </div>
                            <?php if (isset($projectID) && $projectID != '') { ?>
                                <div class="form-group">
                                    <label class="control-label col-md-3"><?php _e('Project Status', 'wpttar') ?></label>
                                    <div class="col-md-8">
                                        <select class="form-control" name="wpttar-project-status" required="required">
                                            <option value=""><?php _e('Select Status', 'wpttar') ?></option>
                                            <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Open') { ?>
                                           <option <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Open') { ?> selected="selected" <?php } ?> value="<?php _e('Open', 'wpttar') ?>"><?php _e('Open', 'wpttar') ?></option>
                                           <option value="<?php _e('Close', 'wpttar') ?>"><?php _e('Close', 'wpttar') ?></option>
                                            <?php }else ?>
                                             <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Close') { ?>
                                            <option <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Close') { ?> selected="selected" <?php } ?> value="<?php _e('Close', 'wpttar') ?>"><?php _e('Close', 'wpttar') ?></option>
                                        <option value="<?php _e('Reopen', 'wpttar') ?>"><?php _e('Reopen', 'wpttar') ?></option>
                                             <?php }else ?>    
                                           <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Reopen') { ?>
                                       <option <?php if (isset($wpttarTableResults->projectStatus) && $wpttarTableResults->projectStatus == 'Reopen') { ?> selected="selected" <?php } ?> value="<?php _e('Reopen', 'wpttar') ?>"><?php _e('Reopen', 'wpttar') ?></option>
                                       <option value="<?php _e('Close', 'wpttar') ?>"><?php _e('Close', 'wpttar') ?></option>
                                           <?php } ?>
                                        </select>
                                    </div>
                                </div>
                            <?php } ?>
                            <?php
                            $i = 1;
                            if (!empty($documentnameArray['documentlink'])) {
                                foreach ($documentnameArray['documentlink'] as $keylink => $attechmentlink) {
                                    foreach ($documentnameArray['documentName'] as $keyname => $documentName) {
                                        foreach ($documentnameArray['documenturl'] as $keyurl => $dacumenturl) {
                                            $keyValue = get_post_meta($attechmentlink, '_wp_attached_file', true);
                                            if ($keyname == $keyurl && $keylink == $keyurl) {
                                                ?>
                                                <div class="form-group documentarraydatahide"><label class="control-label col-md-3"><?php echo esc_html($documentName); ?></label> 
                                                    <div class="col-md-3"><input  type="text" readonly placeholder="<?php _e('Documents Url', 'wpttar'); ?>" id="documents-url" value="<?php echo esc_attr($dacumenturl); ?>" class="form-control"/>
                                                    </div>
                                                    <div class="col-md-5"><div data-provides="fileinput" class="fileinput fileinput-new">
                                                            <div class="input-group input-large"><div data-trigger="fileinput" class="form-control uneditable-input">
                                                                    <i class="fa fa-file fileinput-exists"></i>&nbsp; <span class="fileinput-filename">
                                                                        <?php
                                                                        if (isset($keyValue) && $keyValue != '') :
                                                                            $resumeExt = pathinfo($keyValue, PATHINFO_EXTENSION);
                                                                            echo esc_html(mb_strimwidth($keyValue, 0, 10) . '....' . $resumeExt);
                                                                        endif;
                                                                        ?></span></div>
                                                            </div></div></div>
                                                </div>
                                                <?php
                                            }
                                        }
                                    }
                                }
                            }
                            ?>
                            <div id="documents-full-array"></div>
                            <div class="form-group" style="text-align: right;" >
                                <label class="control-label col-md-3"></label>
                                <div class="col-md-8">
                                    <button href="#static" data-toggle="modal" type="button" class="btn btn-sm green form-control-inline"  style="text-align: center;">
                                        <i class="fa fa-plus"></i><?php _e('Add Documents', 'wpttar'); ?>
                                    </button>
                                </div></div>



                            <div class="form-actions">
                                <div class="row">
                                    <div class="col-md-offset-3 col-md-9">
                                        <?php if (isset($projectID) && $projectID == '') { ?>
                                            <button type="submit"  class="btn green"><?php _e('Add Project', 'wpttar'); ?></button>
                                        <?php } else { ?>
                                            <button type="submit" class="btn green"><?php _e('Edit Project', 'wpttar'); ?></button>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        </div>



                        <!-- END VALIDATION STATES-->
                    </form>
                    <!-- END FORM-->
                </div>
            </div>
        </div>
    </div>
</div>
<!-- END PAGE CONTENT