<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
        <el-form-item prop="appointeeName">
          <template slot="label">{{ dtT("queryAppointeeName") }}</template>
          <el-input
            v-model="queryParams.appointeeName"
            :placeholder="dtT('queryAppointeeNamePh')"
            clearable
            style="width: 160px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <template slot="label">{{ dtT("queryAppointDate") }}</template>
          <el-date-picker
            v-model="queryDateRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="-"
            :start-placeholder="dtT('queryDateRangePh')"
            :end-placeholder="dtT('queryDateRangeEndPh')"
            clearable
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item prop="status">
          <template slot="label">{{ dtT("queryStatus") }}</template>
          <el-select v-model="queryParams.status" :placeholder="dtCommon('pleaseSelect')" clearable style="width: 140px">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ dtCommon("search") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ dtCommon("reset") }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <br />

    <el-card shadow="never">
      <el-table v-loading="loading" :data="tableList" border>
        <template slot="empty">
          <span>{{ dtT("emptyList") }}</span>
        </template>
        <el-table-column :label="dtT('colAppointeeName')" prop="appointeeName" align="center" min-width="100" :show-overflow-tooltip="true" />
        <el-table-column :label="dtT('colAppointDate')" prop="appointDate" align="center" width="120" />
        <el-table-column :label="dtT('colContactPhone')" prop="contactPhone" align="center" width="130" :show-overflow-tooltip="true" />
        <el-table-column :label="dtT('colStatus')" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="orgAppointmentStatusTagType(scope.row.status)" :type="orgAppointmentStatusTagType(scope.row.status)" size="mini">
              {{ displayStatus(scope.row) }}
            </el-tag>
            <span v-else>{{ displayStatus(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="dtCommon('colOp')" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['diseaseTreatment:orgMyAppointment:query']"
              type="text"
              size="mini"
              @click="handleView(scope.row)"
            >{{ dtCommon("view") }}</el-button>
            <el-button
              v-if="canComplete(scope.row)"
              v-hasPermi="['diseaseTreatment:orgMyAppointment:complete']"
              type="text"
              size="mini"
              @click="handleComplete(scope.row)"
            >{{ dtT("actionComplete") }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <el-dialog :title="dtT('viewTitle')" :visible.sync="viewOpen" width="640px" append-to-body>
      <el-descriptions v-if="viewRow.id" :column="1" border size="small">
        <el-descriptions-item :label="dtT('viewAppointeeName')">{{ viewRow.appointeeName || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewAppointDate')">{{ viewRow.appointDate || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewTimeSlot')">{{ formatTimeSlot(viewRow) || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewContactPhone')">{{ viewRow.contactPhone || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewServiceAddress')">{{ viewRow.serviceAddress || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewServiceRequirement')">{{ viewRow.serviceRequirement || dash }}</el-descriptions-item>
        <el-descriptions-item :label="dtT('viewStatus')">{{ displayStatus(viewRow) }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">{{ dtCommon("close") }}</el-button>
        <el-button
          v-if="canComplete(viewRow)"
          v-hasPermi="['diseaseTreatment:orgMyAppointment:complete']"
          type="primary"
          @click="handleComplete(viewRow)"
        >{{ dtT("actionComplete") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import diseaseTreatmentLocaleMixin from "@/mixins/diseaseTreatmentLocaleMixin"
import { listOrgMyAppointment, getOrgMyAppointment, completeOrgMyAppointment } from "@/api/diseaseTreatment/orgMyAppointment"

const ORG_STATUS_BOOKED = 5
const ORG_STATUS_CANCELLED = 3
const ORG_STATUS_COMPLETED = 4

export default {
  name: "DiseaseTreatmentOrgMyAppointment",
  mixins: [diseaseTreatmentLocaleMixin],
  data() {
    return {
      dtNs: "orgMyAppointment",
      loading: false,
      tableList: [],
      total: 0,
      queryDateRange: null,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        appointeeName: undefined,
        startDate: undefined,
        endDate: undefined,
        status: undefined
      },
      viewOpen: false,
      viewRow: {}
    }
  },
  computed: {
    dash() {
      return this.$t("diseaseTreatment.status.dash")
    },
    statusOptions() {
      return [
        { value: ORG_STATUS_BOOKED, label: this.orgAppointmentStatusText(ORG_STATUS_BOOKED) },
        { value: ORG_STATUS_CANCELLED, label: this.orgAppointmentStatusText(ORG_STATUS_CANCELLED) },
        { value: ORG_STATUS_COMPLETED, label: this.orgAppointmentStatusText(ORG_STATUS_COMPLETED) }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      if (!this.checkQueryDateRange()) {
        return
      }
      this.syncDateRangeToQuery()
      this.loading = true
      listOrgMyAppointment(this.queryParams)
        .then((res) => {
          this.tableList = res.rows || []
          this.total = res.total || 0
        })
        .finally(() => {
          this.loading = false
        })
    },
    checkQueryDateRange() {
      if (this.queryDateRange && this.queryDateRange.length === 2) {
        const [start, end] = this.queryDateRange
        if (start && end && start > end) {
          this.$modal.msgWarning(this.dtT("dateRangeInvalid"))
          return false
        }
      }
      return true
    },
    syncDateRangeToQuery() {
      if (this.queryDateRange && this.queryDateRange.length === 2) {
        this.queryParams.startDate = this.queryDateRange[0]
        this.queryParams.endDate = this.queryDateRange[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryDateRange = null
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        appointeeName: undefined,
        startDate: undefined,
        endDate: undefined,
        status: undefined
      }
      this.$nextTick(() => {
        if (this.$refs.queryForm) {
          this.$refs.queryForm.resetFields()
        }
      })
      this.getList()
    },
    formatTimeSlot(row) {
      if (!row) {
        return ""
      }
      const start = row.timeStart
      const end = row.timeEnd
      if (start && end) {
        return `${start}~${end}`
      }
      return start || end || ""
    },
    displayStatus(row) {
      if (!row || row.status == null) {
        return this.dash
      }
      if (row.statusLabel) {
        return row.statusLabel
      }
      return this.orgAppointmentStatusText(row.status)
    },
    canComplete(row) {
      if (!row) {
        return false
      }
      if (row.canComplete != null) {
        return !!row.canComplete
      }
      return row.status === ORG_STATUS_BOOKED
    },
    handleView(row) {
      getOrgMyAppointment(row.id).then((res) => {
        this.viewRow = res.data || {}
        this.viewOpen = true
      })
    },
    handleComplete(row) {
      if (!row || !row.id) {
        return
      }
      this.$modal
        .confirm(this.dtT("confirmCompleteMsg"))
        .then(() => completeOrgMyAppointment(row.id))
        .then(() => {
          this.$modal.msgSuccess(this.dtT("msgCompleteOk"))
          this.viewOpen = false
          this.getList()
        })
        .catch(() => {})
    }
  }
}
</script>
