<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 id="accountForm" action="accAccountController.php?do=savedata" method="post" class="form-horizontal">

            <div class="form-group">
                <label class="col-lg-2 control-label">الحساب الأب</label>
                <div class="col-lg-4">
                    <select name="parent_id" id="parent_id" class="form-control" onchange="onParentChange(this)">
                        <option value="0">-- حساب رئيسى --</option>
                        {foreach $parentAccounts as $pa}
                        <option value="{$pa.id}" data-code="{$pa.code}" data-nature="{$pa.account_nature}" data-type="{$pa.account_type}" {if $pa.id == $preParent}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">
                    <input type="text" name="code" id="code" class="form-control" value="{$nextCode}" required>
                    <span class="help-block" id="codeHint" style="color:#888;font-size:12px;">
                        {if $parentData}الكود التالى تحت {$parentData.code} - {$parentData.name}{/if}
                    </span>
                </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" required>
                </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 !$parentData || $parentData.account_nature == 'debit'}checked{/if}> مدين
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="account_nature" value="credit" {if $parentData && $parentData.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" id="account_type" class="form-control" required>
                        <option value="">اختر</option>
                        <option value="assets" {if $parentData && $parentData.account_type == 'assets'}selected{/if}>أصول</option>
                        <option value="liabilities" {if $parentData && $parentData.account_type == 'liabilities'}selected{/if}>خصوم</option>
                        <option value="equity" {if $parentData && $parentData.account_type == 'equity'}selected{/if}>حقوق ملكية</option>
                        <option value="revenue" {if $parentData && $parentData.account_type == 'revenue'}selected{/if}>إيرادات</option>
                        <option value="expenses" {if $parentData && $parentData.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" checked> نعم، هذا حساب فرعى (يقبل القيود)
                    </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>

{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; }
    });

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

function onParentChange(sel) {
    var opt = sel.options[sel.selectedIndex];
    var parentCode = opt.getAttribute('data-code') || '';
    var parentNature = opt.getAttribute('data-nature') || '';
    var parentType = opt.getAttribute('data-type') || '';
    var parentId = sel.value;

    // Auto-generate code: fetch next code via AJAX
    if (parentId > 0) {
        $.get('accAccountController.php?do=getNextCode&parent_id=' + parentId, function(resp) {
            if (resp.status == 1) {
                $('#code').val(resp.code);
                $('#codeHint').text('الكود التالى تحت ' + parentCode);
            }
        }, 'json');

        // Auto-fill nature and type from parent
        if (parentNature) {
            $('input[name=account_nature][value=' + parentNature + ']').prop('checked', true);
        }
        if (parentType) {
            $('#account_type').val(parentType);
        }
    } else {
        $('#code').val('');
        $('#codeHint').text('');
    }
}
</script>
{/literal}
