<!-- 成品入库 - 智能仓储风格 -->
<template>
  <div class="stock-requisition-page">
    <!-- 背景层 -->
    <div class="bg-layer" :style="{ backgroundImage: `url(${bgImg})` }" />

    <!-- 顶部标题区域 -->
    <div class="header-section">
      <button class="logout-btn" @click="goHome">
        <i class="fas fa-home" />
        <span>主页</span>
      </button>
      <h1 class="page-title">成品入库</h1>
      <!-- 右侧操作按钮 -->
      <div class="header-actions">
        <button class="action-btn add-btn" @click="openModal">
          <i class="fas fa-plus-circle" />
          <span>添加成品</span>
        </button>
      </div>
    </div>

    <!-- 主内容区域 -->
    <main class="main-content">
      <!-- 统计信息 -->
      <div v-if="products.length > 0" class="stats-section">
        <span class="stats-text">当前已添加 {{ products.length }} 条成品记录</span>
      </div>

      <!-- 卡片网格区域 -->
      <div class="card-grid-wrapper">
        <!-- 空状态 -->
        <div v-if="products.length === 0" class="empty-state">
          <i class="fas fa-box-open empty-icon" />
          <p>请添加成品</p>
        </div>

        <!-- 卡片网格 -->
        <div v-else class="card-grid">
          <div v-for="(product, index) in products" :key="product.code || index" class="inventory-card">
            <!-- 卡片序号 -->
            <div class="card-index">{{ index + 1 }}</div>

            <!-- 图片区域 -->
            <div class="card-image">
              <div class="image-placeholder">
                <i class="fas fa-box" />
              </div>
            </div>

            <!-- 信息区域 -->
            <div class="card-info">
              <div class="info-row">
                <span class="info-label">名称:</span>
                <span class="info-value">{{ product.inventoryName || '-' }}</span>
              </div>
              <div class="info-row">
                <span class="info-label">图号:</span>
                <span class="info-value">{{ product.inventoryNo || '-' }}</span>
              </div>
              <div class="info-row location-row">
                <i class="fas fa-map-marker-alt location-icon" />
                <span class="info-value">{{ product.positionName || '-' }}</span>
              </div>
            </div>

            <!-- 操作区域 -->
            <div class="card-action-section">
              <button class="delete-btn" @click.stop="openDeleteDialog(index)">
                <i class="fas fa-trash" /> 删除
              </button>
            </div>
          </div>
        </div>
      </div>
    </main>

    <!-- 底部操作按钮 -->
    <div class="bottom-actions">
      <button class="submit-btn" :disabled="products.length === 0" @click="handleSubmit">
        提交入库
      </button>
    </div>

    <!-- 新增成品弹窗 -->
    <div v-if="isModalOpen" class="tech-modal-overlay" @click.self="closeModal">
      <div class="tech-modal">
        <!-- <button class="modal-close-btn" @click="closeModal">
          <i class="fas fa-times" />
        </button> -->
        <div class="modal-header">
          <h3>新增成品</h3>
          <p>请填写成品信息</p>
        </div>
        <div class="modal-body">
          <div class="form-item">
            <label>成品名称</label>
            <input v-model="newProduct.inventoryName" type="text" placeholder="请输入成品名称" />
          </div>
          <div class="form-item">
            <label>成品图号</label>
            <input v-model="newProduct.inventoryNo" type="text" placeholder="请输入成品图号" />
          </div>
          <div class="form-item">
            <label>货位选择</label>
            <v-select
              v-model="newProduct.positionId" :options="warehouseLocationOptions"
              :reduce="item => item.positionId" label="positionName" placeholder="请选择货位" :clearable="true"
              :filterable="true" class="dark-select" append-to-body
            >
              <template #no-options>
                <span>无该选项数据</span>
              </template>
            </v-select>
          </div>
        </div>
        <div class="modal-footer">
          <button class="modal-btn cancel" @click="closeModal">取消</button>
          <button class="modal-btn confirm" :disabled="!isFormValid" @click="saveProduct">保存</button>
        </div>
      </div>
    </div>

    <!-- 确认入库弹窗 -->
    <div v-if="showConfirmModal" class="tech-modal-overlay" @click.self="showConfirmModal = false">
      <div class="tech-modal success-modal">
        <!-- <button class="modal-close-btn" @click="showConfirmModal = false">
          <i class="fas fa-times" />
        </button> -->
        <div class="modal-content-row">
          <div class="modal-text">
            <h3>确认入库</h3>
            <p>您确定要将选中的 <span class="highlight">{{ products.length }}</span> 个成品进行入库操作吗？</p>
          </div>
          <div class="modal-icon">
            <div class="icon-box">
              <i class="fas fa-box" />
              <i class="fas fa-check-circle check-icon" />
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="modal-btn cancel" @click="showConfirmModal = false">取消</button>
          <button class="modal-btn confirm" @click="executeInbound">确认入库</button>
        </div>
      </div>
    </div>

    <!-- 删除成品确认弹窗 - 科技感风格 -->
    <div v-if="showDeleteModal" class="tech-modal-overlay" @click.self="showDeleteModal = false">
      <div class="tech-modal warning-modal">
        <!-- <button class="modal-close-btn" @click="showDeleteModal = false">
          <i class="fas fa-times" />
        </button> -->
        <div class="modal-content-row">
          <div class="modal-text">
            <h3>确认删除</h3>
            <p>你确认要删除掉该成品吗？</p>
          </div>
          <div class="modal-icon warning">
            <div class="icon-box">
              <i class="fas fa-trash" />
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="modal-btn cancel" @click="showDeleteModal = false">取消</button>
          <button class="modal-btn confirm danger" @click="confirmDelete">确认删除</button>
        </div>
      </div>
    </div>

    <!-- Loading -->
    <div v-if="loading" class="loading-overlay">
      <div class="loading-dots">
        <div class="dot" />
        <div class="dot" />
        <div class="dot" />
      </div>
      <span class="loading-text">加载中...</span>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { showNotify } from 'vant';
