<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Task - Task Panel</title>
    
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Instrument+Sans:ital,wght@0,400..700;1,400..700&display=swap" rel="stylesheet">
    
    <style>
        :root {
            --primary: #3b46f1;
            --primary-color: #3b46f1;
            --primary-hover: #2a35d8;
            --primary-light: #eff6ff;
            --bg-main: #f8fafc;
            --bg-card: #ffffff;
            
            --text-main: #0f172a;
            --text-muted: #64748b;
            --text-label: #334155;
            --border-color: #e2e8f0;
            --border-hover: #cbd5e1;
            --font-family: 'Instrument Sans', sans-serif;

            --sidebar-width: 260px;
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -2px rgba(0, 0, 0, 0.02);
            --border-radius: 12px;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: var(--font-family);
            background-color: var(--bg-main);
            color: var(--text-main);
            min-height: 100vh;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
            overflow-x: hidden;
        }

        /* Scrollbar customization */
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
        ::-webkit-scrollbar-track {
            background: transparent;
        }
        ::-webkit-scrollbar-thumb {
            background: #cbd5e1;
            border-radius: 4px;
        }
        ::-webkit-scrollbar-thumb:hover {
            background: #94a3b8;
        }

        /* Main Page Layout Wrapper */
        .dashboard-layout {
            display: flex;
            position: relative;
            min-height: 100vh;
        }

        /* Main Content Layout */
        .main-content {
            margin-left: var(--sidebar-width);
            flex-grow: 1;
            padding: 2rem;
            display: flex;
            flex-direction: column;
            gap: 2rem;
            min-width: 0;
            transition: margin-left 0.3s ease;
        }

        /* Topbar styling */
        .topbar {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 1.5rem;
        }

        .topbar-left {
            display: flex;
            flex-direction: column;
            gap: 0.25rem;
        }

        .topbar-right {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

     

     
     

        .user-profile {
            display: flex;
            align-items: center;
            gap: 0.75rem;
            cursor: pointer;
            padding: 0.25rem;
            border-radius: 8px;
            transition: background-color 0.2s;
        }

        .user-profile:hover {
            background-color: #f1f5f9;
        }

        .avatar-frame {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            position: relative;
            background: linear-gradient(135deg, #4f46e5, #3b82f6);
            display: flex;
            align-items: center;
            justify-content: center;
            color: #ffffff;
            font-weight: 700;
            font-size: 0.85rem;
            overflow: hidden;
        }

        .avatar-img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

       

        .user-info {
            display: flex;
            flex-direction: column;
            line-height: 1.25;
        }

        .user-fullname {
            font-size: 0.875rem;
            font-weight: 700;
            color: var(--text-main);
        }

        .user-role-label {
            font-size: 0.75rem;
            color: var(--text-muted);
            font-weight: 500;
        }

        .profile-arrow {
            color: #64748b;
            display: flex;
            align-items: center;
        }

        .profile-arrow svg {
            width: 1rem;
            height: 1rem;
        }

        /* Page Header Title */
        .dashboard-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: none;
            padding-bottom: 0;
        }

        .dashboard-title {
            font-size: 1.75rem;
            font-weight: 800;
            letter-spacing: -0.02em;
            color: #0e1c59;
        }

        /* Alert notifications */
        .alert-error {
            background-color: #fef2f2;
            border-left: 4px solid #ef4444;
            padding: 1rem;
            border-radius: 8px;
            color: #991b1b;
            font-size: 0.875rem;
            font-weight: 600;
        }

        .alert-error ul {
            list-style: inside disc;
            font-weight: 500;
            margin-top: 0.25rem;
            font-size: 0.825rem;
        }

        /* Card form layout */
        .form-card {
            background-color: var(--bg-card);
            border: 1px solid #e2e8f0;
            border-radius: var(--border-radius);
            box-shadow: var(--shadow-sm);
            padding: 2.25rem;
            display: flex;
            flex-direction: column;
            gap: 1.75rem;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 0.625rem;
        }

        .form-label {
            font-size: 0.875rem;
            font-weight: 700;
            color: #0e1c59;
        }

        .input-field {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            font-size: 0.875rem;
            color: var(--text-main);
            background-color: #ffffff;
            transition: all 0.2s;
            font-family: inherit;
            outline: none;
        }

        .input-field::placeholder {
            color: var(--text-muted);
        }

        .input-field:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(59, 70, 241, 0.1);
        }

        textarea.input-field {
            resize: vertical;
            min-height: 100px;
            line-height: 1.5;
        }

        /* 2 Column sub grid */
        .form-row-2col {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1.5rem;
        }

        /* Dropdowns arrow override */
        select.input-field {
            appearance: none;
            padding-right: 2.5rem;
            background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2364748b' stroke-width='2.5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 1rem center;
            background-size: 0.8rem;
            font-weight: 500;
        }

        select.input-field:focus {
            background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%233b46f1' stroke-width='2.5'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'/%3E%3C/svg%3E");
        }

        /* Form Footer Buttons */
        .form-actions-footer {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 1rem;
            border-top: 1px solid #f1f5f9;
            padding-top: 1.75rem;
            margin-top: 0.5rem;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.75rem 1.75rem;
            border-radius: 8px;
            font-size: 0.875rem;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
            border: 1px solid transparent;
            font-family: inherit;
        }

        .btn-cancel {
            background-color: #ffffff;
            border: 1px solid #e2e8f0;
            color: var(--text-label);
        }

        .btn-cancel:hover {
            background-color: #f8fafc;
            border-color: #cbd5e1;
        }

        .btn-submit {
            background-color: var(--primary);
            color: #ffffff;
            box-shadow: 0 4px 10px rgba(59, 70, 241, 0.18);
        }

        .btn-submit:hover {
            background-color: var(--primary-hover);
            box-shadow: 0 6px 15px rgba(59, 70, 241, 0.25);
        }

        /* Drawer Toggler for Mobile/Tablet */
        .menu-toggle-btn {
            display: none;
            background: #ffffff;
            border: 1px solid #e2e8f0;
            width: 38px;
            height: 38px;
            border-radius: 8px;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: var(--text-label);
            margin-right: auto;
        }

        .menu-toggle-btn svg {
            width: 1.25rem;
            height: 1.25rem;
            stroke-width: 2.5;
        }



        /* Responsive adaptation */
        @media (max-width: 1024px) {
            .main-content {
                margin-left: 0;
                padding: 1.5rem;
            }
            .menu-toggle-btn {
                display: flex;
            }
            .topbar {
                justify-content: space-between;
            }
        }

        @media (max-width: 640px) {
            .form-row-2col {
                grid-template-columns: 1fr;
                gap: 1.5rem;
            }
            .form-card {
                padding: 1.5rem;
            }
            .form-actions-footer {
                flex-direction: column-reverse;
                align-items: stretch;
            }
            .btn {
                width: 100%;
            }
        }
    </style>
    <!-- Favicon -->
    <link rel="icon" type="image/png" href="{{ asset('images/faviocntaskpannel.png') }}">
