<div class="row">
    <div class="container">
        <ol class="breadcrumb">
            <li><a href="#">الشئون الادارية</a></li>
            <li><a href="outandin.php?do=show">الصادر والوارد</a></li>
            <li class="active">تعديل</li>
        </ol>
        <div class="panel panel-default panel-m">
            <div class="panel-heading">
                <h3 class="panel-title">تعديل</h3>
            </div>
            <div class="panel-body"> {literal}
                <form class="nameToBeChecked form-horizontal" role="form" action="outandin.php?do=update"
                      method="post" enctype="multipart/form-data" id="spform">
                    {/literal}
                    <!-- =================================================   -->
                        <div class="form-group">
                        <label class="col-lg-2 control-label">النوع</label>
                        <div class="col-lg-6" style="padding-top: 7px;">
                            <label style="font-weight: normal; margin-right: 15px;">
                                <input type="radio" name="type"  value="in" {if $showData->type == in}checked{/if}>وارد 
                            </label>
                            
                            <label style="font-weight: normal; margin-right: 15px;">
                                <input type="radio" name="type"  value="out" {if $showData->type == out}checked{/if}>صادر 
                            </label>
    
                        </div>
                    </div>
                                <!--==============================-->
                                 <div class="form-group" id="date_row" style="display:none;">
                                    <label for="announcementName" class="col-lg-2 control-label">التاريخ</label>
                                    <div class="col-lg-4"> <span id="sprytextfield1">
                                            <input type="date" name="date" id="date" class="form-control" value="{$showData->date}" />
                                            <span class="textfieldRequiredMsg">*</span></span>
                                    </div>
                                </div>
                                <!--==============================-->
                                     <div class="form-group" id="adminstration_row" style="display:none;">
                        <label class="col-lg-2 control-label">الادارة</label>
                        <div class="col-lg-4">
                            <span>
                                <select name="adminstration" id="adminstration" data-placeholder="اختر الادارة" class="select2 wdt100">
                                    <option value="-1">اختر الادارة</option>
                                    {foreach $administrations as $administration}
                                        <option value="{$administration.id}" {if $showData.adminstration == $administration.id }selected{/if} >{$administration.administration}</option>
                                    {/foreach}
                                </select>
                            </span>
                        </div>
                    </div>
                        <!--==============================-->
                                <div class="form-group" id="inside_row" style="display:none;">
                                    <label for="announcementName" class="col-lg-2 control-label" >الجهة المصدر اليها </label>
                                    <div class="col-lg-4"> <span id="sprytextfield1">
                                            <input type="text" name="inside" id="inside" class="form-control" value="{$showData->inside}" />
                                            <span class="textfieldRequiredMsg">*</span></span>
                                    </div>
                                </div>
                                <!--==============================-->
                                 <div class="form-group" id="serial_row" style="display:none;">
                                    <label for="announcementName" class="col-lg-2 control-label">رقم الوارد من الجهة</label>
                                    <div class="col-lg-4"> <span id="sprytextfield1">
                                            <input type="text" name="serial" id="serial" class="form-control" value="{$showData->serial}" />
                                            <span class="textfieldRequiredMsg">*</span></span>
                                    </div>
                                </div>
                                <!--==============================-->
                                   <div class="form-group" id="num_row" style="display:none;">
                                    <label for="announcementName" class="col-lg-2 control-label">عدد المرفقات</label>
                                    <div class="col-lg-1"> <span id="sprytextfield1">
                                            <input type="number" name="num" id="num" class="form-control" value="{$showData->num}" />
                                            <span class="textfieldRequiredMsg">*</span></span>
                                   </div>
                                </div> 
                    <!--==============================-->
                                                    <!--==============================-->
                                  <div class="form-group" id="side_row" style="display:none;">
                        <label class="col-lg-2 control-label">الجهة الوارد منها</label>
                        <div class="col-lg-4">
                            <span>
                                <select name="side" id="side" data-placeholder="اختر الجهة" class="select2 wdt100">
                                    <option value="-1">اختر الجهة</option>
                                    {foreach $sides as $side}
                                        <option value="{$side.side}" {if $showData.side == $side.side }selected{/if} >{$side.side}</option>
                                    {/foreach}
                                </select>
                            </span>
                            <div id="third_input_container" style="display: none; margin-top: 10px;">
                                <input type="text" name="other_side" id="other_side" class="form-control" placeholder="الجهة">
                            </div>
                        </div>
                    </div>
                        <!--==============================-->

                 <div class="form-group" id="subject_row" style="display:none;">
                        <label for="announcementName" class="col-lg-2 control-label" >الموضوع</label>
                        <div class="col-lg-4"> <span id="sprytextfield1">
                               <select name="subject" id="subject" data-placeholder="اختر الموضوع" class="select2 wdt100">
                                            <option value="-1">اختر الموضوع</option>
                                            <option value="other">اخري</option>
                                                {foreach $subjects as $subject}
                                                    <option value="{$subject.subject}" {if $showData->subject == $subject.subject }selected{/if} >{$subject.subject}</option>
                                                {/foreach}
                                </select>            
                            </span>
                             <div id="other_input_container" style="display: none; margin-top: 10px;">
                                <input type="text" name="other_subject" id="other_subject" class="form-control" placeholder="الموضوع">
                            </div>
                        </div>
                    </div>
                    <!--==============================-->

                    <!--==============================-->
                                     <div class="form-group" id="receive_row" style="display:none;">
                        <label for="announcementName" class="col-lg-2 control-label" >المستلم</label>
                        <div class="col-lg-4"> <span id="sprytextfield1">
                                <select name="receiver" id="receiver" data-placeholder="اختر المستلم" class="select2 wdt100">
                                            <option value="-1">اختر المستلم</option>
                                            <option value="other">اخري</option>
                                                {foreach $allNames as $subject}
                                                    <option value="{$subject.name}" {if $showData->receiver == $subject.name }selected{/if} >{$subject.name}</option>
                                                {/foreach}
                                </select>            
                            </span>
                            <div id="sec_input_container" style="display: none; margin-top: 10px;">
                                <input type="text" name="other_receiver" id="other_receiver" class="form-control" placeholder="المستلم">
                            </div>
                        </div>
                    </div>
                        
                    
                     <!--==============================-->
                       <div class="form-group" id="person_row"  style="display:none;">
                        <label for="announcementName" class="col-lg-2 control-label" >الجهة المحول اليها </label>
                        <div class="col-lg-4">
                        <select name="outsideadministration[]" id="outsideadministration" data-placeholder="اختر الادارة" class="select2 wdt100" multiple>
                                                <option value="-1">اختر الادارة</option>
                                                {foreach $administrations as $administration}
                                                    <option value="{$administration.id}" {if $showData->outsideadministration|strpos:$administration.id !== false}selected{/if} >{$administration.administration}</option>
                                                {/foreach}
                                            </select>
                        </div>
                    </div>
                    
                
                    <!--==============================-->
                                                   
                            <div class="form-group">
                                <label for="announcementName" class="col-lg-2 control-label">ملاحظات</label>
                                <div class="col-lg-4"> <span id="sprytextfield1">
                                        <input type="text" name="comment" id="comment" class="form-control"  value="{$showData->comment}"  />
                                        <span class="textfieldRequiredMsg">*</span></span>
                                </div>
                            </div>
                    <!--==============================-->
                     

                      <div class="form-group">                          
                        <label class="col-lg-2 control-label">مرفقات</label>
                        <div class="col-lg-4">
                            {if $showData->images != "" && $showData->images !="." &&
                            $showData->images !="no image"}
                            <div style="margin-bottom: 10px;">
                            {assign var="imgs" value=","|explode:trim($showData->images)}
                            {foreach $imgs as $img}
                                <img src="../upload/outandin/{trim($img)}" width="80" height="60" style="border:1px solid #ccc; margin-left:5px;" />
                                
                            {/foreach}
                            </div>
                            {/if}
                            <input type="file" name="image[]" id="image" multiple  accept=".jpg, .jpeg, .png, .pdf"/>
                            <input type="hidden" name="imageurl" id="imageurl" value="{$showData->images}" />
                        </div>
                    </div>
                <!--==============================-->
                    
                    <input name="id" id="id" type="hidden" value="{$showData->id}" />

                    <!-- =================================================   -->
                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-4">
                            <button type="button" class="btn btn-primary pull-left" id="spbutton">حفظ</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{literal}
