<template>
  <div class="mobile-supplier">
    <!-- 顶部操作栏 -->
    <div class="mobile-header">
      <div class="mobile-header__search">
        <el-input v-model="queryParams.name" placeholder="搜索户主姓名" clearable @keyup.enter="handleQuery" :prefix-icon="Search" />
      </div>
      <div class="mobile-header__actions">
        <el-button :icon="Filter" circle @click="filterVisible = true" />
        <el-button type="primary" :icon="Plus" circle @click="openForm('create')" v-hasPermi="['erp:supplier:create']" />
      </div>
    </div>

    <!-- 卡片列表 -->
    <div class="mobile-list" v-loading="loading">
      <div v-if="list.length === 0 && !loading" class="mobile-empty">
        <el-empty description="暂无供应商" />
      </div>
      <div v-for="item in list" :key="item.id" class="mobile-card" @click="openForm('update', item.id)">
        <div class="mobile-card__header">
          <span class="mobile-card__no">{{ item.name || '-' }}</span>
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="item.status" />
        </div>
        <div class="mobile-card__body">
          <div class="mobile-card__row">
            <span class="mobile-card__label">住址</span>
            <span class="mobile-card__value">{{ item.address || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">种植基地</span>
            <span class="mobile-card__value">{{ item.plantingBase || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">合同号</span>
            <span class="mobile-card__value">{{ item.contractNumber || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">使用品种</span>
            <span class="mobile-card__value">{{ item.usedVariety || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">管理员</span>
            <span class="mobile-card__value">{{ item.adminName || '-' }}</span>
          </div>
          <div class="mobile-card__nums">
            <div class="mobile-card__num-item">
              <div class="mobile-card__num-val">{{ item.contractedArea ?? '-' }}</div>
              <div class="mobile-card__num-label">签订面积</div>
            </div>
            <div class="mobile-card__num-item">
              <div class="mobile-card__num-val">{{ item.actualArea ?? '-' }}</div>
              <div class="mobile-card__num-label">实种面积</div>
            </div>
            <div class="mobile-card__num-item">
              <div class="mobile-card__num-val" style="color:#409eff">{{ item.orderQuantity ?? '-' }}</div>
              <div class="mobile-card__num-label">订购数量</div>
            </div>
          </div>
        </div>
        <div class="mobile-card__footer">
          <el-button size="small" type="primary" @click.stop="openForm('update', item.id)" v-hasPermi="['erp:supplier:update']">编辑</el-button>
          <el-button size="small" type="danger" @click.stop="handleDelete(item.id)" v-hasPermi="['erp:supplier:delete']">删除</el-button>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="mobile-pagination" v-if="total > 0">
      <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" :page-sizes="[10, 20]" layout="total, prev, pager, next" :pager-count="5" @pagination="getList" />
    </div>

    <!-- 筛选抽屉 -->
    <el-drawer v-model="filterVisible" title="筛选条件" direction="btt" size="50%">
      <el-form :model="queryParams" ref="queryFormRef" label-position="top">
        <el-form-item label="住址" prop="address">
          <el-input v-model="queryParams.address" placeholder="请输入住址" clearable />
        </el-form-item>
        <el-form-item label="种植基地" prop="plantingBase">
          <el-input v-model="queryParams.plantingBase" placeholder="请输入种植基地" clearable />
        </el-form-item>
        <el-form-item label="管理员姓名" prop="adminName">
          <el-input v-model="queryParams.adminName" placeholder="请输入管理员姓名" clearable />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleFilterConfirm">确认筛选</el-button>
      </template>
    </el-drawer>

    <!-- 表单弹窗：添加/修改 -->
    <SupplierForm ref="formRef" @success="getList" />
  </div>
</template>

<script setup lang="ts">
import { Search, Filter, Plus } from '@element-plus/icons-vue'
import { DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'
import SupplierForm from './SupplierForm.vue'

/** ERP 供应商 列表 */
defineOptions({ name: 'ErpSupplier' })

const message = useMessage()
const { t } = useI18n()

const loading = ref(true)
const list = ref<SupplierVO[]>([])
const total = ref(0)
const filterVisible = ref(false)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  address: undefined,
  plantingBase: undefined,
  adminName: undefined,
  mobile: undefined,
  telephone: undefined,
  type: 2
})
const queryFormRef = ref()
const exportLoading = ref(false)

const getList = async () => {
  loading.value = true
  try {
    const data = await SupplierApi.getSupplierPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handleFilterConfirm = () => {
  filterVisible.value = false
  handleQuery()
}

const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await SupplierApi.deleteSupplier(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const handleExport = async () => {
  try {
    await message.exportConfirm()
    exportLoading.value = true
    const data = await SupplierApi.exportSupplier(queryParams)
    download.excel(data, 'ERP 供应商.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.mobile-supplier {
  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; }
  &__nums { display: flex; justify-content: space-around; margin-top: 10px; padding: 10px 0; border-top: 1px solid #f0f0f0; border-bottom: 1px solid #f0f0f0; }
  &__num-item { text-align: center; }
  &__num-val { font-size: 15px; font-weight: 600; color: #303133; }
  &__num-label { font-size: 11px; color: #909399; margin-top: 2px; }
  &__footer { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
}
.mobile-pagination {
  margin-top: 12px; display: flex; justify-content: center;
  :deep(.el-pagination) { flex-wrap: wrap; justify-content: center; }
}
</style>
