<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"
          style="font-size: 1rem;"
        >备料单列表</text>
        <!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
        <!-- 实际宽度请根据自己情况设置 -->
        <view style="width:40px;" />
        <!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
      </view>
    </template>
    <template #gBody>
      <view class="list-content">
        <view v-if="cardList?.length > 0" class="list-panel">
          <div
            v-for="(item, key) in cardList"
            :key="key"
            class="panel"
            @click="handleToOutSourceMaterials(item)"
          >
            <div class="panel-row">
              <span>{{ item.prepareOrderNo }}</span>
              <span class="font-icons">前往备料&#xe66b;</span>
            </div>
            <div class="panel-row">
              <span>备料状态</span>
              <span>{{ computedState(item.syntheticalStatus) }}</span>
            </div>
          </div>
        </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,
  onMounted
} from 'vue'
export default defineComponent({
  setup() {
    const queryParams = ref({
      pageSize: 10,
      pageNo: 1,
      prepareType: '1'
    })
    const scanBatchNumber = ref('')
    const cardList = ref([])

    const computedState = computed(() => {
      return item => {
        switch (item) {
        case 0:
          return '待备料'
        case 1:
          return '已备料'
        case 2:
          return '已发料'
        }
      }
    })

    const search = function() {
      uni.$reqGet('getPrepareMatersList', {
        ...queryParams.value
      })
        .then(({
          code,
          data,
          msg
        }) => {
          if (code === 0) {
            cardList.value = data?.list
          } else {
            uni.showToast({
              title: msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
    }

    onMounted(() => {
      search()
    })

    const goBack = function() {
      uni.removeStorageSync('masterId')
      uni.$goHome()
    }

    const handleToOutSourceMaterials = function(params) {
      uni.setStorageSync('masterId', JSON.stringify({
        id: params?.id
      }))
      uni.navigateTo({
        url: `/pages/workbranch/warehouse/outSourceMaterial/outSourceMaterials`
      })
    }

    // uniapp移动端触底事件
    onReachBottom(() => {
      queryParams.value.pageNo += 1
      uni.$reqGet('getPrepareMatersList', queryParams.value)
        .then(({
          code,
          data,
          msg
        }) => {
          if (code === 0) {
            Array.prototype.push.call(cardList.value, ...data?.list ?? [])
          } else {
            uni.showToast({
              title: msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
    })
    return {
      goBack,
      cardList,
      search,
      computedState,
      queryParams,
      scanBatchNumber,
      handleToOutSourceMaterials
    }
  }
})
</script>

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

	.list-content {
		margin-top: 80px;
		min-height: calc(100vh - 80px);
		background-color: #edeeee;
	}

	span,
	text {
		font-size: 12px;
	}

	.card-list-flexbox {
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;
		margin: 3px 2px;

		.card-list-item {
			width: 750rpx;
			min-height: 40px;
			margin: 2rpx 0;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;

			uni-text {
				font-size: 14px;
				min-height: 40px;
				text-align: left;
				padding: 0 12px;
				display: flex;
				flex-direction: row;
				align-items: center;
			}

			.text-1 {
				flex: 1;
				min-height: 40px;
				justify-content: flex-start;
			}

			.text-2 {
				flex: 3;
				min-height: 40px;
				justify-content: flex-end;
				margin-right: 4px;
				padding: 2px 6px;
			}
		}

		.card-list-item:nth-of-type(1) {

			.text-1 {
				flex: 9;
				font-weight: bold;
				color: black !important;
			}

			.text-2 {
				flex: 4;
				display: flex;
				flex-direction: row;
				justify-content: flex-end;
				align-items: center;
				color: limegreen !important;
			}
		}

	}
</style>
