<script setup lang="ts">
import { ref, onMounted, computed, watch } from "vue";
import AddList from "../ListRetirement/AddList.vue";
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";
import { useRoute } from "vue-router";
import http from "@/plugins/http";
import config from "@/app.config";

const route = useRoute();
const $q = useQuasar(); //ใช้ noti quasar
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError, success } = mixin;
const no = route.params.id;
const modalNote = ref<boolean>(false);
const note = ref<string>("");

onMounted(() => {
  console.log(no);
  fecthlist();
  console.log(visibleNote.value);
});
const fecthlist = async () => {
  showLoader();
  await http
    .get(config.API.profile("emplayee", 2023))
    .then((res: any) => {
      console.log(res);
    })
    .catch((e: any) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};
const columns = ref<any["columns"]>([
  {
    name: "index",
    required: true,
    label: "ลำดับ",
    field: "index",
    align: "left",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "fixname",
    align: "left",
    label: "คำนำหน้านาม",
    field: "fixname",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "name",
    align: "left",
    label: "ชื่อ - นามสกุล",
    field: "name",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "brand",
    align: "left",
    label: "สังกัด",
    field: "brand",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionType",
    align: "left",
    label: "ตำแหน่ง",
    sublabel: "ประเภท",
    field: "positionType",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionManager",
    align: "left",
    label: "ตำแหน่ง ",
    sublabel: "ทางการบริหาร",
    field: "positionManager",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionLavel",
    align: "left",
    label: "ตำแหน่ง ",
    sublabel: "ในสายงาน/ระดับ",
    field: "positionLavel",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "positionNumber",
    align: "left",
    label: "ตำแหน่ง ",
    sublabel: "เลขที่",
    field: "positionNumber",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "govGroup",
    align: "left",
    label: "ส่วนราชการ ",
    sublabel: "ระดับฝ่าย/กลุ่มงาน/ส่วน",
    field: "govGroup",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "govOffice",
    align: "left",
    label: "ส่วนราชการ ",
    sublabel: "ระดับสถาบัน/สำนักงาน/กอง",
    field: "govOffice",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "bureau",
    align: "left",
    label: "หน่วยงาน",
    field: "bureau",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "note",
    align: "left",
    label: "",
    field: "",
  },
  {
    name: "button",
    align: "left",
    label: "",
    field: "",
  },
]);
const rows = ref<any>([
  {
    fixname: "นางสาว",
    name: "รัชภรณ์ ภัคดี",
    brand: "ฝ่ายบริหารทั่วไป",
    positionType: "บริหาร",
    positionManager: "จัดการทั่วไป",
    positionLavel: "ชำนาญการ",
    positionNumber: "กบห.2",
    govGroup: "ฝ่ายบริหารทั่วไป",
    govOffice: "ฝ่ายบริหารทั่วไป",
    bureau: "บริหาร",
  },
  {
    fixname: "นางสาว",
    name: "รัชภรณ์ ภัคดี",
    brand: "ฝ่ายบริหารทั่วไป",
    positionType: "บริหาร",
    positionManager: "จัดการทั่วไป",
    positionLavel: "ชำนาญการ",
    positionNumber: "กบห.2",
    govGroup: "ฝ่ายบริหารทั่วไป",
    govOffice: "ฝ่ายบริหารทั่วไป",
    bureau: "บริหาร",
  },
  {
    fixname: "นางสาว",
    name: "รัชภรณ์ ภัคดี",
    brand: "ฝ่ายบริหารทั่วไป",
    positionType: "บริหาร",
    positionManager: "จัดการทั่วไป",
    positionLavel: "ชำนาญการ",
    positionNumber: "กบห.2",
    govGroup: "ฝ่ายบริหารทั่วไป",
    govOffice: "ฝ่ายบริหารทั่วไป",
    bureau: "บริหาร",
  },
]);
const visibleColumns = ref<any>([
  "fixname",
  "name",
  "brand",
  "positionType",
  "positionManager",
  "positionLavel",
  "positionNumber",
  "govGroup",
  "govOffice",
  "bureau",
  "note",
  "button",
]);
const filter = ref<string>("");

const clickDelete = (prop: any) => {
  // console.log(prop);
  $q.dialog({
    title: "ยืนยันการลบข้อมูล",
    message: "ต้องการลบข้อมูลนี้ใช่หรือไม่?",
    cancel: {
      flat: true,
      color: "negative",
    },
    persistent: true,
  })
    .onOk(async () => {
      await http
        .delete(config.API.profileRetire("123"))
        .then(() => {
          success($q, "ลบข้อมูลสำเร็จ");
        })
        .catch((e) => {
          messageError($q, e);
        })
        .finally(() => {
          fecthlist();
        });
    })
    .onCancel(() => {})
    .onDismiss(() => {});
};
const backHistory = () => {
  window.history.back();
};
watch(modalNote, () => {
  if (modalNote.value === true) {
    fetchReason();
  }
});
const fetchReason = async () => {
  await http
    .get(config.API.reasonId("123"))
    .then((res: any) => {
      console.log(res);
    })
    .catch((e: any) => {
      messageError($q, e);
    })
    .finally(() => {
      hideLoader();
    });
};
const saveNote = () => {
  console.log(note.value);
  $q.dialog({
    title: "ยืนยันการบันทึกข้อมูลข้อมูล",
    message: "ต้องการบันทึกข้อมูลนี้ใช่หรือไม่?",
    cancel: {
      flat: true,
      color: "negative",
    },
    persistent: true,
  })
    .onOk(async () => {
      console.log("บันทึกข้อมูล");
      await http
        .post(config.API.createnote(), note.value)
        .then(() => {
          success($q, "บันทึกข้อมูล");
        })
        .catch((e) => {
          messageError($q, e);
        })
        .finally(() => {
          fecthlist();
        });
    })
    .onCancel(() => {})
    .onDismiss(() => {});
};
const visibleNote = computed(() => {
  if (note.value === "" || note.value === undefined) {
    return true;
  }
});
const saveList = () => {
  $q.dialog({
    title: "ยืนยันกาบันทึกข้อมูล",
    message: "ต้องการบันทึกข้อมูลนี้ใช่หรือไม่?",
    cancel: {
      flat: true,
      color: "negative",
    },
    persistent: true,
  })
    .onOk(async () => {
      const retireld = "1233444";
      await http
        .post(config.API.listRetire("123"), { retireld: retireld })
        .then(() => {
          success($q, "บันทึกข้อมูลสำเร็จ");
        })
        .catch((e) => {
          messageError($q, e);
        })
        .finally(() => {
          fecthlist();
        });
    })
    .onCancel(() => {})
    .onDismiss(() => {});
};
</script>

<template>
  <div class="toptitle text-dark col-12 row items-center">
    <q-btn
      icon="mdi-arrow-left"
      unelevated
      round
      dense
      flat
      color="primary"
      class="q-mr-sm"
      @click="backHistory"
    />
    รายชื่อผู้เกษียณอายุราชการ
  </div>
  <div>
    <q-card class="col-12 q-pa-md">
      <q-toolbar>
        <AddList />
        <q-btn flat round color="blue-12" icon="save" @click="saveList">
          <q-tooltip>บันทึกข้อมูล</q-tooltip>
        </q-btn>
        <q-btn color="primary" flat round icon="download">
          <q-menu>
            <q-list style="min-width: 100px">
              <q-item clickable>
                <q-item-section avatar>
                  <q-icon color="red" name="mdi-file-pdf" />
                </q-item-section>
                <q-item-section>ไฟล .PDF</q-item-section>
              </q-item>
              <q-item clickable>
                <q-item-section avatar>
                  <q-icon color="green" name="mdi-file-excel" />
                </q-item-section>
                <q-item-section>ไฟล .xls</q-item-section>
              </q-item>
            </q-list>
          </q-menu>
          <q-tooltip>ดาวน์โหลดไฟล์</q-tooltip>
        </q-btn>
        <q-btn flat round color="light-blue" icon="upload">
          <q-tooltip>อัพโหลดไฟล์</q-tooltip>
        </q-btn>

        <q-space />
        <q-tabs shrink>
          <q-input
            borderless
            outlined
            dense
            debounce="300"
            v-model="filter"
            placeholder="ค้นหา"
            style="max-width: 200px"
            class="q-ml-sm"
          >
            <template v-slot:append>
              <q-icon name="search" />
            </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"
            class="gt-xs q-ml-sm"
          />
        </q-tabs>
      </q-toolbar>

      <div class="q-pa-md">
        <q-table
          flat
          bordered
          :rows="rows"
          :columns="columns"
          row-key="name"
          class="custom-header-table"
          hide-bottom
          :filter="filter"
          :visible-columns="visibleColumns"
          no-data-label="ไม่มีข้อมูล"
        >
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th v-for="col in props.cols" :key="col.name" :props="props">
                <div class="text-grey-7 text-weight-medium">
                  <span class="row">{{ col.label }}</span>
                  <span class="row" style="font-size: 12px">{{
                    col.sublabel
                  }}</span>
                </div>
              </q-th>
            </q-tr>
          </template>
          <template v-slot:body="props">
            <q-tr :props="props" class="cursor-pointer">
              <q-td>{{ props.rowIndex + 1 }}</q-td>
              <q-td key="fixname" :props="props">{{ props.row.fixname }}</q-td>
              <q-td key="name" :props="props">{{ props.row.name }}</q-td>
              <q-td key="brand" :props="props">{{ props.row.brand }}</q-td>
              <q-td key="positionType" :props="props">{{
                props.row.positionType
              }}</q-td>
              <q-td key="positionManager" :props="props">{{
                props.row.positionManager
              }}</q-td>
              <q-td key="positionLavel" :props="props">{{
                props.row.positionLavel
              }}</q-td>
              <q-td key="positionNumber" :props="props">{{
                props.row.positionNumber
              }}</q-td>
              <q-td key="govGroup" :props="props">{{
                props.row.govGroup
              }}</q-td>
              <q-td key="govOffice" :props="props">{{
                props.row.govOffice
              }}</q-td>
              <q-td key="bureau" :props="props">{{ props.row.bureau }}</q-td>
              <q-td>
                <q-btn
                  :props="props"
                  flat
                  round
                  class="text-teal-5"
                  icon="mdi-note"
                  size="md"
                  v-if="no !== undefined"
                  @click="(modalNote = true), (note = '')"
                >
                  <q-tooltip>โน้ต</q-tooltip></q-btn
                >
              </q-td>
              <q-td>
                <q-btn
                  flat
                  round
                  class="text-red-14"
                  icon="mdi-delete"
                  size="md"
                  @click="clickDelete(props.row)"
                  ><q-tooltip>ลบข้อมูล</q-tooltip></q-btn
                >
              </q-td>
            </q-tr>
          </template>
        </q-table>
      </div>
    </q-card>
  </div>

  <!-- note -->
  <q-dialog v-model="modalNote">
    <q-card style="width: 700px; max-width: 80vw">
      <q-card-section class="q-pt-none">
        <q-toolbar class="q-py-md">
          <q-toolbar-title class="header-text text-weight-bolder"
            >กรอกเหตุผล
          </q-toolbar-title>
          <q-btn
            icon="close"
            unelevated
            round
            dense
            @click="modalNote = false"
            style="color: #ff8080; background-color: #ffdede"
          />
        </q-toolbar>
      </q-card-section>
      <q-card-section class="q-pt-none">
        <q-input
          outlined
          dense
          lazy-rules
          v-model="note"
          :rules="[(val) => !!val || 'กรุณากรอกเหตุผล']"
          :label="`${'กรอกเหตุผล'}`"
          type="textarea"
        />
      </q-card-section>

      <q-card-actions align="right">
        <q-btn
          label="บักทึก"
          color="secondary"
          @click="saveNote"
          :disable="visibleNote"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<style lang="scss" scope>
.custom-header-table {
  max-height: 64vh;

  .q-table tr:nth-child(odd) td {
    background: white;
  }

  .q-table tr:nth-child(even) td {
    background: #f8f8f8;
  }

  .q-table thead tr {
    background: #ecebeb;
  }

  .q-table thead tr th {
    position: sticky;
    z-index: 1;
  }
  .q-table thead tr:last-child th {
    top: 48px;
  }
  .q-table thead tr:first-child th {
    top: 0;
  }
}
.q-btn-dropdown__arrow {
  display: none;
}
</style>
