<template>
  <gui-page :custom-header="true" :header-class="['gui-theme-background-color']">
    <template #gHeader>
      <view style="height:44px;" class="gui-flex gui-nowrap gui-rows gui-align-items-center">
        <!-- 使用组件实现返回按钮及返回首页按钮 -->
        <text
          style="font-size:44rpx;"
          class="gui-header-leader-btns gui-color-white font-icons"
          @tap="goBack"
        >&#xe6c5;</text>
        <!-- 导航文本此处也可以是其他自定义内容 -->
        <text
          class="gui-h4 gui-blod gui-flex1 gui-text-center gui-ellipsis gui-color-white gui-primary-text"
        >发料明细</text>
        <!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
        <!-- 实际宽度请根据自己情况设置 -->
        <view style="width:40px;" />
        <!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
      </view>
    </template>
    <template #gBody>
      <view class="list-content">
        <view v-if="cardList.length > 0" class="list-panel">
          <view v-for="(item, key) in cardList" :key="key" class="panel">
            <view class="flex-row">
              <view class="panel-row">
                <text>物料编码</text>
                <text>{{ item.erpMaterialId }}</text>
              </view>
              <view class="panel-row">
                <text>物料批次</text>
                <text>{{ item.wmsLabelMasterId }}</text>
              </view>
              <view class="panel-row">
                <text>物料名称</text>
                <text>{{ item.materialName }}</text>
              </view>
              <view class="panel-row">
                <text>批次数量</text>
                <text>{{ item.batchQty }}</text>
              </view>
            </view>
          </view>
        </view>
        <view v-else>
          <view class="bg-image">
            <image src="@/static/empty.png" mode="heightFix" />
            <text>这里什么都没有...</text>
          </view>
        </view>
      </view>
    </template>
  </gui-page>
</template>

<script>
import {
  ref,
  defineComponent,
  computed
} from 'vue'
import {
  onReachBottom
} from '@dcloudio/uni-app'
export default defineComponent({
  setup(options) {
    const issueId = options.issueId ?? ''
    const cardList = ref([])
    const stateToText = ref('')
    const queryParams = ref({
      pageSize: 10,
      pageNo: 1
    })
    const stateComputed = computed(() => {
      return (state) => {
        switch (state) {
        case 0:
          stateToText.value = '待发料'
          break
        case 1:
          stateToText.value = '已发料-部分'
          break
        case 2:
          stateToText.value = '已发料-全部'
          break
        case 3:
          stateToText.value = '发料成功-部分'
          break
        case 4:
          stateToText.value = '发料成功-全部'
          break
        }
        return stateToText.value
      }
    })

    // 发料明细
    uni.$reqGet('issueDetail', {
      id: issueId,
      ...queryParams.value
    })
      .then(({
        data
      }) => {
        cardList.value = data.list ?? []
      })

    const goBack = function() {
      uni.navigateBack({
        delta: 1
      })
    }
    const handleNavigateTo = function() {
      uni.navigateTo({
        url: '/pages/workbranch/warehouse/productionIssue/demandDetails'
      })
    }
    // uniapp移动端触底事件
    onReachBottom(() => {
      queryParams.value.pageNo += 1
      // 生产发料主列表
      uni.$reqGet('issueDetail', {
        id: issueId,
        ...queryParams.value
      })
        .then(({
          data
        }) => {
          Array.prototype.push.call(cardList.value, ...data?.list ?? [])
        })
    })
    return {
      goBack,
      cardList,
      stateComputed,
      handleNavigateTo
    }
  }
})
</script>

<style lang="scss" scoped>
	.gui-header-leader-btns {
		color: black;
		font-size: 24px !important;
		margin-left: 24rpx;
	}

	.list-content {
		margin-top: 80px;
		background-color: #edeeee;
	}
</style>