</head>
<body>

    <div class="dashboard-layout">
        @include('user.sidebar.sidebar')

        <!-- RIGHT MAIN CONTENT -->
        <main class="main-content">
        @include('user.sidebar.topbar', ['pageTitle' => 'Add New Task'])

        <!-- Form Errors Display -->
        @if ($errors->any())
            <div class="alert-error">
                <strong>Please correct the following errors:</strong>
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <!-- Single Task Form Card -->
        <form id="task-form" action="{{ route('user.tasks.store') }}" method="POST">
            @csrf
            
            <div class="form-card">
                <!-- Project Dropdown -->
                <div class="form-group">
                    <label class="form-label" for="project_id">Project Name </label>
                    <select id="project_id" class="input-field" required>
                        <option value="" disabled selected>Select Project</option>
                        @foreach($projects as $project)
                            <!--<option value="{{ $project->id }}">{{ $project->name }}</option>-->
                            <option value="{{ $project->id }}" data-type="{{ $project->type }}">{{ $project->name }} - ({{ $project->type }})</option>
                        @endforeach
                    </select>
                </div>

                <!--<div class="form-group">-->
                <!--            <label for="type" class="form-label">Project Type</label>-->
                <!--            <div class="input-wrapper">-->
                <!--                <select name="type" id="type" class="input-field" required>-->
                <!--                    <option value="" disabled {{ old('type') === null ? 'selected' : '' }}>Select Project Type</option>-->
                <!--                    <option value="Web" {{ old('type') === 'Web' ? 'selected' : '' }}>Web Application (Web)</option>-->
                <!--                    <option value="App" {{ old('type') === 'App' ? 'selected' : '' }}>Mobile Application (App)</option>-->
                <!--                    <option value="API" {{ old('type') === 'API' ? 'selected' : '' }}>API Integration (API)</option>-->
                <!--                    <option value="UI/UX" {{ old('type') === 'UI/UX' ? 'selected' : '' }}>UI/UX Design (UI/UX)</option>-->
                <!--                </select>-->
                <!--            </div>-->
                <!--</div>-->

                <!-- Tasks Dynamic Input Section -->
                <div class="form-group">
                    <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.25rem;">
                        <label class="form-label" style="margin-bottom: 0;">Task</label>
                        <button type="button" onclick="addTaskRow()" style="background-color: var(--primary); color: #ffffff; border: none; border-radius: 6px; padding: 0.25rem 0.75rem; font-weight: 700; font-size: 0.85rem; display: inline-flex; align-items: center; gap: 0.25rem; cursor: pointer; transition: background-color 0.2s;">
                            <svg style="width: 0.9rem; height: 0.9rem;" fill="none" stroke="currentColor" stroke-width="3" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"></path></svg>
                            <span>Add Task</span>
                        </button>
                    </div>

                    <div id="tasks-input-container">
                        <div class="task-field-row" style="display: flex; gap: 0.75rem; align-items: center; margin-bottom: 0.75rem;">
                            <div style="flex: 1;">
                                <input type="text" name="tasks[0][name]" class="input-field" placeholder="Enter task Name" required>
                            </div>
                            <button type="button" class="btn-remove-row" onclick="removeTaskRow(this)" style="background: none; border: none; color: #ef4444; cursor: pointer; padding: 0.25rem; display: none; align-items: center; justify-content: center;">
                                <svg style="width: 1.25rem; height: 1.25rem;" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Shared Link Field -->
                <!-- <div class="form-group">
                    <label class="form-label" for="link">Link</label>
                    <input type="text" id="link" class="input-field" placeholder="Enter link">
                </div> -->

                <!-- Form Action Buttons -->
                <div class="form-actions-footer">
                    <a href="{{ route('user.tasks.index') }}" class="btn btn-cancel">Cancel</a>
                    <button type="submit" class="btn btn-submit">Save Tasks</button>
                </div>
            </div>
        </form>
    </main>
