<template>
  <!-- <img src="@/assets/icon/asset/c2c.svg" class="c2c" alt="" /> -->
  <div class="buy-sell">
    <div class="buy-left">
      <div
        class="buy-btn pf500 fs14 fc999999"
        :class="buySellFlag == 1 ? 'buy-green' : ''"
        @click="buySellData(1)">
        购买
      </div>
      <div
        class="sell-btn pf500 fs14 fc999999"
        :class="buySellFlag == 2 ? 'buy-red' : ''"
        @click="buySellData(2)">
        出售
      </div>
    </div>
    <div class="buy-right">
      <div class="text pf400 fs14 fc333333">CNY</div>
      <img src="@/assets/icon/asset/bottom-arrow-black.svg" alt="" />
    </div>
  </div>
  <div class="filter-area">
    <div class="filter-left">
      <div class="left-coin">
        <img src="@/assets/icon/coin/bnb.svg" class="coin" alt="" />
        <div class="text pf400 fs14 fc333333">BTC</div>
        <img src="@/assets/icon/asset/bottom-arrow-black.svg" class="bottom-arrow" />
      </div>
      <div class="left-money" @click="amountFlag = true">
        <div class="text pf400 fs14 fc333333">金额</div>
        <img src="@/assets/icon/asset/bottom-arrow-black.svg" class="bottom-arrow" />
      </div>
      <div class="left-pay-way" @click="paymentWayFlag = true">
        <div class="text pf400 fs14 fc333333">支付方式</div>
        <img src="@/assets/icon/asset/bottom-arrow-black.svg" class="bottom-arrow" />
      </div>
    </div>
    <div class="filter-right" @click="filterFlag = true">
      <img src="@/assets/icon/asset/filter.svg" alt="" />
    </div>
  </div>
  <div class="goods-area">
    <div class="goods-item" v-for="(item, index) in OTCPendingOrderData" :key="index">
      <div class="item-merchant">
        <div class="merchant-left" @click="goMerchantDetails">
          <img src="@/assets/img/index/user/default-head.png" alt="" />
          <div class="left-name pf500 fs14 fc2C3131">{{ item.otc_name }}</div>
          <div class="vip-flag pf500 fs10 fcDF384C">{{ item.otc_level }}</div>
        </div>
        <div class="merchant-right pf400 fs10 fc999999">
          入驻时间: {{ item.otc_create_time }}
        </div>
      </div>
      <div class="item-chengjiao-number pf400 fs10 fc999999">
        成交量 {{ item.order_info.total }} · {{ item.order_info.rate }} 成交率
      </div>
      <div class="item-price">
        <div class="price-area">
          <div class="text pf400 fs10 fc999999">单价</div>
          <div class="price-number pf400 fs10 fc999999">
            ￥
            <span class="pf500 fs16 fc333333">{{ Number(item.price).toFixed(2) }}</span>
            /USDT
          </div>
          <div class="number pf400 fs10 fc999999">
            数量
            <span class="pf400 fs12 fc666666"
              >{{ Number(item.left_count).toFixed(2) }} USDT</span
            >
          </div>
          <div class="number pf400 fs10 fc999999">
            限额
            <span class="pf400 fs12 fc666666">
              {{ Number(item.min_limit).toFixed(2) }} -
              {{ Number(item.max_limit).toFixed(2) }} CNY</span
            >
          </div>
        </div>
        <div class="price-func">
          <div class="func-time pf400 fs10 fc999999">
            <img src="@/assets/icon/asset/clock-gray.svg" alt="" />
            {{ item.order_info.avg_time }}
          </div>
          <div class="func-pay-way pf400 fs10 fc999999">
            <div class="color"></div>
            <!-- 1.银行卡 2.支付宝 3.微信 -->
            <div v-if="item.status == 1">银行卡</div>
            <div v-if="item.status == 2">支付宝</div>
            <div v-if="item.status == 3">微信</div>
          </div>
          <div class="func-main">
            <div class="func-chat pf500 fs12 fcDF384C" @click="getChatIdData(item.otc)">
              聊天
            </div>
            <div
              class="func-buy pf500 fs12 fcFFFFFF"
              @click="OTCBuy(item.id, item.otc)"
              v-if="buySellFlag == 1">
              购买
            </div>
            <div
              class="func-sell pf500 fs12 fcFFFFFF"
              @click="OTCSell(item)"
              v-if="buySellFlag == 2">
              出售
            </div>
          </div>
        </div>
      </div>
    </div>
    <PaymentWay v-show="paymentWayFlag" @paymentWayClose="paymentWayClose"></PaymentWay>
    <Amount v-show="amountFlag" @amountClose="amountClose"></Amount>
    <Filter v-show="filterFlag" @filterClose="filterClose"></Filter>
    <!-- 购买弹窗 -->
    <C2CBuy
      v-show="BuyFlag"
      :otcId="otcId"
      :chatId="chatId"
      :orderId="orderId"
      @BuyClose="BuyClose"></C2CBuy>
    <!-- 出售弹窗 -->
    <C2CSell
      v-show="sellFlag"
      :otcId="otcId"
      :orderId="orderId"
      @sellClose="sellClose"></C2CSell>
    <!-- 暂无收款方式，还没点击的地方 -->
    <NotPaymentWay
      v-show="notPaymnetWayFlag"
      @notPaymentWayClose="notPaymentWayClose"></NotPaymentWay>
    <!-- 添加收款方式，还没点击的地方 -->
    <PaymentAccount
      v-show="paymentAccountFlag"
      @paymentAccountClose="paymentAccountClose"></PaymentAccount>
    <!-- 取消订单，还没点击的地方 -->
    <CancelOrder
      v-show="cancelOrderFlag"
      @cancelOrderClose="cancelOrderClose"></CancelOrder>
    <!-- 是否完成付款，还没点击的地方 -->
    <CompletePayment
      v-show="completePaymentFlag"
      @completePaymentClose="completePaymentClose"></CompletePayment>
  </div>
