<template>
  <div class="task-assign-view">
    <h2>Assign Tasks</h2>
    <div class="task-view">
      <MessageBlock v-if="taskCountRef === 0" message="No tasks">
        <span> <button class="round-btn" @click="goToCreateTask">Create</button> a task </span>
      </MessageBlock>
      <ItemList
        v-else
        ref="taskListRef"
        :fetchUrl="`/api/child/${childId}/list-all-tasks?type=${typeFilter}`"
        itemKey="tasks"
        :itemFields="TASK_FIELDS"
        imageField="image_id"
        selectable
        @loading-complete="(count) => (taskCountRef = count)"
        :getItemClass="(item) => ({ bad: !item.is_good, good: item.is_good })"
      >
        <template #item="{ item }">
          <img v-if="item.image_url" :src="item.image_url" />
          <span class="name">{{ item.name }}</span>
          <span class="value">{{ item.points }} pts</span>
        </template>
      </ItemList>
    </div>
    <div class="actions" v-if="taskCountRef > 0">
      <button class="btn btn-secondary" @click="onCancel">Cancel</button>
      <button class="btn btn-primary" @click="onSubmit">Submit</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import ItemList from '../shared/ItemList.vue'
import MessageBlock from '../shared/MessageBlock.vue'
import '@/assets/styles.css'
import { TASK_FIELDS } from '@/common/models'

const route = useRoute()
const router = useRouter()
const childId = route.params.id

const taskListRef = ref()
const taskCountRef = ref(-1)

const typeFilter = computed(() => {
  if (route.params.type === 'good') return 'good'
  if (route.params.type === 'bad') return 'bad'
  return 'all'
})

function goToCreateTask() {
  router.push({ name: 'CreateTask' })
}

async function onSubmit() {
  const selectedIds = taskListRef.value?.selectedItems ?? []
  try {
    const resp = await fetch(`/api/child/${childId}/set-tasks`, {
      method: 'PUT',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ type: typeFilter.value, task_ids: selectedIds }),
    })
    if (!resp.ok) throw new Error('Failed to update tasks')
    router.back()
  } catch (err) {
    alert('Failed to update tasks.')
  }
}

function onCancel() {
  router.back()
}
</script>

<style scoped>
.task-assign-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  padding: 0;
  min-height: 0;
}
.task-assign-view h2 {
  font-size: 1.15rem;
  color: var(--assign-heading-color);
  font-weight: 700;
  text-align: center;
  margin: 0.2rem;
}

.task-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  padding: 0;
  min-height: 0;
}

:deep(.good) {
  border-color: var(--list-item-border-good);
  background: var(--list-item-bg-good);
}
:deep(.bad) {
  border-color: var(--list-item-border-bad);
  background: var(--list-item-bg-bad);
}

.name {
  flex: 1;
  text-align: left;
  font-weight: 600;
}

.value {
  min-width: 60px;
  text-align: right;
  font-weight: 600;
}

.actions {
  display: flex;
  gap: 3rem;
  justify-content: center;
  margin-top: 0.5rem;
}
.actions button {
  padding: 1rem 2.2rem;
  border-radius: 12px;
  border: 0;
  cursor: pointer;
  font-weight: 700;
  font-size: 1.25rem;
  transition: background 0.18s;
  min-width: 120px;
}
</style>
