<ol class="breadcrumb">
    <li><a href="accSetupController.php">نظام المحاسبة</a></li>
    <li><a href="accAccountController.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">
        <form action="accAccountController.php?do=update" method="post" class="form-horizontal">
            <input type="hidden" name="id" value="{$account.id}">

            <div class="form-group">
                <label class="col-lg-2 control-label">كود الحساب</label>
                <div class="col-lg-4">
                    <input type="text" name="code" class="form-control" value="{$account.code}" required>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">اسم الحساب</label>
                <div class="col-lg-4">
                    <input type="text" name="name" class="form-control" value="{$account.name}" required>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">الحساب الأب</label>
                <div class="col-lg-4">
                    <select name="parent_id" class="select2 wdt100" data-placeholder="اختر">
                        <option value=""></option>
                        {foreach $parentAccounts as $pa}
                        <option value="{$pa.id}" {if $pa.id == $account.parent_id}selected{/if}>{$pa.code} - {$pa.name}</option>
                        {/foreach}
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">طبيعة الحساب</label>
                <div class="col-lg-4">
                    <label class="radio-inline">
                        <input type="radio" name="account_nature" value="debit" {if $account.account_nature == 'debit'}checked{/if}> مدين
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="account_nature" value="credit" {if $account.account_nature == 'credit'}checked{/if}> دائن
                    </label>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">نوع الحساب</label>
                <div class="col-lg-4">
                    <select name="account_type" class="form-control" required>
                        <option value="">اختر</option>
                        <option value="assets" {if $account.account_type == 'assets'}selected{/if}>أصول</option>
                        <option value="liabilities" {if $account.account_type == 'liabilities'}selected{/if}>خصوم</option>
                        <option value="equity" {if $account.account_type == 'equity'}selected{/if}>حقوق ملكية</option>
                        <option value="revenue" {if $account.account_type == 'revenue'}selected{/if}>إيرادات</option>
                        <option value="expenses" {if $account.account_type == 'expenses'}selected{/if}>مصروفات</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">ربط بعضو</label>
                <div class="col-lg-4">
                    <input type="hidden" name="member_id" id="member_id" style="width:100%" />
                    <span class="help-block" id="memberHint" style="color:#e74c3c;font-size:12px;display:none;"></span>
                </div>
            </div>

            <div class="form-group">
                <label class="col-lg-2 control-label">حساب فرعى</label>
                <div class="col-lg-4">
                    <label class="checkbox-inline">
                        <input type="checkbox" name="is_leaf" value="1" {if $account.is_leaf == 1}checked{/if}> نعم، هذا حساب فرعى (يقبل القيود)
                    </label>
                </div>
            </div>

            <div class="form-group">
                <div class="col-lg-offset-2 col-lg-4">
                    <button type="submit" class="btn btn-primary">حفظ التعديلات</button>
                    <a href="accAccountController.php?do=show" class="btn btn-default">إلغاء</a>
                </div>
            </div>

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

<script>
var linkedMemberId = {if $linkedMember}{$linkedMember.id}{else}0{/if};
var linkedMemberText = '{if $linkedMember}{$linkedMember.member_name}{if $linkedMember.phone} - {$linkedMember.phone}{/if}{/if}';
</script>
{literal}
<script>
$(document).ready(function(){
    $('#member_id').select2({
        placeholder: 'ابحث عن عضو بالاسم أو الهاتف...',
        allowClear: true,
        minimumInputLength: 0,
        ajax: {
            url: 'accAccountController.php?do=searchMembers',
            dataType: 'json',
            quietMillis: 300,
            data: function(term, page) { return { term: term }; },
            results: function(data) { return { results: data.results }; }
        },
        formatResult: function(item) { return item.text; },
        formatSelection: function(item) { return item.text; }
    });

    if (linkedMemberId > 0) {
        $('#member_id').select2('data', { id: linkedMemberId, text: linkedMemberText });
    }

    $('#member_id').on('change', function(){
        var val = $(this).select2('data');
        var memberId = val ? val.id : '';
        var accountId = $('input[name=id]').val();
        $('#memberHint').hide();
        if (memberId) {
            $.get('accAccountController.php?do=checkMemberLinked&member_id=' + memberId + '&exclude_account=' + accountId, function(resp){
                if (resp.linked == 1) {
                    $('#memberHint').text('هذا العضو مرتبط بالفعل بحساب: ' + resp.account_code + ' - ' + resp.account_name).show();
                }
            }, 'json');
        }
    });
});
</script>
{/literal}
