<script setup lang="ts">
import { useRouter, useRoute } from "vue-router";
import { ref, useAttrs, onMounted } from "vue";
import type { QTableProps } from "quasar";
import type { FormProbationDetail } from "@/modules/05_placement/interface/request/Main";
import type { FormProbationPersonal } from "@/modules/05_placement/interface/request/Main";
import { useCounterMixin } from "@/stores/mixin";
import { date, useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";

const $q = useQuasar(); //ใช้ noti quasar
const mixin = useCounterMixin();
const {
  messageError,
  success,
  showLoader,
  hideLoader,
  date2Thai,
  dialogConfirm,
} = mixin;
const router = useRouter();
const route = useRoute();
const personalId = ref<string>(route.params.id as string);

const name = ref<string>("");
const position_line = ref<string>("");
const position_level = ref<string>("");
const organization = ref<string>("");
const probation_status = ref<string>("");
const probation_statusold = ref<string>("");

const visibleColumns = ref<string[]>([
  "no",
  "date_start",
  "date_finish",
  "mentors",
  "commander",
]);
const columns = ref<QTableProps["columns"]>([
  {
    name: "no",
    align: "left",
    label: "ลำดับ",
    sortable: true,
    field: "no",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "date_start",
    align: "left",
    label: "ตั้งแต่วันที่",
    sortable: true,
    field: "date_start",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "date_finish",
    align: "left",
    label: "ถึงวันที่",
    sortable: true,
    field: "date_finish",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "mentors",
    align: "left",
    label: "ผู้ดูแล",
    sortable: true,
    field: "mentors",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
  {
    name: "commander",
    align: "left",
    label: "ผู้บังคับบัญชา",
    sortable: true,
    field: "commander",
    headerStyle: "font-size: 14px",
    style: "font-size: 14px",
  },
]);
const rows = ref<FormProbationDetail[]>([]);

const probation_statusOP = ref<any>([
  {
    id: 1,
    label: "อยู่ระหว่างการทดลองปฏิบัติหน้าที่ราชการ",
  },
  {
    id: 2,
    label: "พ้นการทดลองปฏิบัติหน้าที่ราชการ",
  },
  {
    id: 3,
    label: "ไม่พ้นการทดลองปฏิบัติหน้าที่ราชการ",
  },
  {
    id: 4,
    label: "ยุติการทดลองปฏิบัติหน้าที่ราชการเนื่องจากเปลี่ยนตำแหน่ง",
  },
  {
    id: 5,
    label: "ยุติการทดลองปฏิบัติหน้าที่ราชการเนื่องจากลาออก",
  },
  {
    id: 6,
    label: "ยุติการทดลองปฏิบัติหน้าที่ราชการเนื่องจากถึงแก่กรรม",
  },
  {
    id: 7,
    label: "ขยายระยะเวลาทดลองปฏิบัติหน้าที่ราชการ",
  },
  {
    id: 8,
    label: "ดึงรายชื่อไปออกคำสั่งแล้ว",
  },
]);

onMounted(async () => {
  await getpersonalList();
  await getAssignList();
});
const getAssignList = async () => {
  showLoader();
  await http
    .get(config.API.probationGetAssignList(personalId.value))
    .then((res) => {
      const data = res.data.data;
      rows.value = data.map((item: FormProbationDetail) => ({
        id: item.id,
        round_no: item.round_no,
        date_start: date2Thai(new Date(item.date_start)),
        date_finish: date2Thai(new Date(item.date_finish)),
        mentors: item.mentors,
        commander: item.commander,
        chairman: item.chairman,
      }));
    })
    .catch(() => {})
    .finally(() => {
      hideLoader();
    });
};
const getpersonalList = async () => {
  showLoader();
  await http
    .get(config.API.personal(personalId.value))
    .then((res) => {
      const data = res.data.data;
      name.value = data.name;
      position_line.value = data.position_line;
      position_level.value = data.position_level;
      organization.value = data.organization;
      probation_status.value = data.probation_status;
      probation_statusold.value = data.probation_status;
    })
    .catch(() => {})
    .finally(() => {
      hideLoader();
    });
};
const selectStatus = async () => {
  dialogConfirm(
    $q,
    async () => {
      showLoader();
      await http
        .put(config.API.changestatusProbations(personalId.value), {
          status: probation_status.value,
        })
        .then((res) => {
          success($q, "แก้ไขสถานะสำเร็จ");
        })
        .catch((err) => {
          messageError($q, err);
          probation_status.value = probation_statusold.value;
        })
        .finally(() => {
          hideLoader();
          getpersonalList();
        });
    },
    "ยืนยันการเปลี่ยนสถานะทดลองงาน",
    "ต้องการเปลี่ยนสถานะทดลองงานข้อมูลนี้หรือไม่ ?",
    () => {
      probation_status.value = probation_statusold.value;
    }
  );
};
const clickSelect = (id: string) => {
  router.push(`${route.fullPath}/${id}`);
};

// ค้นหาในตาราง
const filterKeyword = ref<string>("");
const filterRef = ref<any>(null);
const resetFilter = () => {
  filterKeyword.value = "";
  filterRef.value.focus();
};

const attrs = ref<any>(useAttrs());
const pagination = ref({
  sortBy: "desc",
  descending: false,
  page: 1,
  rowsPerPage: 10,
});
const paging = ref<boolean>(true);
const paginationLabel = (start: string, end: string, total: string) => {
  if (paging.value == true) return " " + start + "-" + end + " ใน " + total;
  else return start + "-" + end + " ใน " + total;
};
</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="router.push(`/probation/`)"
    />
    รายละเอียดงานที่ได้รับมอบหมายของ {{ name }}
  </div>
  <q-card bordered class="row col-12 text-dark">
    <div class="bg-grey-1 q-pa-sm col-12 row items-center text-primary">
      <div class="q-pl-sm text-weight-bold text-subtitle2">{{ name }}</div>
      <q-space />
      <q-btn
        outline
        color="blue"
        dense
        icon-right="mdi-open-in-new"
        class="q-px-sm"
        label="ดูข้อมูลทะเบียนประวัติ"
        @click="router.push(`/registry/${personalId}`)"
      />
    </div>
    <div class="col-12"><q-separator /></div>
    <div class="row col-12 q-pa-md">
      <div class="col-12 row bg-white q-col-gutter-md">
        <div class="col-xs-3 col-sm-2 col-md-1 row">
          <q-img src="@/assets/avatar_user.jpg" />
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12 q-pl-md">
            <div class="col-12 text-top">ตำแหน่งในสายงาน</div>
            <div class="col-12 text-detail">{{ position_line }}</div>
          </div>
        </div>
        <div class="col-xs-6 col-sm-2 row items-center">
          <div class="col-12">
            <div class="col-12 text-top">ระดับ</div>
            <div class="col-12 text-detail">{{ position_level }}</div>
          </div>
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12">
            <div class="col-12 text-top">สังกัด</div>
            <div class="col-12 text-detail">{{ organization }}</div>
          </div>
        </div>
        <div class="col-xs-6 col-sm-3 row items-center">
          <div class="col-12">
            <div class="col-12 text-top">สถานะการทดลองงาน</div>
            <div class="col-12 text-detail">
              <q-select
                dense
                borderless
                v-model="probation_status"
                :options="probation_statusOP"
                option-label="label"
                option-value="id"
                @update:model-value="selectStatus"
                emit-value
                map-options
              />
              <!-- <q-icon
                size="20px"
                v-if="probation_status === 'PENDING'"
                name="mdi-timer-sand"
                color="deep-orange"
              />
              <q-icon
                size="20px"
                v-else-if="probation_status === 'NO-PASS'"
                name="mdi-close"
                color="red"
              />
              <q-icon
                size="20px"
                v-else-if="probation_status === 'PROCESS'"
                name="mdi-timer-sand"
                color="deep-orange"
              />
              <q-icon
                size="20px"
                v-else-if="probation_status === 'PASS'"
                name="mdi-check"
                color="teal"
              />
              <q-icon size="20px" v-else name="mdi-check" color="teal" />
              {{
                probation_status === "PENDING"
                  ? "รอมอบหมายงาน"
                  : probation_status === "NO-PASS"
                  ? "ไม่ผ่านการทดลองงาน"
                  : probation_status === "PROCESS"
                  ? "อยู่ในระหว่างการทดลองงาน"
                  : probation_status === "PASS"
                  ? "ผ่านการทดลองงาน"
                  : ""
              }} -->
            </div>
          </div>
        </div>
      </div>
    </div>
  </q-card>
  <q-card flat bordered class="col-12 q-mt-sm q-pa-md">
    <div class="row q-col-gutter-sm">
      <div class="row col-12 q-col-gutter-sm">
        <div>
          <q-btn
            @click="router.push(`/probation/add/${personalId}`)"
            size="12px"
            flat
            round
            color="add"
            icon="mdi-plus"
          >
            <q-tooltip>เพิ่มงานที่ได้รับมอบหมาย</q-tooltip>
          </q-btn>
        </div>
        <q-space />
        <q-input
          class="col-xs-12 col-sm-3 col-md-2"
          standout
          dense
          v-model="filterKeyword"
          ref="filterRef"
          outlined
          debounce="300"
          placeholder="ค้นหา"
        >
          <template v-slot:append>
            <q-icon v-if="filterKeyword == ''" name="search" />
            <q-icon
              v-if="filterKeyword !== ''"
              name="clear"
              class="cursor-pointer"
              @click="resetFilter"
            />
          </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="col-xs-12 col-sm-3 col-md-2"
        />
      </div>
      <div class="col-12">
        <q-table
          ref="table"
          :columns="columns"
          :rows="rows"
          :filter="filterKeyword"
          row-key="Order"
          flat
          bordered
          :paging="true"
          dense
          class="custom-header-table"
          v-bind="attrs"
          :visible-columns="visibleColumns"
          :pagination-label="paginationLabel"
          v-model:pagination="pagination"
        >
          <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"
              @click="clickSelect(props.row.id)"
            >
              <q-td v-for="col in props.cols" :key="col.name" :props="props">
                <div v-if="col.name == 'no'">
                  {{ props.rowIndex + 1 }}
                </div>
                <div v-else>
                  {{ col.value }}
                </div>
              </q-td>
              <!-- <q-td auto-width>
								<q-btn flat dense size="12px" round color="blue" icon="mdi-file-download-outline">
									<q-tooltip> ดาวน์โหลด .Docx</q-tooltip>
								</q-btn>
							</q-td> -->
            </q-tr>
          </template>
          <template v-slot:pagination="scope">
            <q-pagination
              v-model="pagination.page"
              active-color="primary"
              color="dark"
              :max="scope.pagesNumber"
              :max-pages="5"
              size="sm"
              boundary-links
              direction-links
            ></q-pagination>
          </template>
        </q-table>
      </div>
    </div>
  </q-card>
</template>

<style lang="scss" scope>
.q-img {
  border-radius: 5px;
  height: 70px;
}
.text-top {
  color: gray;
  font-weight: 400;
  padding-bottom: 3px;
}
.text-detail {
  font-weight: 500;
}
.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;
  }

  /* this will be the loading indicator */
  .q-table thead tr:last-child th {
    /* height of all previous header rows */
    top: 48px;
  }

  .q-table thead tr:first-child th {
    top: 0;
  }
}
</style>
