<template>
  <Theme>
    <view class="wrap">
      <Navbar title="确认订单" fixed border autoBack @leftClick="leftClick">
        <template #right>
          <!-- <navMenu :options="{ icon: 'icon-home', text: '主页' }" /> -->
        </template>
      </Navbar>
      <view class="content">
        <view class="cont">
          <addressModel @confirm="addressConfirm" />
          <view
            class="card_wrapper"
            v-for="(item, index) in orderList"
            :key="index"
          >
            <view class="card_header">
              <!-- <view class="header_img">
                <image
                  class="img"
                  :src="`../../static/shop/icon_${item.channel}.png`"
                ></image>
              </view> -->
              <view class="header_name">{{ item.seller_name }}</view>
            </view>
            <view class="card_item">
              <view
                class="card_item_wrapper"
                v-for="(val, num) in item.goods"
                :key="num"
              >
                <view class="item_top">
                  <view class="item_left">
                    <image :src="val.picurl" class="item_img"></image>
                  </view>
                  <view class="item_middle">
                    <view class="title">{{ val.goodsName }}</view>
                    <view class="desc">{{ val.skudesc }}</view>
                  </view>
                  <view class="item_right">
                    <view class="price">
                      <text>{{ symbol.symbol }}</text>
                      <text>{{ Moneyhtml(val.price) }}</text>
                    </view>
                    <view class="total_num">X {{ val.total }}</view>
                  </view>
                </view>
              </view>
              <view class="card_footer">
                <up-collapse :border="false" :value="collapseValue">
                  <up-collapse-item :name="item.seller_id" :border="false">
                    <template #title>
                      <view class="collapse_title">
                        <trans _t="订单总价" />:
                      </view>
                    </template>
                    <template #value>
                      <view class="collapse_value">
                        <text>{{ symbol.symbol }} </text>
                        <text class="item_num">{{
                          Moneyhtml(totalAmount)
                        }}</text>
                      </view>
                    </template>
                    <view class="card_footer_item" style="padding-top: 24rpx">
                      <view class="item_label"> <trans _t="总价" />: </view>
                      <view class="item_value item_red">
                        <text>{{ symbol.symbol }} </text>
                        <text class="item_num">{{
                          Moneyhtml(item.allAmt)
                        }}</text>
                      </view>
                    </view>
                    <!-- <view class="card_footer_item">
                      <view class="item_label">
                        <trans _t="运费到仓库" />:
                      </view>
                      <view class="item_value">
                        <text>{{ symbol.symbol }}</text>
                        <text class="item_num">{{
                          Moneyhtml(item.postAmt)
                        }}</text>
                      </view>
                    </view>
                    <view class="card_footer_item">
                      <view class="item_label"> <trans _t="折扣" />: </view>
                      <view class="item_value item_red">
                        <text>-{{ symbol.symbol }}</text>
                        <text class="item_num">{{
                          Moneyhtml(item.disAMt)
                        }}</text>
                      </view>
                    </view> -->
                  </up-collapse-item>
                </up-collapse>
              </view>
            </view>
          </view>
          <!-- <view class="friendly_reminder">
            <trans class="reminder_title" _t="温馨提示" />
            <view class="reminder_desc">
              <trans _t="提示内容" />
            </view>
          </view> -->

          <!-- <couponModel
            :item="couponList"
            @change="changeId"
            isWidth
            style="margin-top: 24rpx"
            v-if="isFree == 0"
          /> -->
        </view>
        <view class="footer">
          <view class="footer_protocol">
            <view class="protocol">
              <view
                class="protocol_item"
                v-for="(item, index) in protocolList"
                :key="index"
                @click="openAgent(item)"
              >
                <trans :_t="item.text" />
              </view>
            </view>
            <view class="agreed_protocol">
              <view
                class="confirm_box"
                :class="lang == 'zh' ? 'zh_protocol' : ''"
              >
                <Tip title="请阅读并同意" :show="tipShow">
                  <up-checkbox
                    activeColor="var(--black)"
                    labelSize="14"
                    labelColor="#676969"
                    iconSize="16"
                    name="agree"
                    usedAlone
                    v-model:checked="selectAllChecked"
                    class="right-check"
                  >
                    <template #label>
                      <trans class="text" _t="我已阅读并同意" />
                    </template>
                  </up-checkbox>
                </Tip>
              </view>
            </view>
          </view>
          <view class="footer_confirm">
            <view class="total_info">
              <view class="price_num">
                <text class="price_text" v-if="postAmt > 0">
                  (
                  <trans _t="增值费用" />:
                  <text class="amont_num"
                    >{{ symbol.symbol }}{{ Moneyhtml(postAmt) }}</text
                  >)
                </text>
                <text>{{ symbol.symbol }}</text>
                <text class="num">{{ Moneyhtml(totalAmount) }}</text>
              </view>
              <view class="price_text">
                <trans _t="总计不含国际运费" />
              </view>
            </view>
            <view class="total_btn" @click="submit">
              <trans _t="提交" />
            </view>
          </view>
        </view>
      </view>
    </view>
    <SimpleAgreementModal ref="agreementModal" />
  </Theme>