</template>
<script setup>
  import { ref, onMounted } from "vue";
  import { useRoute, useRouter } from "vue-router";
  import { GetOTCPendingOrder, GetChatId } from "@/api/otc";
  import PaymentWay from "../../dialog/PaymentWay.vue";
  import Amount from "../../dialog/Amount.vue";
  import Filter from "../../dialog/Filter.vue";
  import C2CSell from "../../dialog/C2CSell.vue";
  import C2CBuy from "../../dialog/C2CBuy.vue";
  import NotPaymentWay from "../../dialog/NotPaymentWay.vue";
  import PaymentAccount from "../../dialog/PaymentAccount.vue";
  import CancelOrder from "../../dialog/CancelOrder.vue";
  import CompletePayment from "../../dialog/CompletePayment.vue";

  const router = useRouter();

  const otcId = ref();
  const chatId = ref();
  const orderId = ref();
  const buySellFlag = ref(1);
  const orderData = ref({
    // 1购买 2出售
    order_type: "1",
  });

  const buySellData = (flag) => {
    buySellFlag.value = flag;
    orderData.value.order_type = flag;
    GetOTCPendingOrderData();
  };

  // item.id 6, item.otc 1
  const OTCBuy = async (id, otc) => {
    otcId.value = otc;
    orderId.value = id;

    const params = {
      otc_id: otc,
    };
    const data = await GetChatId(params);
    if (data) {
      chatId.value = data;
      BuyFlag.value = true;
    }
  };

  const OTCSell = (item) => {
    otcId.value = item.otc;
    orderId.value = item.id;
    console.log(3, orderId.value);
    sellFlag.value = true;
    // console.log(1, item);
  };

  const getChatIdData = async (otc) => {
    const params = {
      otc_id: otc,
    };
    const data = await GetChatId(params);
    if (data) {
      chatId.value = data;

      router.push({
        name: "OTCCustomService",
        params: { chatId: chatId.value, otcId: otc },
      });
    }
  };

  const goMerchantDetails = () => {
    router.push("/OTCMerchantDetails");
  };

  const OTCPendingOrderData = ref();
  const GetOTCPendingOrderData = async () => {
    OTCPendingOrderData.value = [];
    const params = {
      // 1购买，2出售
      order_type: orderData.value.order_type,
      // 1,普通交易，2快捷交易，3.大宗交易
      trans_type: "3",
      coin_type: "",
      price: "",
      count: "",
      min_limit: "",
      min_amount: "",
      pay_type: "",
      left_count: "",
    };
    const data = await GetOTCPendingOrder(params);
    if (data) {
      OTCPendingOrderData.value = data.list;
    }
  };

  onMounted(() => {
    GetOTCPendingOrderData();
  });

  const completePaymentFlag = ref(false);
  const completePaymentClose = () => {
    completePaymentFlag.value = false;
  };

  const cancelOrderFlag = ref(false);
  const cancelOrderClose = () => {
    cancelOrderFlag.value = false;
  };

  const paymentAccountFlag = ref(false);
  const paymentAccountClose = () => {
    paymentAccountFlag.value = false;
  };

  const notPaymnetWayFlag = ref(false);
  const notPaymentWayClose = () => {
    notPaymnetWayFlag.value = false;
  };

  const BuyFlag = ref(false);
  const BuyClose = () => {
    BuyFlag.value = false;
  };

  const sellFlag = ref(false);
  const sellClose = () => {
    sellFlag.value = false;
  };

  const filterFlag = ref(false);
  const filterClose = () => {
    filterFlag.value = false;
  };

  const amountFlag = ref(false);
  const amountClose = () => {
    amountFlag.value = false;
  };

  const paymentWayFlag = ref(false);
  const paymentWayClose = () => {
    paymentWayFlag.value = false;
  };
