<template>
  <business-page-layout :title="title" :show-back="true">
    <view class="page-wrap">
      <view v-if="!auditMode" class="toolbar">
        <button class="btn" @click="goForm()">新建录入</button>
      </view>
      <view v-for="item in list" :key="item.id" class="card" @click="goDetail(item)">
        <view class="row">
          <text class="name">{{ item.name }}</text>
          <text class="status" :class="'s' + item.status">{{ statusText(item.status) }}</text>
        </view>
        <view class="sub">{{ item.mobile }} · {{ displayTime(item.created_at) }}</view>
      </view>
      <view v-if="!list.length" class="empty">暂无记录</view>
    </view>
  </business-page-layout>
</template>

<script>
import BusinessPageLayout from '@/subPackages/sub_platform_admin/components/business-page-layout.vue'
import businessMixin from '@/subPackages/sub_platform_admin/common/businessMixin.js'

export default {
  mixins: [businessMixin],
  components: { BusinessPageLayout },
  props: {
    auditMode: { type: Boolean, default: false },
    inputType: { type: String, default: 'store' },
    formPath: { type: String, default: '' },
    detailPath: { type: String, required: true },
  },
  data() { return { list: [], page: 1 } },
  computed: {
    title() {
      if (this.inputType === 'doctor') return this.auditMode ? '医生信息审核' : '医生信息预填'
      return this.auditMode ? '诊所信息审核' : '诊所信息录入'
    },
  },
  mounted() {
    this.reload()
  },
  methods: {
    listApi(params) {
      return this.inputType === 'doctor'
        ? this.bizApi.getDoctorInputList(params)
        : this.bizApi.getStoreInputList(params)
    },
    parseListItems(data) {
      if (Array.isArray(data)) return data
      if (data && Array.isArray(data.items)) return data.items
      if (data && Array.isArray(data.data)) return data.data
      return []
    },
    reload() { this.page = 1; this.load(1, false) },
    load(page, append) {
      const params = { page, pageSize: 15 }
      if (this.auditMode) params.status = 0
      this.listApi(params)
        .then(w => {
          const items = this.parseListItems(w.data)
          if (!items.length && !w.ok) {
            uni.showToast({ title: (w.res && (w.res.message || w.res.msg)) || '加载失败', icon: 'none' })
            return
          }
          this.list = append ? this.list.concat(items) : items
          this.page = page
        })
        .catch(() => {
          uni.showToast({ title: '加载失败', icon: 'none' })
        })
    },
    statusText(s) {
      return { 0: '待审核', 1: '已通过', 2: '已拒绝' }[Number(s)] || '-'
    },
    displayTime(t) {
      if (t == null || t === '') return '-'
      if (typeof t === 'string' && (t.includes('-') || t.includes(':'))) {
        return t.length >= 10 ? t.slice(0, 10) : t
      }
      const n = Number(t)
      if (!Number.isFinite(n) || n <= 0) return '-'
      const ms = n < 1e12 ? n * 1000 : n
      const d = new Date(ms)
      const pad = (x) => String(x).padStart(2, '0')
      return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
    },
    goForm() {
      if (this.formPath) uni.navigateTo({ url: this.formPath })
    },
    goDetail(item) {
      uni.navigateTo({ url: `${this.detailPath}?id=${item.id}` })
    },
  },
}
</script>

<style scoped>
.page-wrap { padding: 24rpx; }
.toolbar { margin-bottom: 24rpx; }
.btn { background: #6acdbb; color: #fff; font-size: 28rpx; }
.card { background: #fff; padding: 28rpx; border-radius: 16rpx; margin-bottom: 16rpx; }
.row { display: flex; justify-content: space-between; align-items: center; }
.name { font-size: 30rpx; font-weight: 600; }
.status { font-size: 24rpx; }
.s0 { color: #FF7D00; }
.s1 { color: #00B42A; }
.s2 { color: #F53F3F; }
.sub { margin-top: 12rpx; color: #86909C; font-size: 24rpx; }
.empty { text-align: center; color: #86909C; padding: 80rpx 0; }
</style>
