<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 }} · {{ formatTime(item.created_at) }}</view>
        <view v-if="inputType === 'store' && item.promoter_admin" class="promoter-row" @click.stop>
          <promoter-info-card :admin="item.promoter_admin" />
        </view>
      </view>
      <view v-if="!list.length" class="empty">暂无记录</view>
    </view>
  </business-page-layout>
</template>

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

export default {
  mixins: [businessMixin],
  components: { BusinessPageLayout, PromoterInfoCard },
  props: {
    auditMode: { type: Boolean, default: false },
    inputType: { type: String, default: 'store' },
    formPath: { type: String, required: true },
    detailPath: { type: String, required: true },
  },
  data() { return { list: [], page: 1 } },
  computed: {
    title() {
      if (this.inputType === 'doctor') return this.auditMode ? '医生信息审核' : '医生信息预填'
      return this.auditMode ? '诊所信息审核' : '诊所信息录入'
    },
  },
  onShow() { this.reload() },
  onReachBottom() { this.load(this.page + 1, true) },
  methods: {
    listApi(params) {
      return this.inputType === 'doctor'
        ? this.bizApi.getDoctorInputList(params)
        : this.bizApi.getStoreInputList(params)
    },
    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 = (w.data && w.data.items) || []
        this.list = append ? this.list.concat(items) : items
        this.page = page
      })
    },
    statusText(s) {
      return { 0: '待审核', 1: '已通过', 2: '已拒绝' }[Number(s)] || '-'
    },
    formatTime(t) {
      if (!t) return '-'
      const d = new Date(Number(t) * 1000)
      return `${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}`
    },
    goForm() { 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; }
.promoter-row { margin-top: 16rpx; padding-top: 12rpx; border-top: 1rpx solid #f5f5f5; }
.empty { text-align: center; color: #86909C; padding: 80rpx 0; }
</style>
