  <?php
    $this->load->view('templates/header');

    $with_registered = 1; // default = REGISTERED
    $per_no = 0;
    $per_unregi_no = 0;
    $pur_with_registered = 0;

    if (!empty($purchase_data)) {
        $per_no = (int)$purchase_data[0]['per_entry_no'];
        $per_unregi_no = (int)$purchase_data[0]['per_unregi_entry_no'];
        $pur_with_registered = (int)$purchase_data[0]['pur_with_registered'];

        if ($per_no > 0) {
            $with_registered = 1;
        } elseif ($per_unregi_no > 0) {
            $with_registered = 0;
        }
    }

     $id   = empty($purchase_data) ? 0 : $purchase_data[0]['per_id'];
  ?>
    <script>
        var link = "purchase_section";
        var sub_link = "purchase";
    </script>
    <style>
        /* .premium-header {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 42px;
            height: 42px;
            background: #19305e !important;
            color: #fff;
            font-weight: 600;
            font-size: 20px;
            border-radius: 10px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .premium-header:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
            background: linear-gradient(135deg, #1565c0, #1e88e5);
        } */


        /* Make label not block clicks */
        .floating-label label {
            pointer-events: none;
            display: inline-flex;
            align-items: center;
            gap: 5px; /* space between STOCK ID and + */
        }

        /* Allow only the button itself to receive clicks */
        .floating-label label .add-diamond-btn {
            pointer-events: auto;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: linear-gradient(135deg, #007bff, #0056b3);
            color: #fff;
            font-size: 10px;
            margin-top: -5px;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        }

        .floating-label label .add-diamond-btn:hover {
            transform: scale(1.15);
            background: linear-gradient(135deg, #0056b3, #003d82);
        }
       .table-responsive-wrapper {
    overflow-x: auto;     /* allows horizontal scroll */
    width: 100%;          /* take full width of container */
}

#purchase_wrapper {
    table-layout: auto;   /* auto width for each column */
    width: max-content;   /* expand table to content width */
    min-width: 100%;      /* ensure table is at least full container width */
}

#purchase_wrapper th,
#purchase_wrapper td {
    white-space: nowrap;  /* prevent wrapping */
    padding: 6px 10px;
    text-align: center;
}
.floating-input1{
        width: auto;
}