</script>
<style lang="less" scoped>
  .c2c {
    margin-top: 13px;
    width: 345px;
    height: 100px;
  }

  .buy-sell {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 13px;
    width: 345px;
    height: 35px;

    .buy-left {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      height: 100%;

      .buy-green {
        background: #45b26b !important;
        color: #ffffff;
        transition: all ease 0.3s;
      }

      .buy-red {
        background: #df384c !important;
        color: #ffffff;
        transition: all ease 0.3s;
      }

      .buy-btn {
        width: 85px;
        height: 35px;
        line-height: 35px;
        text-align: center;
        border-radius: 6px;
        background: #f5f5f5;
      }

      .sell-btn {
        margin-left: 14px;
        width: 85px;
        height: 35px;
        line-height: 35px;
        text-align: center;
        border-radius: 6px;
        background: #f5f5f5;
      }
    }

    .buy-right {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
      width: 75px;
      height: 35px;
      line-height: 35px;
      text-align: center;
      border-radius: 6px;
      background: #f5f5f5;

      .text {
        margin-left: 15px;
      }

      img {
        margin-left: 12px;
      }
    }
  }

  .filter-area {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    padding-left: 6px;
    margin-top: 13px;
    width: 345px;
    height: 18px;
    box-sizing: border-box;

    .filter-left {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;
      height: 18px;

      .left-coin {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
        height: 18px;

        .coin {
          width: 18px;
          height: 18px;
        }

        .text {
          margin-left: 3px;
        }

        .bottom-arrow {
          margin-left: 5px;
        }
      }

      .left-money {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
        margin-left: 34px;
        height: 18px;

        .text {
          margin-left: 3px;
        }

        .bottom-arrow {
          margin-left: 5px;
        }
      }

      .left-pay-way {
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        align-items: center;
        margin-left: 34px;
        height: 18px;

        .text {
          margin-left: 3px;
        }

        .bottom-arrow {
          margin-left: 5px;
        }
      }
    }

    .filter-right {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      height: 18px;
    }
  }

  .goods-area {
    margin-top: 10px;
    margin-bottom: 30px;
    width: 345px;

    .goods-item {
      padding: 0 10px;
      margin-top: 10px;
      width: 345px;
      height: 150px;
      border-radius: 8px;
      border: 1px solid #ebebeb;
      box-sizing: border-box;

      .item-merchant {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        margin-top: 15px;
        width: 100%;
        height: 24px;

        .merchant-left {
          display: flex;
          flex-direction: row;
          justify-content: flex-start;
          align-items: center;
          height: 24px;

          img {
            width: 24px;
            height: 24px;
          }

          .left-name {
            margin-left: 4px;
          }

          .vip-flag {
            margin-left: 4px;
            width: 18px;
            height: 15px;
            line-height: 15px;
            text-align: center;
            border-radius: 3px;
            background: #df384c1a;
          }
        }
      }

      .item-chengjiao-number {
        margin-top: 6px;
      }

      .item-price {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        margin-top: 10px;

        .price-area {
          .price-number {
            margin-top: 3px;
          }

          .number {
            margin-top: 5px;
            height: 13px;
            line-height: 13px;

            span {
              margin-left: 1px;
            }
          }
        }

        .price-func {
          display: flex;
          flex-direction: column;
          justify-content: flex-start;
          align-items: flex-end;

          .func-time {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
            height: 12px;

            img {
              margin-right: 3px;
              width: 9px;
              height: 9px;
            }
          }

          .func-pay-way {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            align-items: center;
            margin-top: 5px;
            height: 12px;

            .color {
              margin-right: 3px;
              width: 3px;
              height: 10px;
              border-radius: 2px;
              background: #df384c;
            }
          }

          .func-main {
            display: flex;
            flex-direction: row;
            justify-content: flex-start;
            margin-top: 15px;
            height: 24px;

            .func-chat {
              width: 58px;
              height: 24px;
              line-height: 24px;
              text-align: center;
              border-radius: 5px;
              background: #df384c1a;
            }

            .func-buy {
              margin-left: 8px;
              width: 58px;
              height: 24px;
              line-height: 24px;
              text-align: center;
              border-radius: 5px;
              background: #45b26b;
            }

            .func-sell {
              margin-left: 8px;
              width: 58px;
              height: 24px;
              line-height: 24px;
              text-align: center;
              border-radius: 5px;
              background: #df384c;
            }
          }
        }
      }
    }
  }
</style>
