<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="goHome"
        >&#xe6c5;</text>
        <!-- 导航文本此处也可以是其他自定义内容 -->
        <text
          class="gui-h4 gui-blod gui-flex1 gui-text-center gui-ellipsis gui-color-white gui-primary-text"
          style="font-size: 14px;"
        >生产发料</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">
            <div class="panel-row">
              <span>{{ item.productCode }}</span>
            </div>
            <div class="panel-row">
              <span>产品名称</span>
              <span>{{ item.productName }}</span>
            </div>
            <div class="panel-row">
              <span>领料单号</span>
              <span>{{ item.workOrderBomNo }}</span>
            </div>
            <div class="panel-row">
              <span>计划数量</span>
              <span>{{ item.productAmount * 1 }}</span>
            </div>
            <div class="panel-row">
              <span>生产订单</span>
              <span>{{ item.workOrderNo }}</span>
            </div>
            <div class="panel-row">
              <span>发料状态</span>
              <span>{{ stateComputed(item.sendStatus) }}</span>
            </div>
            <view class="panel-row-operation">
              <text class="tag-limegreen" @click="handleNavigateTo('发料记录', item.id)">发料记录</text>
              <text class="tag-skyblue" @click="handleNavigateTo('需求明细', item.id)">需求明细</text>
            </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 {
  onReachBottom
} from '@dcloudio/uni-app'
import {
  ref,
  defineComponent,
  computed
} from 'vue'
export default defineComponent({
  setup() {
    const queryParams = ref({
      pageSize: 10,
      pageNo: 1
    })
    const stateToText = ref('')
    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
      }
    })
    const cardList = ref([])
    // 生产发料主列表
    uni.$reqGet('productionIssueList')
      .then(({
        data
      }) => {
        cardList.value = data.list ?? []
      })

    // uniapp移动端触底事件
    onReachBottom(() => {
      queryParams.value.pageNo += 1
      // 生产发料主列表
      uni.$reqGet('productionIssueList', queryParams.value)
        .then(({
          data
        }) => {
          Array.prototype.push.call(cardList.value, ...data?.list ?? [])
        })
    })

    const goHome = function() {
      uni.$goHome()
    }
    const handleNavigateTo = function(name, id) {
      switch (name) {
      case '发料记录':
        uni.navigateTo({
          url: `/pages/workbranch/warehouse/productionIssue/issueRecord?issueId=${id}`
        })
        break
      case '需求明细':
        uni.navigateTo({
          url: `/pages/workbranch/warehouse/productionIssue/demandDetails?issueId=${id}`
        })
        break
      }
    }
    return {
      goHome,
      cardList,
      stateComputed,
      handleNavigateTo
    }
  }
})
</script>

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

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

	.tag-limegreen,
	.tag-skyblue {
		padding: 0 8px;
		margin: 4px;
		border-radius: 5px;
		font-size: 12px;
		color: white;
	}

	.tag-limegreen {
		background-color: #2fb657;
	}

	.tag-skyblue {
		background-color: #00a0e9;
	}
</style>
