<template>
  <div class="sure-order-warp">
    <userHeader :title="'确认订单'"></userHeader>
    <div class="addr-list">
      <div class="addr-list-top">
        <div class="back">
          <el-icon><ArrowLeftBold /></el-icon>
          <span>确认收货地址</span>
        </div>
        <div class="addr-manage">管理地址</div>
      </div>
      <el-scrollbar>
        <div class="scrollbar-flex-content">
          <div
            v-for="item in addrlist"
            :key="item"
            class="addr-item"
            :class="currentAddr == item.id ? 'addr-item-act' : 'addr-item-normal'"
            @click="selectAddr(item.id)"
          >
            <div class="addr-item-icon">
              <div class="icon-pos">
                <div class="iconfont icon-location"></div>
              </div>
            </div>
            <div class="addr-item-info">
              <div class="region">{{ item.region || '--' }}</div>
              <div class="addr">{{ item.addr || '--' }}</div>
              <div class="link-name">{{ item.linkName }} {{ item.phone }}</div>
            </div>
          </div>
        </div>
      </el-scrollbar>
    </div>

    <div class="order-info">
      <div class="order-info-top">确认订单信息</div>
      <div class="order-info-list">
        <div class="content-item-warp">
          <div class="content-item-header">
            <div class="header-th first">商品</div>
            <div class="header-th other">规格</div>
            <div class="header-th other">原价</div>
            <div class="header-th other">数量</div>
            <div class="header-th other">小计</div>
          </div>
          <div class="content-item-list">
            <div v-for="(n, index) in datalist" :key="index" class="content-item">
              <div class="shop-info">
                <div class="shop-img">
                  <costomImg
                    :url="'images/ecommerce/' + 'pic.png'"
                    :preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')]"
                    :is-view="false"
                  ></costomImg>
                </div>
                <span class="shop-name txt-ellipsis clamp2">{{ n.shop }}</span>
              </div>

              <div v-if="n.goodlist && n.goodlist.length > 0" class="good-list">
                <div v-for="(g, indexg) in n.goodlist" :key="indexg" class="good-item">
                  <div class="good-img">
                    <costomImg
                      :url="'images/ecommerce/' + 'pic.png'"
                      :preview-list="[getAssetsFile('images/ecommerce/' + 'pic.png')]"
                      :is-view="false"
                    ></costomImg>
                  </div>
                  <div class="good-info">
                    <div class="good-info-pos">
                      <div class="txt-ellipsis clamp2">{{ g.title || '--' }}</div>
                    </div>
                  </div>
                  <div class="good-price-num">
                    <div class="good-price-num-pos">
                      <div class="price">{{ g.price }} / {{ g.unit }}</div>
                      <div class="total">{{ g.price }}</div>
                      <div class="num">
                        <div class="right-item">
                          <el-input-number v-model="g.num" :min="1">
                            <template #suffix>
                              <span>{{ g.unit }}</span>
                            </template>
                          </el-input-number>
                        </div>
                      </div>
                      <div class="good-total">
                        {{ (g.price * g.num).toFixed(2) }}
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="content-item-bottom">
            <div class="num-total">
              共
              <span class="num-val">{{ totalNum }}</span
              >件商品
            </div>
            <div class="price-total">
              <div class="amount cost-item">
                <span>商品总价：</span>
                <span class="coat-val">{{ totalAmout.toFixed(2) }}</span>
              </div>
              <div class="freight cost-item">
                <span>运费：</span>
                <span class="coat-val">{{ carriage.toFixed(2) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="order-bottom">
        <div class="bottom-total">
          <span class="tips">实付款</span>
          <span class="total">{{ (totalAmout + carriage).toFixed(2) }}</span>
        </div>
        <div class="bottom-do">
          <el-button type="primary" @click="toSureOrder">提交订单</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, computed } from 'vue';
import { isEmpty, getAssetsFile } from '@/utils';
import userHeader from './components/userHeader.vue';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

const addrlist = reactive([
  { id: '001', region: '云南省 昆明市 呈贡区 彩云南路', addr: '星光小区 一单元 1楼 101', linkName: '李先生', phone: '13813575526' },
  { id: '002', region: '云南省 昆明市 呈贡区 彩云南路', addr: '星光小区 一单元 1楼 102', linkName: '王先生', phone: '13813575528' },
  { id: '003', region: '云南省 昆明市 呈贡区 彩云北路', addr: '星光小区 一单元 1楼 101', linkName: '李女士', phone: '13813575527' },
  { id: '004', region: '云南省 昆明市 呈贡区 彩云南路', addr: '星光小区 一单元 1楼 102', linkName: '王女生', phone: '13813565525' },
  { id: '005', region: '云南省 昆明市 呈贡区 彩云南路', addr: '星光小区 一单元 1楼 105', linkName: '张女生', phone: '13813565526' },
]);

let currentAddr = ref('001');

const selectAddr = (data) => {
  currentAddr.value = data;
};

let datalist = reactive([
  {
    id: '01',
    shop: '银河生态农产品有限公司',
    shopimg: '',
    ischeck: false,
    goodlist: [
      { id: '001', title: '耿马镇 原生态 有机 西红柿', price: 4.9, unit: '份', num: 2, ischeck: false },
      { id: '002', title: '耿马镇 原生态 有机 西蓝花', price: 2.6, unit: '份', num: 100, ischeck: false },
    ],
  },
  {
    id: '02',
    shop: '方立生态农产品有限公司',
    shopimg: '',
    ischeck: false,
    goodlist: [{ id: '001', title: '勐撒镇 原生态 有机 大白菜', price: 4.9, unit: '份', num: 2, ischeck: false }],
  },
]);

const toSureOrder = () => {
  router.push('/sub-operation-service/userCenter-orderSuccess');
};

let carriage = ref(98);
let totalAmout = computed(() => {
  let num = 0;
  let list = [];
  if (datalist && datalist.length > 0) {
    list = datalist
      .map((m) => {
        return m.goodlist;
      })
      .flat();

    // console.info('totalAmout**************', list);
    if (list && list.length > 0) {
      num = list.reduce((acc, current) => {
        return acc + current.price * current.num;
      }, 0);
    }
  }

  return num;
});

let totalNum = computed(() => {
  let num = 0;
  let list = [];
  if (datalist && datalist.length > 0) {
    list = datalist
      .map((m) => {
        return m.goodlist;
      })
      .flat();

    if (list && list.length > 0) {
      num = list.reduce((acc, current) => {
        return acc + 1;
      }, 0);
    }
  }

  return num;
});
</script>
<style lang="scss" scoped>
.sure-order-warp {
  width: 100%;
  .addr-list {
    background: $color-fff;
    padding: 16px;
    border-radius: 16px;
    overflow: hidden;
    margin: 16px 0;
    .addr-list-top {
      display: inline-flex;
      justify-content: space-between;
      width: 100%;
      margin-bottom: 16px;
      .back {
        font-size: 20px;
        .el-icon {
          font-size: 30px;
        }
        .el-icon,
        span {
          display: inline-block;
          vertical-align: middle;
        }
      }
      .addr-manage {
        font-size: 20px;
      }
    }
    .scrollbar-flex-content {
      display: flex;
      width: fit-content;
      gap: 16px;
    }
    .addr-item {
      width: 280px;
      padding: 16px 8px;
      display: inline-flex;
      justify-content: flex-start;
      cursor: pointer;
      background: $color-fff;
      border-radius: 16px;
      &.addr-item-act {
        border: 1px solid $color-main;
        .iconfont {
          color: $color-main !important;
        }
      }
      &.addr-item-normal {
        border: 1px solid $color-da;
      }
      .addr-item-icon,
      .addr-item-info {
        display: inline-block;
        vertical-align: middle;
      }
      .addr-item-icon {
        display: inline-flex;
        justify-content: center;
        flex-direction: column;
        .iconfont {
          font-size: 20px;
          color: $color-666;
        }
      }
      .addr-item-info {
        font-size: 15px;
        padding-left: 8px;
        line-height: 24px;
        .region,
        .link-name {
          color: $color-999;
        }
      }
    }
  }

  .order-info {
    background: $color-fff;
    border-radius: 16px;
    width: 100%;
    height: calc(100vh - 340px);
    position: relative;
    padding: 16px;
    .order-info-top,
    .order-bottom,
    .order-info-list {
      position: absolute;
      left: 0;
      width: 100%;
      z-index: 1;
      padding: 16px;
    }
    .order-info-top {
      top: 0;
      font-size: 20px;
    }
    .order-bottom {
      bottom: 0;
      display: inline-flex;
      justify-content: space-between;
      .bottom-total,
      .bottom-do {
        display: inline-block;
        vertical-align: middle;
      }
      .bottom-total {
        .tips,
        .total {
          display: inline-block;
          vertical-align: middle;
        }
        .tips {
          font-size: 30px;
        }
        .total {
          font-size: 42px;
          color: $color-main;
          padding-left: 16px;
        }
        .total::before {
          content: '￥';
        }
      }
      .bottom-do {
        flex-direction: column;
        justify-content: center;
        display: inline-flex;
        ::v-deep() {
          .el-button {
            font-size: 18px !important;
            padding: 0 40px !important;
            line-height: 42px !important;
            height: 42px !important;
            display: inline-block !important;
          }
        }
      }
    }
    .order-info-list {
      top: 60px;
      width: calc(100% - 32px);
      height: calc(100% - 150px);
      background: $color-f5;
      left: 16px;
      padding: 16px;
      border-radius: 16px;
      position: relative;
      .content-item-warp {
        height: 100%;
        width: 100%;
        position: relative;
        .content-item-header {
          width: 100%;
          display: inline-flex;
          justify-content: flex-start;
          gap: 16px;
          position: absolute;
          left: 0;
          top: 0;
          .header-th {
            display: inline-block;
            vertical-align: middle;
            line-height: 32px;
            color: $color-999;
            font-size: 20px;
            margin-bottom: 16px;
            &.first {
              width: 340px;
              text-align: left;
            }
            &.other {
              width: calc((100% - 340px) / 4);
              text-align: center;
            }
          }
        }
        .content-item-list {
          width: 100%;
          top: 48px;
          left: 0;
          position: absolute;
          overflow-y: auto;
          height: calc(100% - 100px);
        }
        .content-item {
          width: 100%;
          margin-bottom: 16px;
          cursor: pointer;
          .shop-info {
            width: 100%;
            display: inline-flex;
            justify-content: flex-start;
            gap: 16px;
            .shop-do,
            .shop-img,
            .shop-name {
              display: inline-block;
              vertical-align: middle;
            }
            .shop-img {
              width: 32px;
              height: 32px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              border-radius: 4px;
            }
            .shop-do {
              width: 30px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
            }
            .shop-name {
              width: calc(100% - 62px);
              font-size: 18px;
            }
          }

          .good-list {
            width: 100%;
            .good-item {
              display: inline-flex;
              width: 100%;
              justify-content: flex-start;
              gap: 16px;
              padding-left: 16px;
              margin: 8px 0;
            }
            .good-do,
            .good-img,
            .good-info,
            .good-price-num {
              display: inline-block;
              vertical-align: middle;
            }
            .good-do {
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              .good-do-pos {
              }
            }
            .good-img {
              width: 120px;
              height: 120px;
            }
            .good-info {
              width: 200px;
              display: inline-flex;
              flex-direction: column;
              justify-content: center;
              .good-info-pos {
                font-size: 18px;
                color: $color-666;
              }
            }
            .good-price-num {
              width: calc(100% - 340px);
              display: inline-flex;
              justify-content: center;
              flex-direction: column;
              .good-price-num-pos {
                display: inline-flex;
                justify-content: space-around;
                gap: 16px;
                .price,
                .total {
                  font-size: 20px;
                }
                .price {
                  font-weight: 400;
                }
                .total {
                  color: $color-main;
                  font-weight: 700;
                }
                .total::before {
                  content: '￥';
                }
                .good-total {
                  color: $color-main;
                  font-size: 16px;
                }
              }
            }
          }
        }

        .content-item-bottom {
          position: absolute;
          left: 0;
          bottom: -8px;
          line-height: 48px;
          z-index: 2;
          width: 100%;
          display: inline-flex;
          justify-content: space-between;
          .num-total,
          .price-total {
            display: inline-block;
            vertical-align: middle;
            font-size: 20px;
            .cost-item {
              display: inline-block;
              margin: 0 16px;
              .coat-val::before {
                content: '￥';
              }
            }
          }
          .num-total {
            .num-val {
              color: $color-main;
              padding: 0 8px;
              font-size: 24px;
              font-weight: bold;
            }
          }
        }
      }
    }
  }
}
</style>