</template>

<script setup>
import Navbar from "@/components/navbar";
import navMenu from "@/components/nav_menu";
import { onLoad } from "@dcloudio/uni-app";
import { ref, nextTick, computed, watch } from "vue";
import { SELLER_CART_CONFIRM, SELLER_ORDER_SUBMIT } from "@/api";
import { Toast, Moneyhtml } from "@/utils";
import { useSystemStore, useTabbarStore, useShopStore } from "@/store";
import Tip from "@/components/tooltip";
import SimpleAgreementModal from "@/components/simpleAgreementModal";
import addressModel from "@/pages/address/components/addressModel";
import { t } from "@/locale";

const useSystem = useSystemStore();
const useTabbar = useTabbarStore();
const useShop = useShopStore();
const symbol = computed(() => useSystem.getSymbol);
const lang = computed(() => useSystem.getLang);
const logistics = computed(() => useShop.getLogistics);
const confirmId = ref("");
const totalAmount = ref(0);
const selectAllChecked = ref(false);
const selectIsChecked = ref(false);
const orderList = ref([]);
const couponId = ref("");
const isPage = ref("");
const tipShow = ref(false);
const qualityAmount = ref(0);
const agreementModal = ref(null);
const couponList = ref([]);
const checkDate = ref(null);
const postAmt = ref(0);

const selectItem = ref(null);

const valueAddedModel = ref(null);
watch(selectAllChecked, (newVal) => {
  if (newVal) tipShow.value = false;
});
watch(selectIsChecked, (newVal) => {
  buyCart();
});

const protocolList = [
  {
    text: "违禁物品说明",
    type: "violation",
  },
  {
    text: "《服务条款》",
    type: "agree",
  },
  {
    text: "退货说明",
    type: "retreat",
  },
  {
    text: "免责声明",
    type: "disclaimer",
  },
];

const addressConfirm = (item) => {
  selectItem.value = item;
};
const openAgent = (item) => {
  agreementModal.value && agreementModal.value.open(item.type);
};

const collapseValue = computed(() => {
  let arr = orderList.value.reduce((acc, item) => {
    acc.push(item.seller_id);
    return acc;
  }, []);
  return arr;
});

const calculateAll = (arr) => {
  let totalQuantity = arr.reduce((sum, item) => sum + item.total, 0);
  return totalQuantity;
};

const leftClick = () => {
  uni.navigateBack();
  // if (isPage.value == "order") {
  //   uni.navigateBack();
  // } else {
  //   uni.navigateTo({ url: "/pages/index/products?channel=1" });
  // }
};

