<style>
    .item_form {
        padding: 20px;
        font-family: 'Segoe UI', Arial, sans-serif;
        background: #f0f4ff;
        min-height: 100%;
    }

    .item_card {
        background: rgba(255, 255, 255, 0.75);
        backdrop-filter: blur(12px);
        border: 1px solid rgba(255, 255, 255, 0.9);
        border-radius: 14px;
        padding: 22px 26px;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
        margin-bottom: 16px;
    }

    .item_section_title {
        font-size: 12px;
        font-weight: 700;
        color: #f47f20;
        text-transform: uppercase;
        letter-spacing: 0.8px;
        margin-bottom: 16px;
        padding-bottom: 8px;
        border-bottom: 2px solid rgba(244, 127, 32, 0.2);
    }

    .item_grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 12px 20px;
    }

    .item_grid.full {
        grid-template-columns: 1fr;
    }

    .form_group {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }

    .form_group label {
        font-size: 11px;
        font-weight: 700;
        color: #666;
        text-transform: uppercase;
        letter-spacing: 0.4px;
    }

    .form_group label.required::after {
        content: ' *';
        color: #dc3545;
    }

    .form_group input[type=text],
    .form_group select,
    .form_group textarea {
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: 9px 12px;
        font-size: 13px;
        color: #333;
        outline: none;
        transition: all 0.2s;
        background: white;
        font-family: 'Segoe UI', Arial, sans-serif;
        width: 100%;
        box-sizing: border-box;
    }

    .form_group input:focus,
    .form_group select:focus,
    .form_group textarea:focus {
        border-color: #f47f20;
        box-shadow: 0 0 0 3px rgba(244, 127, 32, 0.12);
    }

    .form_group textarea {
        resize: vertical;
        min-height: 80px;
    }

    .tax_row {
        display: flex;
        gap: 8px;
        align-items: center;
    }

    .tax_row input {
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: 9px 12px;
        font-size: 13px;
        color: #333;
        outline: none;
        transition: all 0.2s;
        background: white;
        font-family: 'Segoe UI', Arial, sans-serif;
    }

    .tax_row input:first-child {
        flex: 2;
    }

    .tax_row input:last-child {
        flex: 1;
    }

    .tax_row input:focus {
        border-color: #f47f20;
        box-shadow: 0 0 0 3px rgba(244, 127, 32, 0.12);
    }

    .tax_pct {
        font-weight: 700;
        color: #666;
        font-size: 16px;
    }

    .checkbox_row {
        display: flex;
        align-items: center;
        gap: 10px;
        padding: 10px 14px;
        background: rgba(244, 127, 32, 0.05);
        border: 1px solid rgba(244, 127, 32, 0.15);
        border-radius: 8px;
    }

    .checkbox_row input[type=checkbox] {
        accent-color: #f47f20;
        width: 16px;
        height: 16px;
        cursor: pointer;
    }

    .checkbox_row label {
        font-size: 13px;
        font-weight: 600;
        color: #444;
        cursor: pointer;
        margin: 0;
        text-transform: none;
        letter-spacing: 0;
    }

    .submit_btn {
        background: linear-gradient(135deg, #f47f20, #e05f00);
        color: white;
        border: none;
        border-radius: 10px;
        padding: 12px 32px;
        font-size: 14px;
        font-weight: 700;
        cursor: pointer;
        box-shadow: 0 4px 14px rgba(244, 127, 32, 0.4);
        transition: all 0.2s;
        width: 100%;
        margin-top: 4px;
        font-family: 'Segoe UI', Arial, sans-serif;
    }

    .submit_btn:hover {
        background: linear-gradient(135deg, #ff8f30, #f47f20);
        box-shadow: 0 6px 20px rgba(244, 127, 32, 0.5);
        transform: translateY(-1px);
    }

    #error_message_box {
        background: rgba(220, 53, 69, 0.08);
        border: 1px solid rgba(220, 53, 69, 0.2);
        border-radius: 8px;
        padding: 10px 14px;
        margin-bottom: 14px;
        color: #dc3545;
        font-size: 12px;
        list-style: none;
    }

    #error_message_box li {
        margin-left: 12px;
        list-style: disc;
    }

    /* ── Responsive ── */
    @media (max-width: 768px) {

        .item_form {
            padding: 12px;
        }

        .item_card {
            padding: 14px 16px;
            margin-bottom: 12px;
        }

        .item_grid {
            grid-template-columns: 1fr;
        }

        .tax_row {
            flex-wrap: wrap;
            gap: 6px;
        }

        .tax_row input:first-child,
        .tax_row input:last-child {
            flex: 1;
            min-width: 80px;
        }

        .submit_btn {
            padding: 12px;
            font-size: 14px;
        }
    }

    @media (max-width: 480px) {

        .item_form {
            padding: 8px;
        }

        .item_card {
            padding: 12px;
        }

        .item_section_title {
            font-size: 11px;
        }

        .form_group input[type=text],
        .form_group select,
        .form_group textarea {
            font-size: 14px;
            padding: 10px 12px;
        }

        .checkbox_row {
            padding: 8px 10px;
        }

        .checkbox_row label {
            font-size: 12px;
        }
    }
