<template>
  <div class="mobile-pending">
    <!-- 顶部操作栏 -->
    <div class="mobile-header">
      <div class="mobile-header__search">
        <el-select
          v-model="queryParams.bizTableName"
          placeholder="业务类型"
          clearable
          style="width: 100%"
          @change="handleQuery"
        >
          <el-option
            v-for="item in bizTableOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="mobile-header__actions">
        <el-button :icon="Refresh" circle @click="resetQuery" />
      </div>
    </div>

    <!-- 卡片列表 -->
    <div class="mobile-list" v-loading="loading">
      <div v-if="filteredList.length === 0 && !loading" class="mobile-empty">
        <el-empty description="暂无待审批记录" />
      </div>
      <div
        v-for="item in filteredList"
        :key="item.id"
        class="mobile-card"
        @click="handleProcess(item)"
      >
        <div class="mobile-card__header">
          <span class="mobile-card__no">{{ item.bizId }}</span>
          <el-tag type="warning" size="small">待审批</el-tag>
        </div>
        <div class="mobile-card__body">
          <div class="mobile-card__row">
            <span class="mobile-card__label">业务类型</span>
            <span class="mobile-card__value">{{ getBizTableLabel(item.bizTableName) }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">申请人</span>
            <span class="mobile-card__value">{{ item.applicantName || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">审批层级</span>
            <span class="mobile-card__value">第{{ item.approvalLevel }}级</span>
          </div>
          <div class="mobile-card__row" v-if="item.assignerName">
            <span class="mobile-card__label">指定者</span>
            <span class="mobile-card__value">{{ item.assignerName }}</span>
          </div>
          <div class="mobile-card__row" v-if="item.assignReason">
            <span class="mobile-card__label">指定原因</span>
            <span class="mobile-card__value mobile-card__value--ellipsis">{{ item.assignReason }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">提交时间</span>
            <span class="mobile-card__value">{{ formatDate2(item.createTime) }}</span>
          </div>
        </div>
        <div class="mobile-card__footer">
          <el-button size="small" type="primary" @click.stop="handleProcess(item)">处理审批</el-button>
          <el-button size="small" type="info" @click.stop="handleViewRecords(item)">审批记录</el-button>
        </div>
      </div>
    </div>

    <!-- 处理审批对话框 -->
    <ProcessApprovalDialog ref="processApprovalDialogRef" @success="getList" />

    <!-- 审批记录对话框 -->
    <ApprovalRecordsDialog ref="approvalDialogRef" />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { formatDate } from '@/utils/formatTime'
import { ApprovalRecordApi, ApprovalRecordVO } from '@/api/erp/approval'
import { ProcessApprovalDialog, ApprovalRecordsDialog } from '@/components/Approval'
import { useUserStore } from '@/store/modules/user'

defineOptions({ name: 'ErpApprovalPending' })

const userStore = useUserStore()
const loading = ref(true)
const list = ref<ApprovalRecordVO[]>([])
const queryParams = reactive({
  bizTableName: undefined as string | undefined
})
const queryFormRef = ref()

const formatDate2 = (date: any) => {
  if (!date) return '-'
  return formatDate(new Date(date), 'YYYY-MM-DD HH:mm')
}

// 业务类型选项
const bizTableOptions = [
  { label: '库存盘点', value: 'erp_stock_check' },
  { label: '入库单', value: 'erp_stock_in' },
  { label: '出库单', value: 'erp_stock_out' },
  { label: '调拨单', value: 'erp_stock_move' },
  { label: '组装拆卸', value: 'erp_assembly_disassembly' },
  { label: '销售订单', value: 'erp_sale_order' },
  { label: '销售出库', value: 'erp_sale_out' },
  { label: '销售退货', value: 'erp_sale_return' },
  { label: '采购订单', value: 'erp_purchase_order' },
  { label: '采购退货', value: 'erp_purchase_return' },
  { label: '收款单', value: 'erp_finance_receipt' },
  { label: '付款单', value: 'erp_finance_payment' },
  { label: '生产计划', value: 'mes_production_plan' },
  { label: '采购计划', value: 'erp_purchase_requisition' }
]

// 获取业务类型标签
const getBizTableLabel = (value: string) => {
  const option = bizTableOptions.find(item => item.value === value)
  return option ? option.label : value
}

// 过滤后的列表
const filteredList = computed(() => {
  if (!queryParams.bizTableName) {
    return list.value
  }
  return list.value.filter(item => item.bizTableName === queryParams.bizTableName)
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const userId = String(userStore.getUser.id)
    list.value = await ApprovalRecordApi.getPendingApprovalList(userId)
  } catch (error) {
    console.error('获取待审批列表失败', error)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.bizTableName = undefined
  handleQuery()
}

/** 处理审批 */
const processApprovalDialogRef = ref()
const handleProcess = (row: ApprovalRecordVO) => {
  processApprovalDialogRef.value.open(row.bizId, row.bizTableName)
}

/** 查看审批记录 */
const approvalDialogRef = ref()
const handleViewRecords = (row: ApprovalRecordVO) => {
  approvalDialogRef.value.open(row.bizId, row.bizTableName)
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>

<style lang="scss" scoped>
.mobile-pending {
  padding: 12px;
  background: #f5f5f5;
  min-height: 100vh;
}

.mobile-header {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;

  &__search {
    flex: 1;
  }

  &__actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
  }
}

.mobile-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mobile-empty {
  padding: 40px 0;
}

.mobile-card {
  background: #fff;
  border-radius: 10px;
  padding: 14px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);

  &__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  &__no {
    font-weight: 600;
    font-size: 15px;
    color: #303133;
  }

  &__body {
    font-size: 13px;
  }

  &__row {
    display: flex;
    justify-content: space-between;
    padding: 3px 0;
  }

  &__label {
    color: #909399;
    flex-shrink: 0;
    margin-right: 12px;
  }

  &__value {
    color: #606266;
    text-align: right;

    &--ellipsis {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      max-width: 200px;
    }
  }

  &__footer {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #f0f0f0;
  }
}
</style>