import { generateStockIn, getLocatorList } from '../api/finishProduct';
import vSelect from 'vue-select';
import 'vue-select/dist/vue-select.css';
import { gateController } from '../hardware/GateOperate.js';

// 图片资源
import bgImg from '../assets/images/bj.png';

const router = useRouter();

// 返回主页
const goHome = () => {
    router.push('/home');
};

// 加载状态
const loading = ref(false);

// 仓库货位数据
const warehouseLocationOptions = ref([]);

// 弹窗显示状态
const isModalOpen = ref(false);
const showConfirmModal = ref(false);
const showDeleteModal = ref(false);
const deleteIndex = ref(-1);

// 成品列表
const products = ref([]);

// 新增成品表单
const newProduct = reactive({
    inventoryName: '',
    inventoryNo: '',
    positionId: undefined,
});

// 表单校验
const isFormValid = computed(() => {
    return (
        newProduct.inventoryName.trim() !== '' &&
    newProduct.inventoryNo.trim() !== '' &&
    !!newProduct.positionId
    );
});

// 打开弹窗
const openModal = () => {
    resetForm();
    isModalOpen.value = true;
};

// 关闭弹窗
const closeModal = () => {
    isModalOpen.value = false;
};

// 重置表单
const resetForm = () => {
    newProduct.inventoryName = '';
    newProduct.inventoryNo = '';
    newProduct.positionId = undefined;
};

// 保存成品
const saveProduct = () => {
    if (!isFormValid.value) {
        showNotify({ type: 'danger', message: '请完整填写成品信息' });
        return;
    }

    const location = warehouseLocationOptions.value.find(
        item => item.positionId === newProduct.positionId,
    );

    console.log(location);

    products.value.push({
        inventoryName: newProduct.inventoryName.trim(),
        inventoryNo: newProduct.inventoryNo.trim(),
        positionId: newProduct.positionId,
        positionName: location ? location.positionName + ' / ' + location.warehouseName : '',
    });

    showNotify({ type: 'success', message: '成品已添加' });
    isModalOpen.value = false;
};

// 执行入库
const handleSubmit = () => {
    if (products.value.length === 0) {
        showNotify({ type: 'danger', message: '暂无可提交的成品，请添加成品' });
        return;
    }
    showConfirmModal.value = true;
};

