<template>
  <u-popup
    :show="show"
    mode="bottom"
    :round="10"
    :closeable="true"
    @close="handleClose"
    :zIndex="999999999999999999"
  >
    <view class="c-dialog-filter">
      <view class="title">{{ isShipment ? "出货" : "收货" }}产品</view>
      <view class="dialog-product-layout" v-if="!isShipment">
        <text
          v-for="item in state.list"
          :class="{ active: state.currentId === item.id }"
          :key="item.id"
          @click="handleSelect(item.id)"
          >{{ item.reProductsName }}</text
        >
      </view>
      <view v-if="isShipment">
        <block v-for="(item, index) in state.list" :key="index">
          <view style="font-size: 26rpx">{{ item.shmCategoryName }}</view>
          <view class="dialog-product-layout">
            <text
              v-for="cItem in item.childrenList"
              :class="{ active: state.currentId === item.id }"
              :key="cItem.id"
              @click="handleSelect(cItem.id)"
              >{{ cItem.shmCategoryName }}</text
            >
          </view>
        </block>
      </view>
      <!-- <view class="btn-confirm">
        <view class="btn">
          <u-button type="primary" :text="'确认'"></u-button>
        </view>
      </view> -->
    </view>
  </u-popup>
</template>
<script setup lang="ts">
import { GoodsApi, ReceiveProductApi } from "@/services";

const props = defineProps<{
  show: boolean;
  isShipment: boolean;
}>();
const emit = defineEmits(["handleDialog", "changeProduct"]);
const handleClose = () => {
  emit("handleDialog", false);
};

const state = reactive<any>({
  list: [],
  currentId: -1
});

const handleSelect = (id: number) => {
  state.currentId = id
  emit("changeProduct", {productId: id})
  emit("handleDialog", false);
}
const getList = () => {
  if (props.isShipment) {
    GoodsApi.getShipmentCategory().then((res) => {
      if (res.code === 200) {
        state.list = res.data;
      }
    });
  } else {
    GoodsApi.getReceiveProductList().then((res) => {
      if (res.code === 200) {
        state.list = res.data;
      }
    });
  }
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.c-dialog-filter {
  width: 95vw;
  margin: 25rpx;
  .title {
    font-weight: 500;
    font-size: 32rpx;
    color: #000000;
    text-align: center;
  }
}
// 产品dialog
.dialog-product-layout {
  box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(5, 68, 37, 0.12);
  border-radius: 13rpx;
  margin: 42rpx 25rpx;
  padding: 19rpx;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-flow: row wrap;
  max-height: 70vh;
  overflow: auto;

  text {
    font-weight: 400;
    font-size: 24rpx;
    color: #999999;
    background: #ffffff;
    border-radius: 6rpx;
    border: 1px solid rgba(153, 153, 153, 0.64);
    text-align: center;
    cursor: pointer;
    padding: 0rpx 20rpx;
    margin-bottom: 20rpx;
    margin-left: 10rpx;
    margin-right: 10rpx;
  }
  .active {
    border-color: $u-primary;
    color: $u-primary;
  }
}

.btn-confirm {
  box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0, 56, 93, 0.12);
  border-radius: 13rpx 13rpx 0rpx 0rpx;
  width: 100%;
  left: 0rpx;
  padding: 27.56rpx;
  .btn {
    text-align: center;
    ::v-deep button {
      width: 80%;
      border-radius: 43rpx;
    }
  }
}
</style>
