<template>
  <clinic-salesperson-page-layout title="传方记录" :show-back="true">
    <view class="page">
      <view v-if="!list.length && !loading" class="empty">暂无传方记录</view>
      <view v-for="item in list" :key="item.id" class="row" @click="openDetail(item.id)">
        <view class="top">
          <text class="name">{{ item.patient_name }} {{ item.patient_mobile }}</text>
          <u-tag
            :text="item.status_text || (item.is_imported ? '已完成' : '未处理')"
            :type="statusTagType(item.status)"
            size="mini"
          />
        </view>
        <text class="meta">{{ item.transfer_mode_text || '' }} · {{ item.transfer_time_text }}</text>
        <text v-if="item.drug_text_lines && item.drug_text_lines.length" class="drugs">
          药方：{{ item.drug_text_lines.join('；') }}
        </text>
        <text v-if="item.remark" class="remark">备注：{{ item.remark }}</text>
        <text v-if="item.status === 3 && item.exception_message" class="exception">
          异常：{{ item.exception_message }}
        </text>
      </view>
      <u-button v-if="list.length" plain type="primary" class="mt" @click="goForm">新建传方</u-button>
    </view>
    <transfer-detail-drawer ref="detailDrawer" />
  </clinic-salesperson-page-layout>
</template>

<script>
import ClinicSalespersonPageLayout from '@/subPackages/sub_clinic_salesperson/components/clinic-salesperson-page-layout.vue'
import TransferDetailDrawer from '@/subPackages/sub_clinic_salesperson/components/TransferDetailDrawer.vue'
import { getClinicSalespersonTransferPrescriptionList } from '@/api/clinicSalesperson.js'

export default {
  components: { ClinicSalespersonPageLayout, TransferDetailDrawer },
  data() {
    return { list: [], loading: false }
  },
  onShow() {
    this.loadList()
  },
  methods: {
    async loadList() {
      this.loading = true
      try {
        const wrap = await getClinicSalespersonTransferPrescriptionList({ page: 1, pageSize: 50 })
        if (wrap && wrap.ok) {
          this.list = wrap.data?.items || []
        }
      } finally {
        this.loading = false
      }
    },
    statusTagType(status) {
      const map = { 0: 'warning', 1: 'primary', 2: 'success', 3: 'error' }
      return map[Number(status)] || 'info'
    },
    goForm() {
      uni.navigateBack({ fail: () => {
        uni.navigateTo({ url: '/subPackages/sub_clinic_salesperson/home/index' })
      }})
    },
    openDetail(id) {
      if (this.$refs.detailDrawer) {
        this.$refs.detailDrawer.open(id)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.page { padding: 24rpx; }
.empty { text-align: center; color: #999; padding: 80rpx 0; }
.row { background: #fff; border-radius: 12rpx; padding: 24rpx; margin-bottom: 16rpx; }
.top { display: flex; justify-content: space-between; align-items: center; }
.name { font-size: 28rpx; font-weight: 600; }
.meta, .drugs, .remark { display: block; font-size: 24rpx; color: #666; margin-top: 8rpx; }
.exception { display: block; font-size: 24rpx; color: #ef4444; margin-top: 8rpx; }
.mt { margin-top: 24rpx; }
</style>