</style>

<ul id="error_message_box" style="display:none;"></ul>

<?php
echo form_open('items/find_item_info/' . $item_info->item_id, array('id' => 'item_number_form'));
echo form_close();
echo form_open('items/save/' . $item_info->item_id, array('id' => 'item_form'));
?>

<div class="item_form">

    <!-- Basic Info -->
    <div class="item_card">
        <div class="item_section_title">📦 <?php echo $this->lang->line('items_basic_information'); ?></div>
        <div class="item_grid">
            <div class="form_group">
                <label><?php echo $this->lang->line('items_item_number'); ?></label>
                <?php echo form_input(array('name' => 'item_number', 'id' => 'item_number', 'value' => $item_info->item_number)); ?>
            </div>
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_name'); ?></label>
                <?php echo form_input(array('name' => 'name', 'id' => 'name', 'value' => $item_info->name)); ?>
            </div>
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_category'); ?></label>
                <?php echo form_input(array('name' => 'category', 'id' => 'category', 'value' => $item_info->category)); ?>
            </div>
            <div class="form_group">
                <label><?php echo $this->lang->line('items_supplier'); ?></label>
                <?php echo form_dropdown('supplier_id', $suppliers, $selected_supplier); ?>
            </div>
        </div>
    </div>

    <!-- Pricing -->
    <div class="item_card">
        <div class="item_section_title">💰 Pricing & Tax</div>
        <div class="item_grid">
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_cost_price'); ?></label>
                <?php echo form_input(array('name' => 'cost_price', 'id' => 'cost_price', 'value' => $item_info->cost_price)); ?>
            </div>
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_unit_price'); ?></label>
                <?php echo form_input(array('name' => 'unit_price', 'id' => 'unit_price', 'value' => $item_info->unit_price)); ?>
            </div>
            <div class="form_group">
                <label><?php echo $this->lang->line('items_tax_1'); ?></label>
                <div class="tax_row">
                    <?php echo form_input(array('name' => 'tax_names[]', 'id' => 'tax_name_1', 'value' => isset($item_tax_info[0]['name']) ? $item_tax_info[0]['name'] : $this->config->item('default_tax_1_name'))); ?>
                    <?php echo form_input(array('name' => 'tax_percents[]', 'id' => 'tax_percent_name_1', 'value' => isset($item_tax_info[0]['percent']) ? $item_tax_info[0]['percent'] : $default_tax_1_rate)); ?>
                    <span class="tax_pct">%</span>
                </div>
            </div>
            <div class="form_group">
                <label><?php echo $this->lang->line('items_tax_2'); ?></label>
                <div class="tax_row">
                    <?php echo form_input(array('name' => 'tax_names[]', 'id' => 'tax_name_2', 'value' => isset($item_tax_info[1]['name']) ? $item_tax_info[1]['name'] : $this->config->item('default_tax_2_name'))); ?>
                    <?php echo form_input(array('name' => 'tax_percents[]', 'id' => 'tax_percent_name_2', 'value' => isset($item_tax_info[1]['percent']) ? $item_tax_info[1]['percent'] : $default_tax_2_rate)); ?>
                    <span class="tax_pct">%</span>
                </div>
            </div>
        </div>
    </div>

    <!-- Stock -->
    <div class="item_card">
        <div class="item_section_title">📊 Stock & Description</div>
        <div class="item_grid">
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_quantity'); ?></label>
                <?php echo form_input(array('name' => 'quantity', 'id' => 'quantity', 'value' => $item_info->quantity)); ?>
            </div>
            <div class="form_group">
                <label class="required"><?php echo $this->lang->line('items_reorder_level'); ?></label>
                <?php echo form_input(array('name' => 'reorder_level', 'id' => 'reorder_level', 'value' => $item_info->reorder_level)); ?>
            </div>
        </div>
        <div class="item_grid full" style="margin-top:12px;">
            <div class="form_group">
                <label><?php echo $this->lang->line('items_description'); ?></label>
                <?php echo form_textarea(array('name' => 'description', 'id' => 'description', 'value' => $item_info->description, 'rows' => 4, 'cols' => 17)); ?>
            </div>
        </div>
        <div class="item_grid" style="margin-top:12px;">
            <div class="checkbox_row">
                <?php echo form_checkbox(array('name' => 'allow_alt_description', 'id' => 'allow_alt_description', 'value' => 1, 'checked' => $item_info->allow_alt_description ? 1 : 0)); ?>
                <label for="allow_alt_description"><?php echo $this->lang->line('items_allow_alt_desciption'); ?></label>
            </div>
            <div class="checkbox_row">
                <?php echo form_checkbox(array('name' => 'is_serialized', 'id' => 'is_serialized', 'value' => 1, 'checked' => $item_info->is_serialized ? 1 : 0)); ?>
                <label for="is_serialized"><?php echo $this->lang->line('items_is_serialized'); ?></label>
            </div>
        </div>

        <button type="submit" class="submit_btn" style="margin-top:20px;">
            💾 <?php echo $this->lang->line('common_submit'); ?>
        </button>
    </div>

