<template>
  <van-nav-bar
    :title="$t('forward.name')"
    left-text=""
    left-arrow
    @click-left="onClickLeft"
    @click-right="onClickRight"
  >
    <template #right> 扫码 </template>
  </van-nav-bar>
  <van-search
    v-model="req.keyword"
    :placeholder="$t('common.pleaseEnterKeywords')"
    @search="onRefresh"
  />

  <van-pull-refresh v-model="loading" @refresh="onRefresh">
    <div class="list">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        :finished-text="$t('common.noMore')"
        @load="onLoad"
        style="margin-bottom: 60px"
      >
        <commonList
          :data="listData"
          :config="listConfig"
          :showMore="true"
          @onClick="toDtl"
        ></commonList>
      </van-list>
    </div>
  </van-pull-refresh>
</template>
<script setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import commonList from "@/components/common-list.vue";
import { useRoute } from "vue-router";
const loading = ref(false);
const router = useRoute();
const req = ref({
  pageNum: 1,
  keyword: null,
});
const finished = ref(false);
const proxy = getCurrentInstance().proxy;
const listData = ref([]);

const listConfig = ref([
  {
    label: proxy.t("forward.productName"),
    prop: "productName",
  },

  {
    label: proxy.t("forward.productSN"),
    prop: "productSn",
  },
  {
    label: proxy.t("forward.currentProcess"),
    prop: "productionProcessesName",
  },
]);
const onRefresh = () => {
  req.value.pageNum = 1;
  finished.value = false;
  getList("refresh");
};
const onLoad = () => {
  getList();
};

const onClickLeft = () => proxy.$router.push("/main/working");

// const onClickRight = () => {
//   proxy.$router.push("jxskForwardAdd");
// };

const toDtl = (row) => {
  let type = "";
  if (
    row.nextProductionProcessesId == "" ||
    row.nextProductionProcessesId == "-1"
  ) {
    type = "20";
  } else {
    type = "10";
  }
  proxy.$router.push({
    path: "jxskForwardAdd",
    query: {
      ...row,
      type: type,
    },
  });
};

const getList = (type) => {
  loading.value = true;
  proxy
    .post("/productionTaskDetail/circulationPage", req.value)
    .then((res) => {
      listData.value =
        type === "refresh"
          ? res.data.rows
          : listData.value.concat(res.data.rows);
      if (req.value.pageNum * 10 >= res.data.total) {
        finished.value = true;
      }
      req.value.pageNum++;
      loading.value = false;
    })
    .catch((err) => {
      loading.value = false;
    });
};

getList();

const onClickRight = () => {
  // 允许从相机和相册扫码
  uni.scanCode({
    success: function (res) {
      console.log("条码类型：" + res.scanType);
      console.log("条码内容：" + res.result);
      uni.showToast({
        title: res.result,
        duration: 2000,
      });
    },
  });
};
</script>

<style lang="scss" scoped>
.list {
  min-height: 70vh;
}
</style>