// 提交入库
const executeInbound = async () => {
    const count = products.value.length;
    loading.value = true;

    const params = products.value.map(item => {
        return {
            inventoryName: item.inventoryName,
            inventoryNo: item.inventoryNo,
            positionId: item.positionId,
        };
    });
    try {
        const res = await generateStockIn(params);
        if (res.errorCode === 0) {
            // 调用开门操作
            gateController('SHOTOPEN');

            showNotify({ type: 'success', message: `已提交 ${count} 条成品入库记录` });
            router.push('/');
        } else {
            showNotify({ type: 'danger', message: res.errorMessage || '添加失败' });
        }
        products.value = [];
        showConfirmModal.value = false;
    } catch (error) {
        console.log(error);
        showNotify({ type: 'danger', message: '添加成品失败' });
    } finally {
        loading.value = false;
    }
};

// 打开删除确认弹窗
const openDeleteDialog = index => {
    deleteIndex.value = index;
    showDeleteModal.value = true;
};

// 确认删除成品
const confirmDelete = () => {
    if (deleteIndex.value >= 0 && deleteIndex.value < products.value.length) {
        products.value.splice(deleteIndex.value, 1);
        showNotify({ type: 'success', message: '成品已删除' });
    }
    showDeleteModal.value = false;
    deleteIndex.value = -1;
};

const getLocators = async () => {
    try {
        const res = await getLocatorList();
        if (res && res.length > 0) {
            warehouseLocationOptions.value = res;
        } else {
            showNotify({ type: 'danger', message: '获取货位列表失败' });
        }
    } catch (error) {
        console.log(error);
        showNotify({ type: 'danger', message: '获取货位列表失败' });
    }
};
onMounted(() => {
    getLocators();
});
</script>

<style scoped>
/* ========== 基础样式 ========== */
.stock-requisition-page {
  width: 100%;
  height: 100vh;
  max-height: 100vh;
  position: relative;
  font-family: 'Microsoft YaHei', sans-serif;
  color: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 背景层 */
.bg-layer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #041c3d;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  z-index: 0;
}

/* 成品入库-操作按钮 - 绿色主题 */
.action-btn {
  background: linear-gradient(90deg, #1a6a5a 0%, #0d5a4a 100%);
  border-color: #2affcf;
}

.action-btn:hover {
  background: linear-gradient(90deg, #2a7a6a 0%, #1d6a5a 100%);
  box-shadow: 0 0 20px rgba(42, 255, 207, 0.5);
}

.action-btn i {
  color: #2affcf;
}

/* ========== 主内容区域 ========== */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 30px;
  position: relative;
  z-index: 10;
  min-height: 0;
  overflow: hidden;
}

/* 统计信息 */
.stats-section {
  background: rgba(9, 61, 140, 0.5);
  border: 1px solid #049FD8;
  border-radius: 12px;
  padding: 15px 20px;
  margin-bottom: 15px;
  flex-shrink: 0;
}

.stats-text {
  color: #7ec8ff;
  font-size: 16px;
}

/* ========== 卡片网格区域 ========== */
.card-grid-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: 10px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.card-grid-wrapper::-webkit-scrollbar {
  display: none;
}

/* 空状态 */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #7ec8ff;
  padding: 60px 0;
}

.empty-icon {
  font-size: 80px;
  margin-bottom: 20px;
  opacity: 0.5;
}

.empty-state p {
  font-size: 18px;
  margin: 0;
}

/* 卡片网格 - 4列布局 */
.card-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

/* ========== 设备卡片样式 ========== */
.inventory-card {
  background: rgba(5, 30, 60, 0.8);
  border: 2px solid #0d4a8a;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  position: relative;
}

.inventory-card:hover {
  border-color: #1e90ff;
  box-shadow: 0 0 20px rgba(30, 144, 255, 0.3);
  transform: translateY(-3px);
}

/* 卡片序号 - 左上角 */
.card-index {
  position: absolute;
  top: 10px;
  left: 10px;
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #1e90ff 0%, #00bfff 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  z-index: 2;
  box-shadow: 0 2px 8px rgba(0, 191, 255, 0.4);
}

/* 图片区域 */
.card-image {
  width: 100%;
  aspect-ratio: 3 / 4;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin: 15px;
  margin-bottom: 10px;
  border-radius: 8px;
  width: calc(100% - 30px);
}

.image-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #f0f4f8 0%, #e8ecf0 100%);
  color: #fff;
  font-size: 48px;
}

/* 信息区域 */
.card-info {
  padding: 10px 15px 15px;
}

.info-row {
  display: flex;
  align-items: baseline;
  margin-bottom: 6px;
  font-size: 14px;
}

.info-row:last-child {
  margin-bottom: 0;
}