</div>

<?php echo form_close(); ?>

<script type='text/javascript'>
    $(document).ready(function() {
        $("#category").autocomplete("<?php echo site_url('items/suggest_category'); ?>", {
            max: 100,
            minChars: 0,
            delay: 10
        });
        $("#category").result(function(event, data, formatted) {});
        $("#category").search();

        $('#item_form').validate({
            submitHandler: function(form) {
                $('#item_number').val($('#scan_item_number').val());
                $(form).ajaxSubmit({
                    success: function(response) {
                        tb_remove();
                        post_item_form_submit(response);
                    },
                    dataType: 'json'
                });
            },
            errorLabelContainer: "#error_message_box",
            wrapper: "li",
            rules: {
                name: "required",
                category: "required",
                cost_price: {
                    required: true,
                    number: true
                },
                unit_price: {
                    required: true,
                    number: true
                },
                quantity: {
                    required: true,
                    number: true
                },
                reorder_level: {
                    required: true,
                    number: true
                }
            },
            messages: {
                name: "<?php echo $this->lang->line('items_name_required'); ?>",
                category: "<?php echo $this->lang->line('items_category_required'); ?>",
                cost_price: {
                    required: "<?php echo $this->lang->line('items_cost_price_required'); ?>",
                    number: "<?php echo $this->lang->line('items_cost_price_number'); ?>"
                },
                unit_price: {
                    required: "<?php echo $this->lang->line('items_unit_price_required'); ?>",
                    number: "<?php echo $this->lang->line('items_unit_price_number'); ?>"
                },
                quantity: {
                    required: "<?php echo $this->lang->line('items_quantity_required'); ?>",
                    number: "<?php echo $this->lang->line('items_quantity_number'); ?>"
                },
                reorder_level: {
                    required: "<?php echo $this->lang->line('items_reorder_level_required'); ?>",
                    number: "<?php echo $this->lang->line('items_reorder_level_number'); ?>"
                }
            },
            showErrors: function(errorMap, errorList) {
                $('#error_message_box').show();
                this.defaultShowErrors();
            }
        });
    });
</script>