<template>
  <u-popup :show="show" mode="right" :closeable="true" @close="handleClose">
    <view class="c-dialog-filter">
      <view class="title">{{ isShipment ? "出货" : "收货" }}产品</view>
      <view v-if="isShipment">
        <view v-for="(item, index) in state.list" :key="index">
          <view class="first-title">{{ item.shmCategoryName }}</view>
          <view v-for="(cItem, cIndex) in item.childrenList" :key="cIndex">
            <view class="second-title">{{ cItem.shmCategoryName }}</view>

            <view class="dialog-product-layout">
              <view v-if="cItem.childrenLists">
                <text
                  v-for="(child, childIndex) in cItem.childrenLists"
                  :key="childIndex"
                  :class="{ active: state.current === child.id }"
                  @click="handleSelect(child)"
                  >{{ child.shmProductsName }}</text
                >
              </view>

              <view v-else style="flex: auto">
                <u-empty
                  mode="data"
                  icon="http://cdn.uviewui.com/uview/empty/data.png"
                >
                </u-empty>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view class="dialog-product-layout" v-else>
        <text
          v-for="(item, index) in state.list"
          :class="{ active: state.current === item.id }"
          @click="handleSelect(item)"
          :key="index"
          >{{ isShipment ? item.shmProductsName : item.reProductsName }}</text
        >
      </view>
    </view>
    <view class="bottom">
      <u-button
        text="清空"
        color="#E8E8E8"
        :customStyle="{ color: '#999' }"
        shape="circle"
        @click="reset"
      ></u-button>
    </view>
  </u-popup>
</template>
<script setup lang="ts">
import { ReceiveProductApi, GoodsApi } from "@/services";

const props = defineProps<{
  show: boolean;
  isShipment: boolean;
}>();
const emit = defineEmits(["handleDialog", "changeProduct"]);
const handleClose = () => {
  emit("handleDialog", false);
};
const reset = () => {
  state.current = -1;
  emit("changeProduct", { id: -1 });
  emit("handleDialog", false);
};
const state = reactive<any>({
  list: [],
  current: -1,
});

const handleSelect = (item: any) => {
  state.current = item.id;
  emit("changeProduct", item);
  emit("handleDialog", false);
};
const getList = () => {
  if (props.isShipment) {

    GoodsApi.getChildrenList().then((res) => {
      if (res.code === 200) {
        if (res.code === 200) {
          state.list = res.data;
        }
      }
    });
  } else {
    ReceiveProductApi.getAllReProducts().then((res) => {
      if (res.code === 200) {
        state.list = res.data;
      }
    });
  }
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.c-dialog-filter {
  width: 95vw;
  padding: 25rpx;
  height: 90vh;
  overflow-y: scroll;
  .title {
    font-weight: 500;
    font-size: 32rpx;
    color: #000000;
    text-align: center;
  }
}
.first-title {
  font-size: 28rpx;
  font-weight: 500;
}
.second-title {
  font-size: 26rpx;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.7);
  margin: 20rpx;
}
// 产品dialog
.dialog-product-layout {
  box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(5, 68, 37, 0.12);
  border-radius: 13rpx;
  margin: 30rpx 24rpx;
  padding: 20rpx;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-flow: row wrap;
  max-height: 70vh;
  overflow: auto;

  text {
    font-weight: 400;
    font-size: 26rpx;
    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;
  position: absolute;
  bottom: 0rpx;
  width: 100%;
  left: 0rpx;
  padding: 27.56rpx;
  .btn {
    text-align: center;
    ::v-deep button {
      width: 80%;
      border-radius: 43rpx;
    }
  }
}

.bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  margin: 30rpx;
  width: calc(100% - 60rpx);
}
</style>