.info-label {
  color: #7ec8ff;
  white-space: nowrap;
  margin-right: 5px;
}

.info-value {
  color: #fff;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 位置行样式 */
.location-row {
  display: flex;
  align-items: center;
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px solid rgba(30, 144, 255, 0.2);
}

.location-icon {
  color: #00bfff;
  font-size: 12px;
  margin-right: 6px;
}

/* ========== 操作区域 ========== */
.card-action-section {
  padding: 12px 15px;
  border-top: 1px solid rgba(30, 144, 255, 0.3);
  background: rgba(9, 61, 140, 0.3);
  min-height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 删除按钮 */
.delete-btn {
  padding: 8px 20px;
  background: rgba(239, 68, 68, 0.2);
  border: 1px solid #ef4444;
  border-radius: 6px;
  color: #fca5a5;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.delete-btn:hover {
  background: rgba(239, 68, 68, 0.4);
  box-shadow: 0 0 10px rgba(239, 68, 68, 0.3);
}

/* ========== 底部操作按钮 ========== */
.bottom-actions {
  width: 100%;
  padding: 15px 30px;
  box-sizing: border-box;
  background: rgba(4, 28, 61, 0.95);
  z-index: 20;
  flex-shrink: 0;
}

.submit-btn {
  width: 100%;
  padding: 18px 0;
  background: #4a99e2;
  border: none;
  border-radius: 12px;
  font-size: 24px;
  font-weight: bold;
  color: #fff;
  cursor: pointer;
  transition: all 0.3s;
  letter-spacing: 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.submit-btn:hover:not(:disabled) {
  box-shadow: 0 0 30px rgba(74, 153, 226, 0.6);
  transform: translateY(-2px);
}

.submit-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========== 科技感弹窗样式 ========== */
.tech-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 28, 61, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.tech-modal {
  background: linear-gradient(135deg, #0a3d6d 0%, #041c3d 100%);
  border: 1px solid #049FD8;
  border-radius: 16px;
  width: 90%;
  max-width: 500px;
  position: relative;
  box-shadow: 0 0 40px rgba(4, 159, 216, 0.3);
}

.modal-close-btn {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: 50%;
  color: #7ec8ff;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.modal-header {
  padding: 30px 30px 20px;
}

.modal-header h3 {
  font-size: 22px;
  font-weight: bold;
  color: #fff;
  margin: 0 0 8px 0;
}

.modal-header p {
  font-size: 14px;
  color: #7ec8ff;
  margin: 0;
}

.modal-body {
  padding: 0 30px 20px;
}

.form-item {
  margin-bottom: 20px;
}

.form-item:last-child {
  margin-bottom: 0;
}

.form-item label {
  display: block;
  font-size: 14px;
  color: #7ec8ff;
  margin-bottom: 8px;
  font-weight: 500;
}

.form-item input {
  width: 100%;
  padding: 12px 15px;
  background: rgba(13, 58, 106, 0.8);
  border: 1px solid #2a7fff;
  border-radius: 8px;
  color: #fff;
  font-size: 15px;
  outline: none;
  transition: all 0.3s;
  box-sizing: border-box;
}

.form-item input::placeholder {
  color: #5a8abf;
}

.form-item input:focus {
  border-color: #00bfff;
  box-shadow: 0 0 10px rgba(0, 191, 255, 0.3);
}

.modal-footer {
  padding: 20px 30px 30px;
  display: flex;
  gap: 15px;
}

.modal-btn {
  flex: 1;
  padding: 14px 0;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
}

.modal-btn.cancel {
  background: transparent;
  border: 1px solid #2a7fff;
  color: #7ec8ff;
}

.modal-btn.cancel:hover {
  background: rgba(42, 127, 255, 0.2);
}

.modal-btn.confirm {
  background: linear-gradient(90deg, #1e90ff 0%, #00bfff 100%);
  border: none;
  color: #fff;
}

.modal-btn.confirm:hover:not(:disabled) {
  box-shadow: 0 0 20px rgba(30, 144, 255, 0.5);
}

.modal-btn.confirm:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.modal-btn.confirm.danger {
  background: linear-gradient(90deg, #ef4444 0%, #f87171 100%);
}

.modal-btn.confirm.danger:hover {
  box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
}

/* 成功/警告弹窗样式 */
.modal-content-row {
  padding: 30px;
  display: flex;
  align-items: center;
  gap: 30px;
}

.modal-text {
  flex: 1;
}

.modal-text h3 {
  font-size: 22px;
  font-weight: bold;
  color: #fff;
  margin: 0 0 12px 0;
}

.modal-text p {
  font-size: 15px;
  color: #7ec8ff;
  margin: 0;
  line-height: 1.6;
}

.modal-text .highlight {
  color: #00bfff;
  font-weight: bold;
}

.modal-icon {
  flex-shrink: 0;
}

.icon-box {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #1e90ff 0%, #00bfff 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  color: #fff;
  position: relative;
  transform: perspective(100px) rotateY(-10deg);
  box-shadow: 0 10px 30px rgba(0, 191, 255, 0.3);
}

.icon-box .check-icon {
  position: absolute;
  bottom: -5px;
  right: -5px;
  font-size: 24px;
  color: #10b981;
  background: #041c3d;
  border-radius: 50%;
  padding: 2px;
}

.modal-icon.warning .icon-box {
  background: linear-gradient(135deg, #ef4444 0%, #f87171 100%);
  box-shadow: 0 10px 30px rgba(239, 68, 68, 0.3);
}

/* Vue Select 深色主题 */
:deep(.dark-select .vs__dropdown-toggle) {
  background: rgba(13, 58, 106, 0.8);
  border: 1px solid #2a7fff;
  border-radius: 8px;
  padding: 8px 12px;
  min-height: 44px;
}

:deep(.dark-select .vs__selected) {
  color: #fff;
}

:deep(.dark-select .vs__search) {
  color: #fff;
}

:deep(.dark-select .vs__search::placeholder) {
  color: #5a8abf;
}

:deep(.dark-select .vs__actions svg) {
  fill: #7ec8ff;
}

:deep(.dark-select .vs__dropdown-menu) {
  background: rgba(13, 58, 106, 0.95);
  border: 1px solid #2a7fff;
  border-radius: 8px;
  z-index: 99999;
}

:deep(.dark-select .vs__dropdown-option) {
  color: #fff;
  padding: 10px 15px;
}

:deep(.dark-select .vs__dropdown-option--highlight) {
  background: rgba(30, 144, 255, 0.3);
}

/* ========== 响应式 - 横屏 ========== */
@media screen and (orientation: landscape) {
  .header-section {
    padding: 10px 20px;
  }

  .page-title {
    font-size: 22px;
  }

  .logout-btn {
    padding: 6px 14px;
    font-size: 13px;
    left: 20px;
  }

  .header-actions {
    right: 20px;
  }

  .action-btn {
    padding: 6px 14px;
    font-size: 12px;
  }

  .main-content {
    padding: 0 20px;
  }

  .card-grid {
    grid-template-columns: repeat(6, 1fr);
    gap: 15px;
  }

  .card-index {
    width: 24px;
    height: 24px;
    font-size: 11px;
    top: 6px;
    left: 6px;
  }

  .card-image {
    margin: 10px;
    margin-bottom: 8px;
    width: calc(100% - 20px);
  }

  .image-placeholder {
    font-size: 36px;
  }

  .card-info {
    padding: 6px 10px 8px;
  }

  .info-row {
    font-size: 11px;
    margin-bottom: 3px;
  }

  .card-action-section {
    padding: 8px 10px;
    min-height: 40px;
  }

  .delete-btn {
    padding: 5px 12px;
    font-size: 11px;
  }

  .bottom-actions {
    padding: 10px 20px;
  }

  .submit-btn {
    padding: 12px 0;
    font-size: 18px;
    letter-spacing: 6px;
    border-radius: 8px;
  }

  :deep(.v-select .vs__dropdown-toggle,
    .dark-select .vs__dropdown-toggle,
    .filter-select .vs__dropdown-toggle,
    .dialog-select .vs__dropdown-toggle) {
    padding: 8px 12px !important;
    min-height: 48px !important;
  }
}
</style>

<style>
/* 全局样式：确保 v-select 下拉菜单在模态框之上 */
.vs__dropdown-menu {
  z-index: 99999 !important;
}

/* vue-select 的下拉菜单容器 */
.vs--open .vs__dropdown-menu {
  z-index: 99999 !important;
}

/* 大选择器的下拉菜单 */
.large-select .vs__dropdown-menu {
  z-index: 99999 !important;
}
</style>
