<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { useQuasar, type QTableProps } from "quasar";

/** importStore*/
import { useCounterMixin } from "@/stores/mixin";

const $q = useQuasar();
const router = useRouter();
const { showLoader, hideLoader, dialogRemove, success, messageError } =
  useCounterMixin();

/** table*/
const columns = ref<QTableProps["columns"]>([
  {
    name: "including",
    align: "left",
    label: "รหัสตัวชี้วัด",
    sortable: true,
    field: "including",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "includingName",
    align: "left",
    label: "ชื่อตัวชี้วัด",
    sortable: true,
    field: "includingName",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const visibleColumns = ref<string[]>(["including", "includingName"]);
const rows = ref<any[]>([]);
const filter = ref<string>("");

function onClickView(id: string = "") {
  router.push(`/assignment/${id}`);
}

function onClickHistory(id: string) {}

function onClickDelete(id: string) {
  dialogRemove($q, () => {
    console.log(id);
  });
}

onMounted(() => {
  showLoader();
  const list = [
    {
      id: "1",
      including: "12",
      includingName: "name",
    },
  ];

  setTimeout(() => {
    rows.value = list;
    hideLoader();
  }, 100);
});
</script>

<template>
  <div class="toptitle text-dark col-12 row items-center">
    รายการงานที่ได้รับมอบหมาย
  </div>
  <q-card flat bordered class="q-pa-md">
    <div class="row q-col-gutter-sm q-pl-sm">
      <q-toolbar style="padding: 0">
        <q-space />
        <div class="row q-gutter-sm">
          <q-input dense outlined v-model="filter" label="ค้นหา">
            <template v-slot:append>
              <q-icon
                v-if="filter !== ''"
                name="clear"
                class="cursor-pointer"
                @click="filter = ''"
              />
              <q-icon v-else name="search" color="grey-5" />
            </template>
          </q-input>
          <q-select
            v-model="visibleColumns"
            multiple
            outlined
            dense
            options-dense
            :display-value="$q.lang.table.columns"
            emit-value
            map-options
            :options="columns"
            option-value="name"
            options-cover
            style="min-width: 150px"
          />
        </div>
      </q-toolbar>
    </div>
    <div class="col-12">
      <d-table
        for="table"
        ref="table"
        :columns="columns"
        :rows="rows"
        row-key="id"
        flat
        bordered
        dense
        class="custom-header-table"
        :rows-per-page-options="[10, 25, 50, 100]"
        :visible-columns="visibleColumns"
      >
        <template v-slot:header="props">
          <q-tr :props="props">
            <q-th v-for="col in props.cols" :key="col.name" :props="props">
              <span class="text-weight-medium">{{ col.label }}</span>
            </q-th>
            <q-th auto-width />
          </q-tr>
        </template>
        <template v-slot:body="props">
          <q-tr :props="props" class="cursor-pointer">
            <q-td v-for="col in props.cols" :key="col.name" :props="props">
              <div class="table_ellipsis">
                {{ col.value ? col.value : "-" }}
              </div>
            </q-td>
            <q-td auto-width>
              <q-btn
                flat
                round
                color="grey"
                @click.stop
                size="14px"
                icon="more_vert"
              >
                <q-menu>
                  <q-list style="min-width: 180px">
                    <q-item
                      clickable
                      v-close-popup
                      @click="onClickView(props.row.id)"
                    >
                      <q-item-section
                        style="min-width: 0px"
                        avatar
                        class="q-py-sm"
                      >
                        <q-tooltip>แก้ไขข้อมูล</q-tooltip>
                        <q-icon
                          color="primary"
                          size="xs"
                          name="mdi-pencil-outline"
                        />
                      </q-item-section>
                      <q-item-section>แก้ไขข้อมูล</q-item-section>
                    </q-item>
                    <q-item
                      clickable
                      v-close-popup
                      @click="onClickHistory(props.row.id)"
                    >
                      <q-item-section
                        style="min-width: 0px"
                        avatar
                        class="q-py-sm"
                      >
                        <q-tooltip>ประวัติการแก้ไข</q-tooltip>
                        <q-icon color="info" size="xs" name="mdi-history" />
                      </q-item-section>
                      <q-item-section>ประวัติการแก้ไข</q-item-section>
                    </q-item>
                    <q-item
                      clickable
                      v-close-popup
                      @click.stop.pervent="onClickDelete(props.row.id)"
                    >
                      <q-item-section
                        style="min-width: 0px"
                        avatar
                        class="q-py-sm"
                      >
                        <q-tooltip>ลบข้อมูล</q-tooltip>
                        <q-icon color="red" size="xs" name="mdi-delete" />
                      </q-item-section>
                      <q-item-section>ลบข้อมูล</q-item-section>
                    </q-item>
                  </q-list>
                </q-menu>
              </q-btn>
            </q-td>
          </q-tr>
        </template>
      </d-table>
    </div>
  </q-card>
</template>

<style scoped></style>
