<?php
/**
 * interface/modules/zend_modules/module/Carecoordination/view/carecoordination/setup/index.phtml
 *
 * @package   OpenEMR
 * @link      https://www.open-emr.org
 * @author    Vinish K <vinish@zhservices.com>
 * @copyright Copyright (c) 2014 Z&H Consultancy Services Private Limited <sam@zhservices.com>
 * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
 */
?>
<script type="text/javascript">
var save_array = new Array();
$(function() {
    $( "ul.droptrue" ).sortable({
            connectWith: "ul",
            placeholder: "ui-state-highlight"
    });
    $( "ul.dropfalse" ).sortable({
            connectWith: "ul",
            dropOnEmpty: false,
            placeholder: "ui-state-highlight"
    });
    $( ".sortable_div" ).disableSelection();
    $( ".sortable_div" ).sortable({
        receive: function( event, ui ) {
            classes = $(ui.item).prev().attr('class');
            classes_arr = classes.split(' ');
            for(key in classes_arr){
                if(classes_arr[key].substr(0, 4) == 'sub_'){
                    $(ui.item).addClass(classes_arr[key]);
                }
            }
            rearrange();
        }
    });
    $("#sortabledrop .delete").click(function() {
        $(this).parent().remove();
        rearrange();
    });
    rearrange();
});

function rearrange(){
    $( "#sortabledrop" ).each(function( index ) {
        var str = '';
        var component = '';
        var section = '';
        $(this).find('li').each(function(){
            if($( this ).attr('id') && ($( this ).attr('id')).match(/\s?ID.*\s?/gi)){
                component = ($( this ).attr('id')).replace('ID','');
                str += '|***|'+component;
                save_array[component] = new Array();
            }
            else if($( this ).attr('class') && ($( this ).attr('class')).match(/\sFIELD.*\s/gi)){
                matched_value = ($( this ).attr('class')).match(/\sFIELD.*\s/gi);
                section = matched_value[0].replace('FIELD','');
                str += '|**|'+section;
                save_array[component][section] = new Array();
            }
            else if($( this ).attr('class') && ($( this ).attr('class')).match(/\s[1|2|3|4].*\s/gi)){
                form = ($( this ).attr('class')).match(/\s[1|2|3|4].*\s/gi);
                str += '|*|'+form;
                save_array[component][section] = form;
            }
        });
        document.getElementById('tosave').value = str;
    });
}

