<template>
  <div class="mobile-bom-list">
    <!-- 顶部操作栏 -->
    <div class="mobile-header">
      <div class="mobile-header__search">
        <el-input
          v-model="queryParams.bomName"
          placeholder="搜索BOM名称"
          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="['mes:product-bom:create']" />
      </div>
    </div>

    <div class="mobile-header__quick-filter">
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === undefined }"
        @click="handleQuickFilter(undefined)"
      >
        全部
      </div>
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 1 }"
        @click="handleQuickFilter(1)"
      >
        启用
      </div>
      <div
        class="quick-filter-item"
        :class="{ active: queryParams.status === 0 }"
        @click="handleQuickFilter(0)"
      >
        禁用
      </div>
    </div>

    <!-- 卡片列表 -->
    <div class="mobile-list" v-loading="loading">
      <div v-if="list.length === 0 && !loading" class="mobile-empty">
        <el-empty description="暂无BOM记录" />
      </div>
      <div
        v-for="item in list"
        :key="item.id"
        class="mobile-card"
        @click="openForm('view', item.id)"
      >
        <div class="mobile-card__header">
          <span class="mobile-card__no">{{ item.bomName }}</span>
          <el-tag :type="item.status === 1 ? 'success' : 'danger'" size="small">
            {{ item.status === 1 ? '启用' : '禁用' }}
          </el-tag>
        </div>
        <div class="mobile-card__body">
          <div class="mobile-card__row">
            <span class="mobile-card__label">BOM编码</span>
            <span class="mobile-card__value">{{ item.bomCode || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">产品名称</span>
            <span class="mobile-card__value">{{ item.productName || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">产品编码</span>
            <span class="mobile-card__value">{{ item.productCode || '-' }}</span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">版本</span>
            <span class="mobile-card__value">
              {{ item.version || '-' }}
              <el-tag v-if="item.isDefault === 1" type="success" size="small" style="margin-left: 4px">默认</el-tag>
            </span>
          </div>
          <div class="mobile-card__row">
            <span class="mobile-card__label">创建时间</span>
            <span class="mobile-card__value">{{ item.createTime || '-' }}</span>
          </div>
        </div>
        <div class="mobile-card__footer">
          <el-button size="small" @click.stop="openForm('view', item.id)" v-hasPermi="['mes:product-bom:query']">查看</el-button>
          <el-button size="small" type="primary" @click.stop="openForm('update', item.id)" v-hasPermi="['mes:product-bom:update']">编辑</el-button>
          <el-button size="small" type="danger" @click.stop="handleDelete(item.id)" v-hasPermi="['mes:product-bom: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="productName">
          <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable style="width:100%" />
        </el-form-item>
        <el-form-item label="BOM编码" prop="bomCode">
          <el-input v-model="queryParams.bomCode" placeholder="请输入BOM编码" clearable style="width:100%" />
        </el-form-item>
        <el-form-item label="BOM名称" prop="bomName">
          <el-input v-model="queryParams.bomName" placeholder="请输入BOM名称" clearable style="width:100%" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width:100%">
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleFilterConfirm">确认筛选</el-button>
      </template>
    </el-drawer>

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

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Filter, Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Icon } from '@/components/Icon'
import ProductBomForm from './ProductBomForm.vue'
import { getProductBomPage, deleteProductBom } from '@/api/mes/product/bom'

defineOptions({ name: 'MesProductBom' })

const filterVisible = ref(false)

const loading = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const queryFormRef = ref()
const formRef = ref()

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productName: undefined,
  bomCode: undefined,
  bomName: undefined,
  status: undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data = await getProductBomPage(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 openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

const handleDelete = async (id: number) => {
  try {
    await ElMessageBox.confirm('确认删除该BOM记录吗？删除后明细数据也将被删除！', '提示', {
      type: 'warning'
    })
    await deleteProductBom(id)
    ElMessage.success('删除成功')
    getList()
  } catch {}
}

onMounted(() => {
  getList()
})

/** 快捷分类筛选 */
const handleQuickFilter = (status: number | undefined) => {
  queryParams.status = status
  queryParams.pageNo = 1
  getList()
}

/** 筛选确认 */
const handleFilterConfirm = () => {
  filterVisible.value = false
  handleQuery()
}
</script>

<style lang="scss" scoped>
.mobile-bom-list {
  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-header__quick-filter {
  display: flex;
  gap: 12px;
  margin: 8px 0;
  justify-content: flex-start;

  .quick-filter-item {
    padding: 4px 12px;
    font-size: 14px;
    border-radius: 20px;
    cursor: pointer;
    color: #909399;
    background: transparent;
    transition: all 0.2s;

    &.active {
      color: #fff;
      background: #409eff;
    }
  }
}
.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;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
  &__footer { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; padding-top: 10px; border-top: 1px solid #f0f0f0; }
}
.mobile-pagination {
  margin-top: 12px;
  display: flex;
  justify-content: center;
  :deep(.el-pagination) { flex-wrap: wrap; justify-content: center; }
}
</style>