const issubnum = ref(0);
const submit = () => {
  if (issubnum.value == 0) {
    if (!selectItem.value.id) return Toast(t("请先添加收件地址"));
    if (!selectAllChecked.value) return (tipShow.value = true);
    shopConfirm();
  }
};
const getCoupon = async (money) => {
  try {
    const res = await SHOP_GET_SHOPCOUPON(money);
    couponList.value = res.data || [];
  } catch (error) {}
};
const changeId = (id, data) => {
  if (id) {
    couponId.value = id;
    checkDate.value = data;
    buyCart();
  } else {
    couponId.value = "";
    checkDate.value = null;
  }
};

onLoad((options) => {
  confirmId.value = options.comfirmId;
  isPage.value = options.page || "";
  nextTick(() => {
    buyCart();
  });
});

const shopConfirm = async () => {
  try {
    const res = await SELLER_ORDER_SUBMIT({
      cartids: confirmId.value,
      address_id: selectItem.value?.id,
    });
    uni.navigateTo({
      url: `/pages/shop/payment?oid=${res.data.sid}&type=sellerorder`,
    });
  } catch (error) {
    Toast(error.msg);
  }
};

const buyCart = async () => {
  try {
    const res = await SELLER_CART_CONFIRM({
      cartids: confirmId.value,
    });
    orderList.value = res.data.cartList || [];
    totalAmount.value = res.data.totalAmount || 0;
    postAmt.value = res.data.postAmt || 0;
  } catch (error) {
    Toast(error.msg);
  }
};
</script>

<style lang="less" scoped>
@import url("@/style.less");

.uni-stat-tooltip {
  min-width: 400rpx;
  max-height: 400rpx;
  overflow: scroll;
  word-break: break-word;
}