</div>

    <script>
        let taskRowCount = 1;

        function addTaskRow() {
            const container = document.getElementById('tasks-input-container');
            const index = taskRowCount;
            taskRowCount++;

            const rowHtml = `
                <div class="task-field-row" style="display: flex; gap: 0.75rem; align-items: center; margin-bottom: 0.75rem;">
                    <div style="flex: 1;">
                        <input type="text" name="tasks[${index}][name]" class="input-field" placeholder="Enter task title" required>
                    </div>
                    <button type="button" class="btn-remove-row" onclick="removeTaskRow(this)" style="background: none; border: none; color: #ef4444; cursor: pointer; padding: 0.25rem; display: flex; align-items: center; justify-content: center;">
                        <svg style="width: 1.25rem; height: 1.25rem;" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path></svg>
                    </button>
                </div>
            `;

            container.insertAdjacentHTML('beforeend', rowHtml);
            updateRemoveButtons();
        }

        function removeTaskRow(button) {
            const row = button.closest('.task-field-row');
            row.remove();
            reindexRows();
            updateRemoveButtons();
        }

        function reindexRows() {
            const rows = document.querySelectorAll('#tasks-input-container .task-field-row');
            taskRowCount = rows.length;
            rows.forEach((row, index) => {
                row.querySelector('input[name$="[name]"]').setAttribute('name', `tasks[${index}][name]`);
            });
        }

        function updateRemoveButtons() {
            const rows = document.querySelectorAll('#tasks-input-container .task-field-row');
            rows.forEach(row => {
                const btn = row.querySelector('.btn-remove-row');
                if (rows.length === 1) {
                    btn.style.display = 'none';
                } else {
                    btn.style.display = 'flex';
                }
            });
        }

        // Mobile Sidebar toggler + submit handler
        document.addEventListener('DOMContentLoaded', function() {
            // Populate selected project ID and status into hidden inputs upon form submission
            const form = document.getElementById('task-form');
            form.addEventListener('submit', function(e) {
                const projectSelect = document.getElementById('project_id');
                // const typeSelect = document.getElementById('type');
                const linkInput = document.getElementById('link');
                
                const selectedProjectId = projectSelect ? projectSelect.value : '';
                //const selectedType = typeSelect ? typeSelect.value : '';
                const selectedOption = projectSelect && projectSelect.selectedIndex >= 0 ? projectSelect.options[projectSelect.selectedIndex] : null;
                const selectedType = selectedOption ? selectedOption.getAttribute('data-type') : '';
                const sharedLink = linkInput ? linkInput.value : '';
                
                if (!selectedProjectId) {
                    alert('Please select a project first.');
                    e.preventDefault();
                    return;
                }
                
                // if (!selectedType) {
                //     alert('Please select a project type.');
                //     e.preventDefault();
                //     return;
                // }
                
                // Clear any existing appended inputs
                document.querySelectorAll('.appended-submit-input').forEach(el => el.remove());
                
                const rows = document.querySelectorAll('#tasks-input-container .task-field-row');
                rows.forEach((row, index) => {
                    const pInput = document.createElement('input');
                    pInput.type = 'hidden';
                    pInput.name = `tasks[${index}][project_id]`;
                    pInput.value = selectedProjectId;
                    pInput.className = 'appended-submit-input';
                    row.appendChild(pInput);
                    
                    const tInput = document.createElement('input');
                    tInput.type = 'hidden';
                    tInput.name = `tasks[${index}][type]`;
                    tInput.value = selectedType;
                    tInput.className = 'appended-submit-input';
                    row.appendChild(tInput);
                    
                    const sInput = document.createElement('input');
                    sInput.type = 'hidden';
                    sInput.name = `tasks[${index}][status]`;
                    sInput.value = 'Pending';
                    sInput.className = 'appended-submit-input';
                    row.appendChild(sInput);

                    if (sharedLink) {
                        const lInput = document.createElement('input');
                        lInput.type = 'hidden';
                        lInput.name = `tasks[${index}][link]`;
                        lInput.value = sharedLink;
                        lInput.className = 'appended-submit-input';
                        row.appendChild(lInput);
                    }
                });
            });
        });
    </script>
</body>
</html>