<script type="text/javascript">
    var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
    var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
    $('#spbutton').live('click', function (e) {
        if (!$('#spbutton').hasClass("processing")) {
            $('#spbutton').addClass("processing");
            var myForm = document.getElementById("spform");
            var SS = Spry.Widget.Form.validate(myForm);
            if (SS == true) {
                myForm.submit();
            } else {
                $('#spbutton').removeClass("processing");
            }
        }
    });
$(document).ready(function(){
    $('#name').on('change', function() {
        let sideValue = $(this).find(':selected').data('side');
        if (sideValue) {
                $('#side').val(sideValue);
            } else {
                $('#side').val(''); 
            }
        });
})
$(document).ready(function() {
    function toggleTypeFields() {
        var selectedRadio = $('input[name="type"]:checked');
        var selectedType = selectedRadio.val();
        var nextValue = selectedRadio.data('next'); 
        
        if (selectedType === 'out') {
            $('#date_row').show();
            $('#subject_row').show();
            $('#inside_row').show();
            $('#receive_row').show();
            $('#notes_row').show();
            $('#adminstration_row').show();
            $('#serial_row').hide();
            $('#person_row').hide();
            $('#num_row').hide();
            $('#side_row').hide();
            $('#ord').val(nextValue);
            $('#coming').val('');
            
        } else if (selectedType === 'in') {
            $('#notes_row').show();
            $('#serial_row').show();
            $('#num_row').show();
            $('#date_row').show();
            $('#person_row').show();
            $('#subject_row').show();
            $('#receive_row').show();
            $('#side_row').show();
            $('#inside_row').hide();
            $('#adminstration_row').hide();
            $('#coming').val(nextValue);
            $('#ord').val('');
            
        } else {
            $('#ord_row').hide();
            $('#coming_row').hide();
        }
    }

    $('input[name="type"]').on('change', function() {
        toggleTypeFields();
    });

    toggleTypeFields();
});
$(document).ready(function() {
    $('#subject').on('change', function() {
        if ($(this).val() === 'other') {
            $('#other_input_container').show();
        } else {
            $('#other_input_container').hide(); 
            $('#other_subject').val('');
        }
    });
});
$(document).ready(function() {
    $('#receiver').on('change', function() {
        if ($(this).val() === 'other') {
            $('#sec_input_container').show();
        } else {
            $('#sec_input_container').hide(); 
            $('#other_receiver').val('');
        }
    });
});
$(document).ready(function() {
    $('#side').on('change', function() {
        if ($(this).val() === 'other') {
            $('#third_input_container').show();
        } else {
            $('#third_input_container').hide(); 
            $('#other_side').val('');
        }
    });
});
</script>
{/literal}