.wrap {
  max-height: 100vh;

  .confirm_box {
    width: 100%;
    .hor(end);

    .confirm_item {
      .ver();
    }
  }

  .zh_protocol {
    // .flex_position(flex-end);

    // .right-check {
    //   margin-left: 30rpx;
    // }
  }

  background-color: var(--bg);
  .flex();
  flex-direction: column;
  overflow: hidden;

  .content {
    flex-grow: 1;
    height: calc(100vh - 44px);
    overflow: hidden scroll;
    .flex();
    flex-direction: column;

    .cont {
      padding: 0 24rpx 48rpx;
      flex-grow: 1;
      overflow: hidden scroll;

      .card_wrapper {
        padding: 16rpx 0;
        background-color: var(--light);
        margin-top: 24rpx;
        border-radius: 16rpx;

        .card_header {
          .ver();
          padding: 0 32rpx;

          .header_img {
            width: 48rpx;
            height: 48rpx;

            .img {
              width: inherit;
              height: inherit;
            }
          }

          .header_name {
            margin-left: 16rpx;
            font-weight: 700;
            .size(28rpx);
            color: var(--text);
          }
        }

        .card_item {
          &_wrapper {
            padding: 0 32rpx;
            padding-top: 16rpx;

            .item_top {
              .flex();
              column-gap: 16rpx;
              padding-bottom: 16rpx;

              .item_left {
                width: 128rpx;
                height: 128rpx;

                .item_img {
                  width: inherit;
                  height: inherit;
                  border-radius: 16rpx;
                }
              }

              .item_middle {
                flex: 1;

                .title {
                  color: var(--text);
                  .size(24rpx);
                  line-height: 48rpx;
                  font-weight: 700;
                  .ellipsis(2);
                }

                .desc {
                  color: var(--text-01);
                  .size(24rpx);
                  font-weight: 400;
                  line-height: 40rpx;
                  margin-top: 8rpx;
                }
              }

              .item_right {
                flex: 0 0 180rpx;
                text-align: right;

                .price {
                  .size(24rpx);
                  color: var(--red);
                  font-weight: 700;
                  line-height: 60rpx;
                }

                .total_num {
                  color: #7d8fb3;
                  .size(24rpx);
                  font-weight: 400;
                  line-height: 40rpx;
                }
              }
            }
          }

          .card_footer {
            border-top: 1px solid var(--bg);

            /deep/ .u-collapse {
              .u-cell--clickable {
                background-color: transparent;
              }

              .u-cell__body {
                padding: 0 40rpx;
                height: 48rpx;
                margin-top: 4rpx;

                .u-cell__left-icon-wrap {
                  margin-right: 0;
                }
              }

              // .u-collapse-item__content{
              //   padding-top: 24rpx;
              // }
              .u-collapse-item__content__text {
                padding: 0 40rpx;
              }

              .uicon-arrow-right {
                color: var(--black) !important;
                .size() !important;
                font-weight: 700 !important;
              }
            }

            .collapse_title {
              .size(24rpx);
              color: var(--text);
              font-weight: 500;
            }

            .collapse_value {
              color: var(--red);
              font-weight: 700;
              .size();

              .item_num {
                display: inline-block;
                margin-left: 8rpx;
              }
            }

            .card_footer_item {
              color: var(--text-01);
              .size(24rpx);
              height: 40rpx;
              .flex_position(space-between);
              box-sizing: content-box;

              .item_value {
                &.item_red {
                  color: var(--red);
                }

                .item_num {
                  // .blocked();
                  display: inline-block;
                  margin-left: 8rpx;
                }
              }
            }
          }
        }
      }

      .friendly_reminder {
        padding: 16rpx 24rpx 24rpx;
        background-color: var(--light);
        margin-top: 20rpx;
        border-radius: 16rpx;
        color: #346;
        .size(24rpx);
        min-height: 260rpx;

        .reminder_title {
          font-weight: 700;
          color: var(--text);
          .size(28rpx);
          line-height: 60rpx;
        }
      }

      .add_on_box {
        width: 100%;
        padding: 16rpx 24rpx;
        background-color: var(--bg-primary);
        border-radius: 16rpx;

        .title {
          color: var(--text);
          .size(28rpx);
          font-weight: 700;
          line-height: 60rpx;
          .title-tips {
            margin: 0 12rpx;
            padding: 2rpx 6rpx;
            color: #fff;
            .size(24rpx);
            background-color: red;
            border-radius: 8rpx;
          }
        }

        .add_on_content {
          .hor(space-between);
          flex-wrap: wrap;

          .checkbox-row {
            .ver();
          }

          .question-icon {
            margin-left: 8rpx;
            font-size: 32rpx;
          }
        }
      }
    }

    .footer {
      padding-bottom: env(safe-area-inset-bottom);
      padding-bottom: constant(safe-area-inset-bottom);
      box-sizing: border-box;
      &_protocol {
        background-color: #fff3ea;
        padding: 16rpx 24rpx;

        .protocol {
          .flex_position(flex-end);
          flex-wrap: wrap;

          &_item {
            text-wrap: nowrap;
            color: var(--black);
            .size(24rpx);
            margin-left: 8rpx;
          }
        }

        .agreed_protocol {
          // .flex_position(flex-end);
          // height: 60rpx;

          .text {
            .size(28rpx);
            color: var(--black);
          }
        }
      }

      &_confirm {
        background-color: var(--light);
        border-top: var(--bor);
        height: 120rpx;
        .flex_position(flex-end);
        padding: 0 24rpx;

        .total_info {
          .ver(flex-end);
          flex-direction: column;

          .price_num {
            color: var(--red);
            .size();
            font-weight: 700;
            line-height: 48rpx;

            .num {
              display: inline-block;
              margin-left: 8rpx;
            }

            .icon-question2 {
              color: var(--text-01);
              font-weight: 400;
              .size(40rpx);
              margin-left: 8rpx;
            }
          }

          .price_text {
            color: var(--text-01);
            .size(24rpx);
            line-height: 40rpx;

            .amont_num {
              color: var(--red);
            }
          }
        }

        .total_btn {
          background-color: var(--black);
          color: var(--light);
          height: 76rpx;
          .size(28rpx);
          font-weight: 700;
          min-width: 180rpx;
          margin-left: 24rpx;
          border-radius: 16rpx;
          .flex_center();
          padding: 16rpx 30rpx;
        }
      }
    }
  }
}
</style>