/**
 * 科技平台与服务：tsT（techService.tsNs）、tsCommon
 */
export default {
  methods: {
    tsT(key, values) {
      const ns = this.tsNs
      if (!ns) {
        return key
      }
      return this.$t(`techService.${ns}.${key}`, values)
    },
    tsCommon(key) {
      return this.$t(`techService.common.${key}`)
    },
    publishStatusText(v) {
      if (v === 0) {
        return this.$t("techService.status.unpublished")
      }
      if (v === 1) {
        return this.$t("techService.status.published")
      }
      return this.$t("techService.status.dash")
    },
    /** 列表三态：0 未发布 1 已发布 2 已下架 */
    displayPublishStatusText(code) {
      if (code === 1) {
        return this.$t("techService.status.published")
      }
      if (code === 2) {
        return this.$t("techService.status.offline")
      }
      if (code === 0) {
        return this.$t("techService.status.unpublished")
      }
      return this.$t("techService.status.dash")
    },
    /** 资源类型：004005～004008 或存量 1～4 */
    resourceTypeText(type) {
      const legacyKey = { 1: "resourceType1", 2: "resourceType2", 3: "resourceType3", 4: "resourceType4" }
      const codeKey = {
        "004005": "resourceType1",
        "004006": "resourceType2",
        "004007": "resourceType3",
        "004008": "resourceType4"
      }
      const key = legacyKey[type] || codeKey[String(type)]
      if (key) {
        return this.$t(`techService.techResource.${key}`)
      }
      return type != null && type !== "" ? String(type) : this.$t("techService.status.dash")
    },
    /** 入库状态：0 未入库 1 已入库 */
    kbSyncStatusText(v) {
      if (v === 0) {
        return this.$t("techService.status.kbNotIn")
      }
      if (v === 1) {
        return this.$t("techService.status.kbIn")
      }
      return this.$t("techService.status.dash")
    },
    /** 确认后执行同步知识库，带全局 loading */
    runSyncKbAfterConfirm(confirmMessage, syncFn) {
      let syncLoading = false
      return this.$modal
        .confirm(confirmMessage)
        .then(() => {
          syncLoading = true
          this.$modal.loading(this.tsCommon("syncKbLoading"))
          return syncFn()
        })
        .finally(() => {
          if (syncLoading) {
            this.$modal.closeLoading()
          }
        })
    },
    /** 确认后执行移出知识库，带全局 loading */
    runRemoveKbAfterConfirm(confirmMessage, removeFn) {
      let removeLoading = false
      return this.$modal
        .confirm(confirmMessage)
        .then(() => {
          removeLoading = true
          this.$modal.loading(this.tsCommon("removeKbLoading"))
          return removeFn()
        })
        .finally(() => {
          if (removeLoading) {
            this.$modal.closeLoading()
          }
        })
    },
    knowledgeTopicText(topic) {
      if (topic >= 1 && topic <= 5) {
        return this.$t(`techService.knowledge.topic${topic}`)
      }
      return this.$t("techService.status.dash")
    },
    /** 专家预约状态 0～4（与兽医接单制一致） */
    expertAppointmentStatusText(status) {
      if (status >= 0 && status <= 4) {
        return this.$t(`techService.expertMyAppointment.status${status}`)
      }
      return this.$t("techService.status.dash")
    },
    expertAppointmentStatusTagType(status) {
      const map = { 0: "warning", 1: "success", 2: "danger", 3: "info", 4: "" }
      return map[status] != null ? map[status] : ""
    }
  }
}
