<template>
  <view class="goods-page">
    <u-navbar
      height="40px"
      title="货物信息录入"
      bgColor="#fff"
      autoBack
      placeholder
    >
      <view class="u-nav-slot depot-label" slot="right" @click="stashClick">
        <view class="name">{{ curDepotName }}</view>
      </view>
    </u-navbar>
    <view class="container_main">
      <u-sticky :offsetTop="offsetTop" bgColor="#fff">
        <view class="search-box">
          <u-search
            placeholder="请输入单据编号或名称"
            shape="square"
            v-model="query.number"
            :showAction="false"
            :clearabled="true"
            @search="handleSearch"
            @clear="handleSearch"
          ></u-search>
        </view>
      </u-sticky>
      <view class="content-box">
        <view class="content-box-val"> </view>
        <u-empty
          mode="data"
          text="暂无内容"
          marginTop="60"
          icon="https://xiangli-erp.oss-cn-hangzhou.aliyuncs.com/APP/no-notifcations.png"
          v-if="list.length == 0"
        ></u-empty>
        <!-- 加载更多 -->
        <u-loadmore
          v-if="list.length > 0"
          :status="loadStatus"
          @loadmore="onLoadMore"
        />
      </view>
    </view>

    <error-pop
      v-model="actionPop.errorShow"
      isCenter
      cancelBtnText="取消"
      confirmBtnText="确定"
      @close="actionPop.errorShow = false"
      @confirm="submit"
      :content="actionPop.errorText"
    ></error-pop>
  </view>
</template>

<script>
import checkItem from "./components/check-item.vue";

import {
  taskStocktakingList,
  startTask,
  taskComplete,
  taskStocktakingDetail,
} from "@/common/request/apis/inventoryTask";
import { mapGetters } from "vuex";

export default {
  components: {
    checkItem,
    errorPop,
  },
  data() {
    return {
      offsetTop: 0,
      query: {
        number: "",
      },
      currentPage: 1,
      pageSize: 10,
      loadStatus: "loadmore", //加载前值为loadmore，加载中为loading，没有数据为nomore
      list: [],

      // 中心仓
      cangName: "",
    };
  },
  computed: {
    ...mapGetters(["depotInfo"]),
    curDepotId() {
      return this.depotInfo ? this.depotInfo.id : "";
    },
    curDepotName() {
      return this.depotInfo ? this.depotInfo.depotName : "";
    },
  },
  onLoad() {
    let systemInfo = uni.getSystemInfoSync();
    let statusBarHeight = systemInfo.statusBarHeight;
    this.offsetTop = statusBarHeight + 40;
    this.loadData();
  },
  onShow() {},
  onPullDownRefresh() {
    // 下拉刷新
    this.onRefresh();
  },
  onReachBottom() {
    // 触底加载更多
    this.onLoadMore();
  },
  methods: {
    async onRefresh() {
      try {
        await this.loadData(true);
      } finally {
        uni.stopPullDownRefresh();
      }
    },
    async onLoadMore() {
      if (this.loadStatus !== "loadmore") return;
      this.loadStatus = "loading";
      try {
        await this.loadData();
      } catch (e) {
        this.loadStatus = "loadmore";
      }
    },
    async loadData(isRefresh = false) {
      if (isRefresh) {
        this.currentPage = 1;
        this.list = [];
      }

      try {
        this.loadStatus = "loading";
        const currentPage = this.currentPage;
        const pageSize = this.pageSize;
        const params = {
          currentPage,
          pageSize,
          ...this.query,
          depotId: this.curDepotId,
        };
        console.log("params=====", params);
        const res = await taskStocktakingList(params);
        const { rows, total } = res.data;
        this.list = [...this.list, ...rows];
        if (currentPage * pageSize < Number(total)) {
          this.currentPage++;
          this.loadStatus = "loadmore";
        } else {
          this.loadStatus = "nomore";
        }
      } catch (error) {}
    },
    handleSearch(value) {
      this.loadData(true);
    },
  },
};
</script>

<style lang="scss" scoped>
.goods-page {
  min-height: 100vh;
  background-color: #f0f6fb;
  .container_main {
    .search-box {
      background-color: rgba(191, 200, 219, 0.2);
      margin: 0 32rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-radius: 8rpx;

      ::v-deep .u-search__content {
        background-color: transparent !important;

        .u-search__content__input {
          background-color: transparent !important;
        }
      }

      .scan-icon {
        width: 100rpx;
        height: 100%;

        image {
          width: 40rpx;
          height: 40rpx;
        }
      }
    }

    .content-box {
      padding: 24rpx;
      &-val {
        border-radius: 16rpx 16rpx 0 0;
        overflow: hidden;
      }
    }
  }
}
</style>
