<template>
  <z-paging ref="paging" v-model="receiptRecords" @query="queryList" :layout-only="activeTab === 'form'">
    <template #top>
      <!-- 顶部标签：我要领用 / 领用记录 -->
      <view class="tabs">
        <view
          class="tab"
          :class="activeTab === 'form' ? 'active' : ''"
          @click="activeTab = 'form'"
        >
          我要领用
        </view>
        <view
          class="tab"
          :class="activeTab === 'list' ? 'active' : ''"
          @click="switchToList"
        >
          领用记录
        </view>
      </view>
    </template>

    <!-- 表单区域 -->
    <view class="tab-content" v-show="activeTab === 'form'">
      <view class="form-card">
        <view class="form-item">
          <text class="label">物资名称</text>
          <view class="picker">
            <select v-model="receiptForm.materialName">
              <option v-for="material in materials" :key="material.id" :value="material.name">{{ material.name }}</option>
            </select>
          </view>
        </view>

        <view class="form-item">
          <text class="label">领用数量</text>
          <input
            class="input"
            type="number"
            v-model="receiptForm.quantity"
            placeholder="请输入领用数量"
          />
        </view>

        <view class="form-item">
          <text class="label">领用用途</text>
          <textarea
            class="textarea"
            v-model="receiptForm.purpose"
            placeholder="请输入领用用途"
            maxlength="200"
            :auto-height="true"
          />
          <text class="count">{{ receiptForm.purpose.length }}/200</text>
        </view>

        <view class="form-item">
          <text class="label">领用人</text>
          <input
            class="input"
            type="text"
            v-model="receiptForm.applicant"
            placeholder="请输入领用人姓名"
          />
        </view>
      </view>

      <view class="submit-section">
        <view class="submit-btn" @click="submitReceipt">提交领用申请</view>
      </view>
    </view>

    <!-- 列表区域 -->
    <view class="tab-content" v-show="activeTab === 'list'">
      <view
        class="record-card"
        v-for="item in receiptRecords"
        :key="item.id"
      >
        <view class="card-header">
          <view class="left">
            <text class="type">{{ item.materialName }}</text>
            <text
              class="status"
              :class="statusClassMap[item.status] || statusClassMap['__default__']"
            >
              {{ item.status }}
            </text>
          </view>
          <view class="header-right">
            <text class="time">
              {{ item.applyTime || '' }}
            </text>
          </view>
        </view>

        <view class="card-body">
          <view class="row">
            <text class="label">领用数量</text>
            <text class="value">
              {{ item.quantity }}件
            </text>
          </view>
          <view class="row">
            <text class="label">领用用途</text>
            <text class="value">
              {{ item.purpose || '—' }}
            </text>
          </view>
          <view class="row">
            <text class="label">领用人</text>
            <text class="value">
              {{ item.applicant || '—' }}
            </text>
          </view>
        </view>

        <view class="card-footer" v-if="item.status === '已审批'">
          <view class="voucher-btn" @click="viewVoucher(item.id)">查看领用凭证</view>
        </view>
      </view>
    </view>
  </z-paging>
</template>

<script>
export default {
	data() {
		return {
			activeTab: 'form',
			receiptForm: {
				materialName: '',
				quantity: '',
				purpose: '',
				applicant: ''
			},
			statusClassMap: {
				'待审批': 'status-pending',
				'已审批': 'status-approved',
				'已拒绝': 'status-rejected',
				'__default__': 'status-pending'
			},
			materials: [
				{ id: 1, name: '维修耗材' },
				{ id: 2, name: '办公用品' },
				{ id: 3, name: '清洁用品' },
				{ id: 4, name: '食材' }
			],
			receiptRecords: [
				{ id: 1, materialName: '维修耗材', quantity: 10, purpose: '设备维修', applicant: '张三', status: '已审批', applyTime: '2026-03-07 10:00' },
				{ id: 2, materialName: '办公用品', quantity: 5, purpose: '日常办公', applicant: '李四', status: '待审批', applyTime: '2026-03-07 09:30' },
				{ id: 3, materialName: '清洁用品', quantity: 20, purpose: '公共区域清洁', applicant: '王五', status: '已拒绝', applyTime: '2026-03-06 16:00' }
			]
		};
	},
	onLoad() {
		if (this.activeTab === 'list') {
			this.refreshList();
		}
	},
	methods: {
		switchToList() {
			this.activeTab = 'list';
			if (!this.receiptRecords.length) {
				this.$refs.paging.reload();
			}
		},
		submitReceipt() {
			if (!this.receiptForm.materialName || !this.receiptForm.quantity || !this.receiptForm.purpose || !this.receiptForm.applicant) {
				uni.showToast({
					title: '请填写完整信息',
					icon: 'none'
				});
				return;
			}
			
			// 模拟提交领用申请
			uni.showToast({
				title: '领用申请提交成功',
				icon: 'success'
			});
			
			// 重置表单
			setTimeout(() => {
				this.receiptForm = {
					materialName: '',
					quantity: '',
					purpose: '',
					applicant: ''
				};
			},
			1500);
			
			// 如果当前在列表页，刷新列表
			if (this.activeTab === 'list') {
				this.$refs.paging.reload();
			}
		},
		viewVoucher(id) {
			// 模拟查看领用凭证
			uni.showModal({
				title: '领用凭证',
				content: '凭证编号：LP20260307001\n领用人：张三\n领用物资：维修耗材\n领用数量：10件\n审批状态：已审批',
				showCancel: false
			});
		},
		// 获取领用记录列表
		queryList(pageNo, pageSize) {
			// 模拟API请求
			// 实际项目中这里应该调用真实的API
			setTimeout(() => {
				const list = this.receiptRecords;
				// 将请求结果通过complete传给z-paging处理
				this.$refs.paging.complete(list);
			}, 500);
		},
		refreshList() {
			if (this.$refs.paging) {
				this.$refs.paging.reload();
			}
		}
	}
};
</script>