/* ---------- Ultra-Premium Glowing Toggle (Small Width) ---------- */
.premium-toggle {
    --toggle-width: 80px;
    --toggle-height: 30px;
    --radius: 36px;
    --knob-size: 26px;

    position: relative;
    display: inline-block;
    width: var(--toggle-width);
    height: var(--toggle-height);
    border-radius: var(--radius);
    background: linear-gradient(145deg, #1a2238, #2e3b60);
    box-shadow: inset 0 2px 4px rgba(255,255,255,0.1), 0 4px 12px rgba(0,0,0,0.4);
    cursor: pointer;
    transition: all 0.4s ease;
    border: 1px solid rgba(255,255,255,0.2);
}

.premium-toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.premium-slider {
    position: absolute;
    top: 2px;
    left: 2px; /* default knob position = left (R) */
    width: var(--knob-size);
    height: var(--knob-size);
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, #ffffff, #cce5ff);
    box-shadow: 0 0 6px rgba(0,255,255,0.6), 0 3px 6px rgba(0,0,0,0.3), inset 0 2px 4px rgba(255,255,255,0.5);
    transition: all 0.45s cubic-bezier(0.77,0,0.175,1);
}

/* Default R label color */
.premium-toggle .on-label {
    color: #000; /* white when selected by default */
    position: absolute;
    top: 50%;
    left: 11px;
    transform: translateY(-50%);
    font-weight: 700;
    font-size: 12px;
    pointer-events: none;
    transition: color 0.3s ease;
}

/* When U is selected (checkbox checked), R label turns black */
.premium-toggle input:checked ~ .on-label {
    color: #fff;
}

.premium-toggle .off-label {
    position: absolute;
    top: 50%;
    right: 8px; /* U = right */
    transform: translateY(-50%);
    font-weight: 700;
    font-size: 12px;
    color: #fff;
    pointer-events: none;
}

/* Checked = knob slides to right (U) */
.premium-toggle input:checked ~ .premium-slider {
    transform: translateX(50px); /* knob moves to right */
    background: radial-gradient(circle at 30% 30%, #00f0ff, #0099ff);
    box-shadow: 0 0 18px rgba(0,255,255,0.9), 0 3px 6px rgba(0,0,0,0.35), inset 0 2px 5px rgba(255,255,255,0.5);
    animation: glowPulseSmall 2s infinite alternate;
}

@keyframes glowPulseSmall {
    0% { box-shadow: 0 0 18px rgba(0,255,255,0.9),0 3px 6px rgba(0,0,0,0.35), inset 0 2px 5px rgba(255,255,255,0.5); }
    50% { box-shadow: 0 0 25px rgba(0,255,255,1),0 4px 8px rgba(0,0,0,0.4), inset 0 2px 5px rgba(255,255,255,0.5); }
    100% { box-shadow: 0 0 18px rgba(0,255,255,0.9),0 3px 6px rgba(0,0,0,0.35), inset 0 2px 5px rgba(255,255,255,0.5); }
}

.premium-toggle.disabled {
    opacity: 0.65;
    pointer-events: none;
    filter: grayscale(40%);
    cursor: not-allowed;
}



        /* .premium-header {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 42px;
            height: 42px;
            background: #19305e !important;
            color: #fff;
            font-weight: 600;
            font-size: 20px;
            border-radius: 10px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .premium-header:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.25);
            background: linear-gradient(135deg, #1565c0, #1e88e5);
        } */


        /* Make label not block clicks */
        .floating-label label {
            pointer-events: none;
            display: inline-flex;
            align-items: center;
            gap: 5px; /* space between STOCK ID and + */
        }

        /* Allow only the button itself to receive clicks */
        .floating-label label .add-diamond-btn {
            pointer-events: auto;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background: linear-gradient(135deg, #007bff, #0056b3);
            color: #fff;
            font-size: 10px;
            margin-top: -5px;
            cursor: pointer;
            transition: all 0.2s ease;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        }

        .floating-label label .add-diamond-btn:hover {
            transform: scale(1.15);
            background: linear-gradient(135deg, #0056b3, #003d82);
        }

        .floating-add-btn {
            transform: translateY(0%);
            background: linear-gradient(135deg, #0e4881, #3b7dd8);
            color: #fff;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            box-shadow: 0 4px 8px rgba(0,0,0,0.2);
            z-index: 999;
            transition: all 0.3s ease;
        }

        .floating-add-btn:hover {
            background: linear-gradient(135deg, #3b7dd8, #0e4881);
            transform: translateY(-2%) translateX(-2px);
            box-shadow: 0 6px 12px rgba(0,0,0,0.25);
        }
            /* Make the wrapper horizontally scrollable */
        .table-responsive-wrapper {
            overflow-x: auto;
            position: relative;
        }

        /* Make the last cell sticky on the right */
        .floating-add-btn-cell {
            position: sticky;
            right: 0; /* stick to the right */
            background: #fff; /* match table row background */
            z-index: 5; /* keep above other cells */
        }

        /* Optional: style the button */
        .floating-add-btn-cell .floating-add-btn {
            margin: 0;
        }


        .table-responsive-wrapper {
            position: relative;
            overflow-x: auto;
        }

        /* SAME width for header & cells */
        .sticky-col {
            position: sticky;
            right: 0;                  /* will override below per col */
            background: #fff;
            z-index: 50;
        }

        /* 2nd last column – EDIT */
        .sticky-edit {
            right: 60px;               /* width of remove column */
            z-index: 55;
        }

        /* last column – REMOVE */
        .sticky-remove {
            right: 0;
            z-index: 60;
        }

        /* Ensure border still shows */
        .sticky-col {
            border-left: 1px solid #ddd !important;
        }


        /* Sticky columns - stays visible on right */
.sticky-col {
    position: sticky;
    z-index: 10;
    border-left: 1px solid #d5d8df;
}

/* Last 2 sticky columns positioning */
.sticky-edit {
    right: 60px;
    z-index: 11;
}
.sticky-remove {
    right: 0;
    z-index: 12;
}

/* Premium Edit Button */
.edit-btn-premium {
    background: linear-gradient(135deg, #244b93, #3c6bc6);
    border: none;
    padding: 5px 10px;
    border-radius: 7px;
    color: #fff;
    font-size: 13px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.15);
    transition: all 0.25s ease;
}
.edit-btn-premium:hover {
    background: linear-gradient(135deg, #1d3f7b, #355eb0);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.25);
}

/* Premium Remove Button */
.remove-btn-premium {
    display: inline-block;
    background: linear-gradient(135deg, #d9534f, #c9302c);
    color: #fff !important;
    padding: 4px 8px;
    border-radius: 6px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.15);
    transition: all 0.25s ease;
}

.remove-btn-premium:hover {
    background: linear-gradient(135deg, #c04440, #a82926);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0,0,0,0.25);
}

    </style>
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1 class="content_top"><?php echo empty($purchase_data) ? 'ADD PURCHASE' : 'UPDATE PURCHASE'; ?></h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="#">Invoice</a></li>
        <li class="active"><?php echo empty($purchase_data) ? 'Add' : 'Update' ?></li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row"><!-- left column -->
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-body">
                        <div class="alert_msg"></div>
                        <form class="form-horizontal" id="purchase_form">
                            <div class="box-body remove-bottom"><!-- box-body -->
                                <div class="form-group floating-form">

                                    <div class="col-md-1">
                                        <!-- Hidden field for backend (stores 1 or 0) -->
                                        <input type="hidden" 
                                            id="pur_with_registered_hidden" 
                                            name="pur_with_registered" 
                                            value="<?php echo $pur_with_registered; ?>" />

                                        <label class="premium-toggle <?php echo (!empty($purchase_data)) ? 'disabled' : ''; ?>">
                                            <input 
                                                type="checkbox"
                                                id="pur_with_registered_checkbox"
                                                <?php echo ($with_registered == 0) ? 'checked' : ''; ?>
                                                onchange="set_bill_type(this)"
                                            >
                                            <span class="premium-slider"></span>
                                            <span class="on-label">R</span>
                                            <span class="off-label">U</span>
                                        </label>
                                    </div>

                                    <!-- issue no -->
                                    <div id="entry_no_wrapper" class="col-md-2 floating-label">
                                        <!-- Registered Input -->
                                        <input type="text" class="form-control floating-input" 
                                            id="per_no" 
                                            name="per_no" 
                                            value="<?php echo ($per_no > 0) ? $per_no : $entry_no; ?>" 
                                            placeholder=" " autocomplete="off"
                                            style="<?php echo ($with_registered == 1) ? 'display:block;' : 'display:none;'; ?>">

                                        <!-- Unregistered Input -->
                                        <input type="text" class="form-control floating-input" 
                                            id="per_unregi_entry_no" 
                                            name="per_unregi_entry_no"
                                            value="<?php echo ($per_unregi_no > 0) ? $per_unregi_no : $entry_no_unregi; ?>" 
                                            placeholder=" " autocomplete="off"
                                            style="<?php echo ($with_registered == 0) ? 'display:block;' : 'display:none;'; ?>">

                                        <label id="entry_no_label">
                                            <?php echo ($with_registered == 1) ? 'ENTRY NO (Registered)' : 'ENTRY NO (Unregistered)'; ?>
                                        </label>
                                    </div>
                                    <!-- issue no -->

                                    <!-- issue date -->
                                    <div class="col-md-2 floating-label">
                                        <input type="hidden" class="form-control floating-input" id="per_invoice_date" name="per_invoice_date" value="<?php echo !empty($purchase_data) ? date('d-m-Y', strtotime($purchase_data[0]['per_invoice_date'])) : "" ?>"
                                        placeholder=" " autocomplete="off" required/>
                                        <input type="text" class="form-control floating-input" id="per_entry_date" name="per_entry_date" value="<?php echo !empty($purchase_data) ? date('d-m-Y', strtotime($purchase_data[0]['per_entry_date'])) : date('d-m-Y');?>"
                                        placeholder=" " autocomplete="off" required/>
                                        <label>ENTRY DATE</label>
                                    </div>
                                    <!-- issue date -->

                                    <!-- party -->
                                    <div class="col-md-2 floating-label">
                                        <?php echo form_dropdown('', $user, !empty($purchase_data) ? $purchase_data[0]['per_supplier_id'] : 0 ,'id="supp_id" name="supp_id" class="form-control flexselect floating-select" placeholder=" " autocomplete="off" tabindex="2" ');  ?>
                                        <label>SUPPLIER
                                            <span class="add-diamond-btn" title="Add SUPPLIER" onclick="add_supplier_popup()">
                                                <i class="fa fa-plus"></i>
                                            </span>
                                        </label>
                                        <span class="text-danger" id="acc_type_name"></span>
                                        <input type="hidden" name="acc_type" id="acc_type" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_acc_type'] :'';?>">
                                        <input type="hidden" name="user_id" id="user_id" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['user_id'] :'';?>">
                                        <input type="hidden" name="current_date" id="current_date" value="<?php echo date('d-m-Y');?>">
                                    </div>
                                   <!--  <div class="col-md-2 floating-label">
                                        <input type="text" class="form-control floating-input" id="per_dispatch_by" name="per_dispatch_by" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_dispatch_by'] : " " ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>DISPATCH BY</label>
                                    </div> -->
                                    <!-- party -->

                                    <!-- bill no -->
                                    <div class="col-md-2 floating-label">
                                        <input type="text" class="form-control floating-input" id="per_bill_no" name="per_bill_no" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_bill_no'] : $bill_no; ?>" placeholder=" " autocomplete="off"  />
                                        <label>BILL NO</label>
                                    </div>
                                    <!-- bill no -->

                                    <!-- bill date -->
                                    <div class="col-md-2 floating-label">
                                        <input type="text" class="form-control floating-input" id="per_bill_date" name="per_bill_date" value="<?php echo !empty($purchase_data[0]['per_bill_date']) ? date('d-m-Y', strtotime($purchase_data[0]['per_bill_date'])) : date('d-m-Y'); ?>"
                                        placeholder=" " autocomplete="off" required/>
                                        <label>BILL DATE</label>
                                    </div>
                                    <!-- bill date -->

                                    <!-- dollar rate -->
                                    <div class="col-md-1 floating-label">
                                        <input type="number" class="form-control floating-input" id="per_current_dollar_rate" name="per_current_dollar_rate" value="<?php echo !empty($purchase_data[0]['per_current_dollar_rate']) ? $purchase_data[0]['per_current_dollar_rate'] : $current_dollar_rate ?>"
                                        oninput="validateDollarRate(this); calculateDollarRateChange();" 
                                        placeholder=" " autocomplete="off" required/>
                                        <label>DOLLAR RATE</label>
                                    </div>
                                    <!-- dollar rate -->
                                    
                                    <!-- <div> -->
                                        <!-- <input type="text" class="form-control floating-input" id="per_terms" name="per_terms" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_terms'] : " " ?>"
                                        placeholder=" " autocomplete="off"/>
                                        <label>TERMS</label> -->
                                        <input type="hidden" class="form-control floating-input" id="total_per_cgst" name="total_per_cgst" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_cgst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_per_sgst" name="total_per_sgst" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_sgst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_per_igst" name="total_per_igst" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_igst'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_qty" name="total_qty" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_qty'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="total_rate" name="total_rate" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_rate'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="sub_total" name="sub_total" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_sub_total_amt'] :'';?>" autocomplete="off" readonly/>
                                        <input type="hidden" class="form-control floating-input" id="final_total" name="final_total" value="<?php echo !empty($purchase_data) ? $purchase_data[0]['per_total_amt'] :'';?>" autocomplete="off" readonly/>
                                    <!-- </div> -->
                                   
                                </div>
                                
                                <div class="form-group floating-form text-center">




                                   <div class="col-12 col-sm-12 col-md-2 col-lg-2 floating-label">
                            <input 
                                type="file" 
                                name="purchase_attachment[]" 
                                id="purchase_attachment_<?= $id ?>" 
                                class="form-control floating-input" 
                                
                                multiple="multiple"
                            >
                            <label class="text-uppercase" style="width: max-content;">Document <small class="text-danger font-weight-bold">(.png, .jpg, .jpeg, .gif, .pdf) only</small></label>
                        
                        <div class="col-12 col-sm-12 col-md-12 col-lg-12 d-flex flex-wrap purchase_attachment_preview_<?= $id ?>"></div>



                        <?php 
			// echo "<pre>"; print_r($purchase_attachment_data); exit;
?>
                        <div class="col-12 col-sm-12 col-md-12 col-lg-12 d-flex flex-wrap preview">
                                    <?php 
                                if(!empty($purchase_attachment_data[0]['purchase_cam_id'])):
                                    foreach ($purchase_attachment_data as $key => $value):

                                        $is_pdf = strtolower(pathinfo($value['purchase_cam_path'], PATHINFO_EXTENSION)) === 'pdf'; // Check if it's a PDF
                            ?>
                                        <div id="preview_<?php echo $value['purchase_cam_id'] ?>">
                                            <span>

                                            
                                        <?php if ($is_pdf): ?>
                                                <!-- If it's a PDF, show a PDF icon or a clickable link to view the PDF -->
                                                <iframe src="<?php echo base_url('/public/uploads/'.$value['purchase_cam_path']) ?>" width="280" height="280"></iframe>

                                                <button 
                                                type="button" 
                                                class="btn btn-sm btn-primary mt-2" 
                                                onclick="remove_purchase_preview_image('<?php echo $value['purchase_cam_id'] ?>')"
                                            ><i class="text-danger fa fa-trash"></i></button>
                                            
                                        <?php else: ?>
                                             <!-- If it's an image, show the image thumbnail -->
                                            <img 
                                                class=" pan form_loading" 
                                                onclick="zoom(this)" 
                                                title="Click to zoom in and zoom out" 
                                                src="<?php echo base_url('/public/uploads/'.$value['purchase_cam_path']) ?>" 
                                                data-src="<?php echo base_url('/public/uploads/'.$value['purchase_cam_path']) ?>" 
                                                data-big="<?php echo base_url('/public/uploads/'.$value['purchase_cam_path']) ?>" 
                                                style="width: 80px;"
                                            />

                                            <button 
                                                type="button" 
                                                class="btn btn-sm btn-primary mt-2" 
                                                onclick="remove_purchase_preview_image('<?php echo $value['purchase_cam_id']; ?>')"
                                                style="height: fit-content;"
                                            >
                                                <i class="text-danger fa fa-trash"></i>
                                            </button>
                                            <a 
                                                type="button" 
                                                class="btn btn-sm btn-primary mt-2"
                                                href="<?php echo base_url('public/uploads/'.$value['purchase_cam_path']); ?>"
                                                style="height: fit-content;"
                                                download
                                            ><i class="text-info fa fa-download"></i></a>
                                        <?php endif; ?>
                                                    
                                            </span>
                                        </div>
                            <?php 
                                    endforeach;
                                endif; 
                            ?>
                    </div>
                    </div>











                                     <div class="col-md-offset-1 d-flex gap-2">
                                        <button type="button" class="btn btn-primary flex-fill" id="sbt_btn"
                                            onclick="<?php echo !empty($purchase_data) ? 'add_invoice('.$purchase_data[0]['per_id'].')' :'add_invoice(0)' ;?>"
                                            tabindex="10">
                                            <?php echo !empty($purchase_data) ? 'Update' :'Submit' ;?>
                                        </button>

                                        <a href="<?php echo base_url('purchase/purchasectrl/purchase_master?action=view');?>"
                                        type="button" class="btn btn-danger flex-fill" id="cnl_btn" tabindex="11">
                                            Cancel
                                        </a>

                                        <a onclick="import_diamond_stock()" class="btn" style="background: #19305e !important; color:#ffffff !important; display:none;">Import</a>

                                       
                                        <a href="<?php echo uploads('format/diamond_data_new.xlsx'); ?>" data-toggle="tooltip" title="DIAMOND FILE FORMAT" class="fa fa-paperclip" aria-hidden="true" style="cursor: pointer;" download></a>
                                
                                    </div>
                                    
                                </div>
                            </div>
                            <div class="box box-primary">
                                <!-- <div style="display: flex; align-items: center; gap: 3px;">
                                    <div class="premium-header" title="Add Diamond" onclick="add_update_diamond_popup(0)">+</div> -->
                                    
                                    <div class="box-header with-border" style="flex: 1;">
                                        <h3 class="box-title">Add Product</h3>
                                        <div class="box-tools pull-right">
                                            <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                                <i class="fa fa-minus"></i>
                                            </button>
                                        </div>
                                    </div>
                                <!-- </div> -->
                                <div class="box-body">
    <div class="table-responsive-wrapper">
        <table class="table table-bordered" style="margin-top: 20px;">
            <tbody>
                <tr class="floating-form">

                    <!-- HSN CODE -->
                    <td class="floating-label" width="8%">
                        <?php echo form_dropdown(
                            'hsn_id',
                            $hsn_data,
                            '',
                            'id="hsn_id" class="form-control flexselect floating-select floating-input1" placeholder=" " 
                            onchange="hsn_changed()" autocomplete="off" tabindex="6"'
                        ); ?>
                        <label>HSN CODE
                            <span class="add-diamond-btn hsn-code-btn" title="Add HSN" onclick="add_update_hsn_popup(0)">
                                <i class="fa fa-plus"></i>
                            </span>
                        </label>
                    </td>

                    <!-- STOCK ID -->
                    <td class="floating-label" width="8%">
                        <?php echo form_dropdown(
                            '',
                            $diamond_data,
                            '',
                            'id="dm_id" class="form-control flexselect floating-select floating-input1" onchange="get_diamond_data(this.value)" placeholder=" " autocomplete="off" tabindex="6"'
                        );?>
                        <label>STOCK ID
                            <span class="add-diamond-btn stock-btn" title="Add Diamond" onclick="add_update_diamond_popup(0)">
                                <i class="fa fa-plus"></i>
                            </span>
                        </label>
                    </td>

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_carat" onkeyup="calculate_single_fields('carat')" /><label>Carat</label></td>
                    

                    <!-- Price/CT, DOLLAR/CT, DOLLAR AMT -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_price_ct" onkeyup="calculate_single_fields('price_ct')"/><label>Price/CT</label></td>
                    <!-- Rate -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_rate" onkeyup="calculate_single_fields('pc_amt')" readonly/><label>PC AMT</label></td>

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_dollar_ct" onkeyup="calculate_single_fields('dollar_ct')"/><label>DOLLAR/CT</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_dollar_amt" onkeyup="calculate_single_fields('dollar_amt')" readonly/><label>DOLLAR AMT</label></td>

                    </tr>
                    <tr class="floating-form">
                        <td>&nbsp;</td>
                    </tr>

                    <tr class="floating-form">

                    <!-- SIZE, Pcs, CPD -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_size" readonly/><label>SIZE</label></td>

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_qty" readonly/><label>PCS</label></td>

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_cpd" readonly/><label>CPD</label></td>

                    <!-- Shape, LAB, Cert. No., Carat, Col, Clar., Cut, Pol, Sym, Flur. -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_sh" readonly/><label>Shape</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_lab" readonly/><label>LAB</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_cert_no" readonly/><label>Cert. No.</label></td>
                    
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_col" readonly/><label>Col</label></td>

                    </tr>
                    <tr class="floating-form">
                        <td>&nbsp;</td>
                    </tr>

                    <tr class="floating-form">

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_cla" readonly/><label>Clar.</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_cut" readonly/><label>Cut</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_pol" readonly/><label>Pol</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_sym" readonly/><label>Sym</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_fl" readonly/><label>Flur.</label></td>

                    <!-- LENGTH, WIDTH, HEIGHT -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_length" readonly/><label>LENGTH</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_width" readonly/><label>WIDTH</label></td>

                    </tr>
                    <tr class="floating-form">
                        <td>&nbsp;</td>
                    </tr>

                    <tr class="floating-form">

                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_height" readonly/><label>HEIGHT</label></td>

                    <!-- NAT/Lab Grown, DISC, RAP, VIDEO, STATUS -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_dm_lab_grown" readonly/><label>NAT/Lab Grown</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_disc" readonly/><label>DISC</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="dm_rap" readonly/><label>RAP</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_video" readonly/><label>DIAMOND IMAGE AND VIDEO</label></td>

                    <!-- NO OF DAYS, RAPNET, SUSPENSE STOCK -->
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_no_of_days" readonly/><label>NO OF DAYS</label></td>
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_rapnet" readonly/><label>RAPNET</label></td>

                    </tr>
                    <tr class="floating-form">
                        <td>&nbsp;</td>
                    </tr>

                    <tr class="floating-form">
                        
                    <td class="floating-label"><input type="text" class="form-control floating-input floating-input1" id="pro_suspense_stock" readonly/><label>SUSPENSE STOCK</label></td>

                    <!-- CGST, SGST, IGST -->
                    <td class="floating-label gst_block" <?php echo ($with_registered == 0) ? 'style="display:none;"' : ''; ?>>
                        <input type="text" class="form-control floating-input floating-input1" id="pro_cgst_total" readonly />
                        <input type="text" class="form-control floating-input floating-input1" id="pro_cgst" readonly />
                        <label>CGST</label>
                    </td>

                    <td class="floating-label gst_block" <?php echo ($with_registered == 0) ? 'style="display:none;"' : ''; ?>>
                        <input type="text" class="form-control floating-input floating-input1" id="pro_sgst_total" readonly />
                        <input type="text" class="form-control floating-input floating-input1" id="pro_sgst" readonly />
                        <label>SGST</label>
                    </td>

                    <td class="floating-label gst_block" <?php echo ($with_registered == 0) ? 'style="display:none;"' : ''; ?>>
                        <input type="text" class="form-control floating-input floating-input1" id="pro_igst_total" readonly />
                        <input type="text" class="form-control floating-input floating-input1" id="pro_igst" readonly />
                        <label>IGST</label>
                    </td>

                    <!-- Hidden fields to send 0 if invisible -->
                    <!-- <input type="hidden" id="pro_cgst_total" value="0"> -->
                    <!-- <input type="hidden" id="pro_cgst" value="0"> -->
                    <!-- <input type="hidden" id="pro_sgst_total" value="0"> -->
                    <!-- <input type="hidden" id="pro_sgst" value="0"> -->
                    <!-- <input type="hidden" id="pro_igst_total" value="0"> -->
                    <!-- <input type="hidden" id="pro_igst" value="0"> -->

                    <!-- Total -->
                    <td class="floating-label">
                        <input type="hidden" class="form-control floating-input floating-input1" id="pro_sub_total" readonly/>
                        <input type="text" class="form-control floating-input floating-input1" id="pro_final_total" readonly/>
                        <label>Total</label>
                    </td>

                    <!-- Add Button -->
                    <td class="floating-add-btn-cell">
                        <button type="button" class="btn btn-success floating-add-btn"
                            onclick="<?php echo empty($purchase_data) ? 'add_purchase_trans_row(0)' : 'add_purchase_trans_row('.$purchase_data[0]['per_id'].')'; ?>">
                            <i class="fa fa-plus"></i> Add
                        </button>
                    </td>

                </tr>
            </tbody>
        </table>
    </div>
</div>

                            </div><!-- /.box -->
                            <div class="box box-primary">
                                <div class="box-header with-border" style="flex: 1;">
                                    <h3 class="box-title">Product Details</h3>
                                    <div class="box-tools pull-right">
                                        <button type="button" class="btn btn-box-tool" data-widget="collapse">
                                            <i class="fa fa-minus"></i>
                                        </button>
                                    </div>
                                </div>
                            <!-- /.box-header -->
                            <div class="box-body">
                                <div class="table-responsive-wrapper" style="position: relative; overflow-x: auto;">
    <table class="table table-bordered" id="purchase_wrapper">
        <thead>
    <tr>
        <!-- <th>Date</th> -->
        <th>HSN CODE</th>
        <th>STOCK ID</th>
        <th>Carat</th>
        <th>Price/CT</th>
        <th>PC AMT</th>
        <th>DOLLAR/CT</th>
        <th>DOLLAR AMT</th>
        <th>SIZE</th>
        <th>PCS</th>
        <th>CPD</th>
        <th>Shape</th>
        <th>LAB</th>
        <th>Cert. No.</th>
        <th>Col</th>
        <th>Clar.</th>
        <th>Cut</th>
        <th>Pol</th>
        <th>Sym</th>
        <th>Flur.</th>
        <th>LENGTH</th>
        <th>WIDTH</th>
        <th>HEIGHT</th>
        <th>NAT/ Lab Grown</th>
        <th>DISC</th>
        <th>RAP</th>
        <th>DIAMOND IMAGE AND VIDEO</th>
        <!-- <th>STATUS</th> -->
        <!-- <th>UPLOADED DATE</th> -->
        <th>NO OF DAYS</th>
        <th>RAPNET</th>
        <th>SUSPENSE STOCK</th>
        <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">CGST</th>
        <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">SGST</th>
        <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">IGST</th>
        <th>Total</th>
        <th class="sticky-col sticky-edit">EDIT</th>
        <th class="sticky-col sticky-remove">REMOVE</th>
    </tr>
    </thead>
 <tbody>
    <?php $prt_cnt = 1; 
    if (!empty($purchase_trans)):
			// echo "<pre>"; print_r($purchase_trans); exit;

        foreach ($purchase_trans as $value): ?>
    <tr id="rowid_<?php echo $prt_cnt; ?>">
         <input type="hidden" name="prt_id[]" value="<?php echo $value['prt_id']; ?>">
        <!-- <td> -->
            <input type="hidden" class="form-control floating-input floating-input1" name="prt_date[]" value="<?php echo date('d-m-Y', strtotime($value['prt_date'])); ?>" readonly>
        <!-- </td> -->

        <td><input type="text" class="form-control no-edit floating-input floating-input1" name="prt_hsn_code[]" value="<?php echo $value['hsn_code']; ?>" readonly>

            <input type="hidden" name="prt_hsn_id[]" id="prt_hsn_id" value="<?php echo $value['hsn_id']; ?>"/>
        </td>

        <td><input type="text" class="form-control no-edit floating-input floating-input1 text-uppercase" name="prt_dm_code[]" value="<?php echo $value['prt_dm_code']; ?>" readonly>
        
            <input type="hidden" name="prt_dm_id[]" id="prt_dm_id" value="<?php echo $value['prt_dm_id']; ?>"/>
        </td>

        <!-- // QTY -->
        <td><input type="number" class="form-control floating-input floating-input1" name="prt_carat[]" value="<?php echo $value['prt_carat']; ?>" oninput="calculatePriceFields(<?php echo $prt_cnt; ?>, 'carat')" readonly></td>

        <td><input type="number" class="form-control floating-input floating-input1" name="prt_price_ct[]" value="<?php echo $value['prt_price_ct']; ?>"  oninput="calculatePriceFields(<?php echo $prt_cnt; ?>, 'price_ct')" readonly></td>

        <!-- // RATE -->
        <td><input type="number" class="form-control no-edit floating-input floating-input1" name="prt_pc_amt[]" value="<?php echo $value['prt_pc_amt']; ?>" oninput="calculatePriceFields(<?php echo $prt_cnt; ?>, 'pc_amt')" readonly></td>

        <td><input type="number" class="form-control floating-input floating-input1" name="prt_dollar[]" value="<?php echo $value['prt_dollar']; ?>" oninput="calculatePriceFields(<?php echo $prt_cnt; ?>, 'dollar_ct')" readonly></td>

        <td><input type="number" class="form-control no-edit floating-input floating-input1" name="prt_dollar_amt[]" value="<?php echo $value['prt_dollar_amt']; ?>" oninput="calculatePriceFields(<?php echo $prt_cnt; ?>, 'dollar_amt')" readonly></td>

        <td><input type="text" class="form-control floating-input floating-input1" name="prt_size[]" value="<?php echo $value['prt_size']; ?>" readonly></td>

        <td><input type="number" class="form-control floating-input floating-input1" name="prt_pcs[]" value="<?php echo $value['prt_pcs']; ?>" readonly></td>

        <td><input type="text" class="form-control floating-input floating-input1" name="prt_cpd[]" value="<?php echo $value['prt_cpd']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_sh[]" value="<?php echo $value['prt_dm_sh']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_lab[]" value="<?php echo $value['prt_lab']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_cert_no[]" value="<?php echo $value['prt_cert_no']; ?>" readonly></td>

        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_col[]" value="<?php echo $value['prt_dm_col']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_cla[]" value="<?php echo $value['prt_dm_cla']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_cut[]" value="<?php echo $value['prt_dm_cut']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_pol[]" value="<?php echo $value['prt_dm_pol']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_sym[]" value="<?php echo $value['prt_dm_sym']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_fl[]" value="<?php echo $value['prt_dm_fl']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_length[]" value="<?php echo $value['prt_length']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_width[]" value="<?php echo $value['prt_width']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_height[]" value="<?php echo $value['prt_height']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_dm_lab[]" value="<?php echo $value['prt_dm_lab']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_disc[]" value="<?php echo $value['prt_disc']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_rap[]" value="<?php echo $value['prt_rap']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_video[]" value="<?php echo $value['prt_video']; ?>" readonly></td>
        <!-- <td> -->
            <input type="hidden" class="form-control floating-input floating-input1" name="prt_status[]" value="<?php echo $value['prt_status']; ?>" readonly>
        <!-- </td> -->

        <!-- <td> -->
            <input type="hidden" class="form-control floating-input floating-input1" name="prt_uploaded_date[]" value="<?php echo date('d-m-Y', strtotime($value['prt_uploaded_date'])); ?>" readonly>
        <!-- </td> -->

        <td><input type="text" class="form-control floating-input floating-input1" name="prt_no_of_days[]" value="<?php echo $value['prt_no_of_days']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_rapnet[]" value="<?php echo $value['prt_rapnet']; ?>" readonly></td>
        <td><input type="text" class="form-control floating-input floating-input1" name="prt_suspense_stock[]" value="<?php echo $value['prt_suspense_stock']; ?>" readonly></td>

        <td class="floating-label gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">
            <input type="text" class="form-control no-edit floating-input floating-input1" name="prt_cgst_total[]" value="<?php echo $value['prt_amt_cgst']; ?>" readonly> 
            <input type="hidden" name="prt_cgst[]" value="<?php echo $value['prt_per_cgst']; ?>">
        </td>
        <td class="floating-label gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">
            <input type="text" class="form-control no-edit floating-input floating-input1" name="prt_sgst_total[]" value="<?php echo $value['prt_amt_sgst']; ?>" readonly>
            <input type="hidden" name="prt_sgst[]" value="<?php echo $value['prt_per_sgst']; ?>">
        </td>
        <td class="floating-label gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>">
            <input type="text" class="form-control no-edit floating-input floating-input1" name="prt_igst_total[]" value="<?php echo $value['prt_amt_igst']; ?>" readonly>
            <input type="hidden" name="prt_igst[]" value="<?php echo $value['prt_per_igst']; ?>">
        </td>

        <!-- Hidden fields to send 0 if invisible -->
        <!-- <input type="hidden" name="prt_cgst[]" value="0">
        <input type="hidden" name="prt_sgst[]" value="0">
        <input type="hidden" name="prt_igst[]" value="0"> -->

        <td><input type="text" class="form-control no-edit floating-input floating-input1" name="prt_total[]" value="<?php echo $value['prt_total_amt']; ?>" readonly></td>

        <td class="sticky-col sticky-edit">
            <button type="button" class="edit-btn-premium edit-row" onclick="toggleEditRow(<?php echo $prt_cnt; ?>)">
                <i class="glyphicon glyphicon-pencil"></i>
            </button>
        </td>

        <td class="sticky-col sticky-remove">
            <a href="javascript:void(0)" class="remove-btn-premium" onclick="remove_purchase_row(<?php echo $prt_cnt; ?>)">
                <i class="glyphicon glyphicon-trash" style="margin-top:3px;"></i>
            </a>
        </td>
    </tr>
    <?php $prt_cnt++; endforeach; endif; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="2" style="text-align:left;">Grand Totals:</th>
                <th><input type="text" class="form-control" id="total_qty_pcs" readonly></th>
                <th><input type="text" class="form-control" id="total_price_ct" readonly></th>
                <th><input type="text" class="form-control" id="total_rate_price" readonly></th>

                 <th><input type="text" class="form-control" id="total_dollar_ct" readonly></th>
                 <th><input type="text" class="form-control" id="total_dollar_amt" readonly></th>

                <th colspan="22"></th>

                <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>"><input type="text" class="form-control" id="total_cgst" readonly></th>
                <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>"><input type="text" class="form-control" id="total_sgst" readonly></th>
                <th class="gst_block" style="<?php echo ($with_registered == 0) ? 'display:none;' : ''; ?>"><input type="text" class="form-control" id="total_igst" readonly></th>
                <th><input type="text" class="form-control" id="total_amount" readonly></th>
                <th></th>
            </tr>
        </tfoot>

    </table>
</div>

                            </div><!-- /.box -->
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <!-- /.content -->

<?php
    $this->load->view('templates/footer');
?>
<script src="<?php echo assets('js/purchase.js')?>"></script>

<?php
    if(!empty($purchase_data))
    {
        echo "<script>";
        echo "prt_cnt = $prt_cnt;";
        echo "</script>";
    }
?>

<script>
function set_bill_type(checkbox) {
   
    // Hidden field for backend
    const hiddenInput = document.getElementById('pur_with_registered_hidden');
    hiddenInput.value = checkbox.checked ? "1" : "0"; // 1 = Unregistered, 0 = Registered

    // ALL GST columns (CGST, SGST, IGST — both td & th)
    const gstBlocks = document.querySelectorAll(".gst_block");

    if (checkbox.checked) {
        // UNREGISTERED → Hide all GST columns
        gstBlocks.forEach(el => {
            el.style.display = "none";
            // Reset input values inside GST columns
            // el.querySelectorAll('input').forEach(input => input.value = 0);
        });

        // Reset totals
        ["total_cgst", "total_sgst", "total_igst"].forEach(id => {
            const el = document.getElementById(id);
            if (el) el.value = 0;
        });

    } else {
        // REGISTERED → Show GST columns
        gstBlocks.forEach(el => el.style.display = "table-cell");
    }

    // ========= ENTRY NO Swap =========
    const wrapper = document.getElementById('entry_no_wrapper');
    const importBtn = document.getElementById('import_btn');

    const entry_no = "<?php echo $entry_no; ?>";
    const entry_no_unregi = "<?php echo $entry_no_unregi; ?>";
    const reg_no = "<?php echo !empty($purchase_data) ? $purchase_data[0]['per_entry_no'] : ''; ?>";
    const unreg_no = "<?php echo !empty($purchase_data) ? $purchase_data[0]['per_unregi_entry_no'] : ''; ?>";

    wrapper.innerHTML = '';

    if (checkbox.checked) {
        // Unregistered
        wrapper.innerHTML = `
            <input type="text" class="form-control floating-input"
                id="per_unregi_entry_no" name="per_unregi_entry_no"
                value="${unreg_no || entry_no_unregi}"
                placeholder=" " autocomplete="off" />
            <label>ENTRY NO (Unregistered)</label>
        `;
        if (importBtn) importBtn.style.display = 'inline-block';
    } else {
        // Registered
        wrapper.innerHTML = `
            <input type="text" class="form-control floating-input"
                id="per_no" name="per_no"
                value="${reg_no || entry_no}"
                placeholder=" " autocomplete="off" />
            <label>ENTRY NO (Registered)</label>
        `;
        if (importBtn) importBtn.style.display = 'none';
    }

    calculate_sub_total_amt();
    calculate_final_total_for_trans();
    updateGrandTotals();
}

</script>

</body>
</html>