<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="panel-row">
              <text>{{ item.outStockNo }}</text>
            </view>
            <view class="panel-row">
              <text>发料人员</text>
              <text>{{ item.creator }}</text>
            </view>
            <view class="panel-row">
              <text>发料状态</text>
              <text>{{ stateComputed(item.type) }}</text>
            </view>
            <view class="panel-row">
              <text>发料时间</text>
              <text>{{ computedCreateTime(item.createTime) }}</text>
            </view>
            <view class="panel-row-operation">
              <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 {
  ref,
  defineComponent,
  computed
} from 'vue'
import {
  onReachBottom
} from '@dcloudio/uni-app'
export default defineComponent({
  setup(options) {
    const queryParams = ref({
      pageSize: 10,
      pageNo: 1
    })
    const cardList = ref([])
    const stateToText = ref('')

    const issueId = options.issueId ?? ''
    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 computedCreateTime = computed(() => {
      return item => {
        return new Date(item).toLocaleString()
      }
    })

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

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

	.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>