<style lang="scss">
.tabs {
  padding: 0 30rpx;
  display: flex;
  margin-bottom: 30rpx;
  background-color: #fff;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

  .tab {
    flex: 1;
    height: 80rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 26rpx;
    color: #666;
    position: relative;

    &.active {
      color: #409EFF;

      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 60rpx;
        height: 4rpx;
        background-color: #409EFF;
        border-radius: 2rpx;
      }
    }
  }
}

.tab-content {
  padding: 0 30rpx;
  
  .form-card {
    background-color: #fff;
    border-radius: 10rpx;
    padding: 30rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.08);

    .form-item {
      margin-bottom: 30rpx;

      .label {
        display: block;
        font-size: 26rpx;
        color: #333;
        margin-bottom: 16rpx;
        font-weight: 600;
      }

      .input {
        width: 100%;
        height: 72rpx;
        border-radius: 10rpx;
        border: 1rpx solid #e5e5e5;
        padding: 0 20rpx;
        font-size: 24rpx;
        box-sizing: border-box;
      }

      .textarea {
        width: 100%;
        min-height: 160rpx;
        border-radius: 10rpx;
        border: 1rpx solid #e5e5e5;
        padding: 20rpx;
        font-size: 24rpx;
        box-sizing: border-box;
        line-height: 1.6;
      }

      .count {
        display: block;
        margin-top: 10rpx;
        text-align: right;
        font-size: 20rpx;
        color: #999;
      }

      .picker {
        width: 100%;
        height: 72rpx;
        border-radius: 10rpx;
        border: 1rpx solid #e5e5e5;
        padding: 0 20rpx;
        display: flex;
        align-items: center;
        font-size: 24rpx;
        color: #333;
        box-sizing: border-box;
        
        select {
          width: 100%;
          height: 100%;
          border: none;
          outline: none;
          font-size: 24rpx;
          color: #333;
        }
      }
    }
  }

  .submit-section {
    margin-top: 40rpx;
    margin-bottom: 30rpx;

    .submit-btn {
      height: 88rpx;
      background-color: #409EFF;
      color: #fff;
      border-radius: 44rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 30rpx;
      font-weight: 600;
      box-shadow: 0 4rpx 12rpx rgba(64, 158, 255, 0.35);
    }
  }

  .record-card {
    background-color: #fff;
    border-radius: 10rpx;
    padding: 24rpx 26rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.06);

    .card-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 16rpx;

      .left {
        display: flex;
        align-items: center;

        .type {
          font-size: 26rpx;
          font-weight: 600;
          color: #333;
          margin-right: 16rpx;
        }

        .status {
          padding: 6rpx 18rpx;
          border-radius: 20rpx;
          font-size: 20rpx;
        }

        .status-pending {
          background-color: #E6F7FF;
          color: #409EFF;
        }

        .status-approved {
          background-color: #F6FFED;
          color: #52C41A;
        }

        .status-rejected {
          background-color: #FFF1F0;
          color: #FF4D4F;
        }
      }

      .header-right {
        display: flex;
        align-items: center;
        flex-direction: column;
        align-items: flex-end;

        .time {
          font-size: 22rpx;
          color: #999;
          margin-bottom: 8rpx;
        }
      }
    }

    .card-body {
      .row {
        display: flex;
        margin-bottom: 10rpx;

        .label {
          width: 140rpx;
          font-size: 24rpx;
          color: #666;
        }

        .value {
          flex: 1;
          font-size: 24rpx;
          color: #333;
        }
      }
    }

    .card-footer {
      margin-top: 16rpx;
      display: flex;
      align-items: center;
      justify-content: flex-end;

      .voucher-btn {
        padding: 8rpx 24rpx;
        background-color: #409EFF;
        color: #fff;
        border-radius: 16rpx;
        font-size: 22rpx;
      }
    }
  }
}
</style>