$(function() {
    $( "#tabs" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
    $( "#tabs li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );

    $('.sortable_li').click(function(){
        $('#sub_'+this.id).toggle('slow');
    });

    $( "#sortabledrop" ).sortable({
        cancel      : ".ui-state-disabled",
        items       : "li:not(.ui-state-disabled-complete)",
        placeholder : "ui-state-highlight"
    });

    $('#system_based_forms').addClass("activeTabForm");
    $('.top-menu li').click(function(){
        $('.top-menu li').removeClass("activeTabForm");
        $('.all-form').hide();
        $('#tabs-'+$(this).attr("id")).fadeIn(100).addClass("activeWindowForm");
        $(this).addClass("activeTabForm");

    });
});
</script>
<ul class="top-menu top-menu-carecoordination">
           <?php
            foreach($this->menu as $key => $value){
                $class_name = $key."_class";
            ?>
		<li class="form-title" id="<?php echo $this->escapeHtml($key); ?>"><?php echo $this->listenerObject->z_xlt($value);?></li>
            <?php
            }
            ?>
 </ul>
<div class="clear"></div>
<div class="ca-ca-in-1">
    <div class="ca-ca-in-3">
    <?php
    foreach($this->menu as $key => $value){
        $class_name = $key."_class";
    ?>
    <div id = 'tabs-<?php echo $this->escapeHtml($key);?>' class="all-form">
        <div id = "paginatediv_<?php echo $this->escapeHtml($key);?>" class = "paginationstyle">
            <a href="#" rel="previous" style="float:left;"><img src="<?php echo $this->basePath().'/images/prev.png' ?>" alt="" /></a>
            <a href="#" rel="next" style="float:right;"><img src="<?php echo $this->basePath().'/images/next.png' ?>" alt="" /></a>
        </div>
        <ul class='virtualpage_<?php echo $this->escapeHtml($key);?> sortable_div droptrue <?php if(is_array($rowforms[1])) //echo "navigation";?>'>
        <?php
        $per_page = 13;
        $count = 0;
        foreach($this->$key as $rowforms){
            if($count >= $per_page){
                if($count != 0){
                ?>
                    </ul>
                    <?php
                    $count = 0;
                    }
                    ?>
                    <ul class='virtualpage_<?php echo $this->escapeHtml($key);?> sortable_div droptrue <?php if(is_array($rowforms[1])) //echo "navigation";?>'>
                    <?php
                }
                ?>
                <li class='ui-state-default sortable_li <?php echo $this->escapeHtml($rowforms[1]);?>' id='<?php echo preg_replace('/\s/','_',$this->escapeHtml($rowforms[0]));?>'>
                    <?php echo $rowforms[0];
                    if(is_array($rowforms[2])){
                        ?>
                        <ul class='droptrue' style='display: none;' id='sub_<?php echo preg_replace('/\s/','_',$this->escapeHtml($rowforms[0]));?>'>
                            <?php
                            foreach($rowforms[2] as $key_sub => $value_sub){
                            ?>
                            <li class='ui-state-default <?php echo $this->escapeHtml($rowforms[2][$key_sub][1]);?>'><?php echo $this->listenerObject->z_xlt($rowforms[2][$key_sub][0]);?></li>
                            <?php
                            }
                            ?>
                        </ul>
                        <?php
                    }
                    ?>
                </li>
                <?php
                $count++;
            }
            ?>
        </div>
        <script type="text/javascript">
            var pagecontent = new virtualpaginate({
                piececlass      : "virtualpage_<?php echo $this->escapeHtml($key);?>", //class of container for each piece of content
                piececontainer  : "ul",                             //container element type (ie: "div", "p" etc)
                pieces_per_page : 2,                                //Pieces of content to show per page (1=1 piece, 2=2 pieces etc)
                defaultpage     : 0,                                //Default page selected (0=1st page, 1=2nd page etc). Persistence if enabled overrides this setting.
                wraparound      : false,
                persist         : false                             //Remember last viewed page and recall it when user returns within a browser session?
            })
            pagecontent.buildpagination(["paginatediv_<?php echo $this->escapeHtml($key);?>"])
        </script>
        <?php
        }
        ?>
    <div class="clear"></div>
    </div>
</div> <!-- ca-ca-in-1 -->

<div class="ca-ca-in-2">
    <div>
        <div class="dropable_area">
            <ul id="sortabledrop" class='sortable_div dropfalse' style="font-size:12px;">
                <?php
                $prev = '';
                $count = 0;
                foreach($this->sections as $row){
                    $completed = '';
                    if($prev != $row['ccda_components_field']){
                        $completed = ' ui-state-disabled-complete ';
                        if($count > 0){
                        ?>
                        <li class="ui-state-default ui-state-disabled sortable_li" style="visibility: hidden; line-height: 1px; padding: 0px !important;">
                            &nbsp;
                        </li>
                        <?php
                        }
                        ?>
                        <li class="ui-state-default ui-state-disabled sortable_li <?php echo $completed;?>" style="color: #f70428 !important;" id="ID<?php echo $this->escapeHtml($row['ccda_components_field']);?>">
                            <?php echo $this->listenerObject->z_xlt($row['ccda_components_name']);?>
                        </li>
                        <script type="text/javascript">
                            $('#ID<?php echo $this->escapeHtml($row['ccda_components_field']);?>').click(function(){
                                $('.sub_<?php echo $this->escapeHtml($row['ccda_components_field']);?>').toggle('slow');
                            });
                        </script>
                        <?php
                        $count++;
                    }
                    ?>
                    <li class="ui-state-default ui-state-disabled <?php echo $completed;?> <?php echo "sub_".$this->escapeHtml($row['ccda_components_field']);?> <?php echo "FIELD".$this->escapeHtml($row['ccda_sections_field']) ;?> child-drop-area" style="color: #000000 !important; display: none;">
                        <?php echo $this->listenerObject->z_xlt($row['ccda_sections_name']);?>
                    </li>
                    <?php
                    if($this->saved[$row['ccda_components_field']][$row['ccda_sections_field']]){
                        foreach($this->saved[$row['ccda_components_field']][$row['ccda_sections_field']] as $key_1 => $value_1){
                            if(!$this->listenerObject->z_xlt($this->saved[$row['ccda_components_field']][$row['ccda_sections_field']][$key_1]['name'])) continue;
                        ?>
                        <li class="ui-state-default sortable_li <?php echo "sub_".$this->escapeHtml($row['ccda_components_field']);?> <?php echo $this->escapeHtml($this->saved[$row['ccda_components_field']][$row['ccda_sections_field']][$key_1]['class']);?> " style="display: none">
                            <?php
                            echo $this->listenerObject->z_xlt($this->saved[$row['ccda_components_field']][$row['ccda_sections_field']][$key_1]['name']);
                            ?>
                            <img class="delete" height="15px" width="15px" src="<?php echo $this->basePath() . '/images/delete1.png';?>" style="float:right; margin-top:1px;">
                        </li>
                        <?php
                        }
                    }
                    ?>
                    <?php
                    $prev = $row['ccda_components_field'];
                }
                ?>
            </ul>
        </div>
    </div>
    <div class="clearfix"></div>
</div>
<div class="clear"></div>
<form method="post" action="<?php echo $this->basePath() . '/carecoordination/setup/savedata';?>">
    <div style="width:100%; text-align:center;">
        <input style="position: absolute;top: 480px;" type="submit" name="save" value="<?php echo $this->listenerObject->z_xla("Save");?>">
    </div>
    <input type="hidden" id="tosave" name="tosave" value="">
</form>
