  <template>
  <div>
    <q-card class="light-shadow q-pa-md bg-white rounded-borders q-mb-md" flat v-for="(x,index) in orders">
      <div class="row items-center desktop-only">
        <div class="q-mr-md">
          <q-btn color="cyan" unelevated disable size="sm">
            {{$t('v101.scatteredOrderList.shankedinfang')}}
          </q-btn>
        </div>
        <div class="f12 text-grey-6">{{ $t('hotelorder.search.orderNum') }}：{{ x.ThirdOrderNo }}</div>
        <div class="q-ml-md">
          <q-btn color="dark" flat size="sm" :label="$t('hotelorder.copy')" v-if="copyId != x.OrderID" @click="setCopyHandler(x)" />
          <q-btn color="cyan" icon="check" outline size="sm" :label="$t('hotelorder.copyed')" v-else></q-btn>
        </div>
        <div class="col text-center f12 text-grey-6">
          <!-- <span>{{ $t('v101.scatteredOrderList.table1') }}：{{ x.HotelName }}</span> -->
        </div>
        <div class="f12 text-grey-6">{{ $t('hotelorder.createTime') }}：{{ x.CreateDateStr }}</div>
        <div class="q-ml-md f12" :class="[x.typeInfo.Color]">
          <q-icon :name="x.typeInfo.Icon" />
          <span class="q-ml-sm">{{ x.typeInfo.StatusName }}</span>
        </div>
      </div>
      <div class="mobile-only">
        <div class="row justify-between">
          <div class="q-mr-md">
            <q-btn color="negative" unelevated disable size="sm" :label="$t('v102.vehicle.travelvehicle')"></q-btn>
          </div>
          <div class="q-ml-md f12" :class="[x.typeInfo.Color]">
            <q-icon :name="x.typeInfo.Icon" />
            <span class="q-ml-sm">{{ x.typeInfo.StatusName }}</span>
          </div>
        </div>
        <div class="bg-grey-2 rounded-borders q-pa-sm q-mt-md">
          <div class="row items-center justify-between">
            <div class="f12 text-grey-6">{{ $t('hotelorder.search.orderNum') }}：{{ x.ThirdOrderNo }}</div>
            <div class="q-ml-md">
              <q-btn color="dark" flat size="sm" :label="$t('hotelorder.copy')" v-if="copyId != x.OrderID" @click.stop="setCopyHandler(x)" />
              <q-btn color="cyan" outline size="sm" :label="$t('hotelorder.copyed')" v-else></q-btn>
            </div>
          </div>
          <div class="f12 text-grey-6">
            <span></span>
          </div>
          <div class="f12 text-grey-6">{{ $t('hotelorder.createTime') }}：{{ x.CreateTime }}</div>
        </div>
      </div>

      <div class="q-mt-md row">
        <q-table separator="cell" :hide-bottom="!x.CreateRemark" :pagination="{ rowsPerPage: 100 }" :rows="[x]" bordered :columns="cols" class="sticky-rightrowspan-column-table light-border col no-shadow">
          <template v-slot:body-cell-UseCompName="props">
            <q-th :props="props">
              <div>
                <div class="ellipsis fz13">{{ props.row.UseCompName }}</div>
                <div class="fz12 text-grey-6">{{ props.row.UseName }}</div>
                <q-tooltip>{{ props.row.UseCompName + props.row.UseName }}</q-tooltip>
              </div>
            </q-th>
          </template>
          <template v-slot:body-cell-PickUpInfo="props">
            <q-th :props="props">
              <div class="fz13 text-grey-6">
                <div>{{$t('v102.vehicle.title4')}}: <span class="text-dark q-pl-sm">{{ props.row.GoFlightInfo }}</span> </div>
                <div class="q-py-xs">{{$t('v102.vehicle.title5')}}: <span class="text-dark q-pl-sm">{{ props.row.BackFlightInfo }}</span> </div>
                <div>{{$t('v102.vehicle.title3')}}: <span class="text-dark q-pl-sm">{{ props.row.PickUpInfo }}</span></div>
              </div>
            </q-th>
          </template>
          <template v-slot:body-cell-UseDayNum="props">
            <q-td :props="props">
              <div class="room-item f12 text-grey-6" v-for="x in props.row.DetailList">
                <span class="q-mr-md">
                  <!-- {{ $t('v102.vehicle.tatle.th1') }}： -->
                  {{ x.UseDate }}</span>
                <span class="q-mr-md"> {{ $t('v102.vehicle.di') }}{{x.DayNum}}{{ $t('v102.vehicle.tian') }} </span>
                <span class="q-mr-md"> 
                  <!-- {{ $t('v102.vehicle.tatle.th3') }}: -->
                   {{ x.UseTypeName }} </span>
                <span class="q-mr-md">{{ $t('v102.vehicle.tatle.th4') }}: {{x.TripInfo}}</span>
                <span class="q-mr-md" v-if="x.UnitPrice>0">{{ $t('v102.vehicle.list2.s21') }}: {{x.UnitPrice}}</span>
                <span class="q-mr-md" v-if="x.StopBusPrice>0">{{ $t('v102.vehicle.list2.s22') }}: {{x.StopBusPrice}}</span>
                <span class="q-mr-md" v-if="x.RoomPrice>0">{{ $t('v102.vehicle.list2.s23') }}: {{x.RoomPrice}}</span>
                <span class="q-mr-md" v-if="x.Money>0"> {{ $t('hotelorder.xj') }}: {{ x.Money }} </span>
              </div>
            </q-td>
          </template>
          <template v-slot:body-cell-BusTypeName="props">
            <q-td :props="props">
              <div class="f12">
                <div><span class="q-mr-xs text-grey-6">{{ $t('v102.vehicle.title9') }}：</span>{{ x.BusTypeName }}</div>
                <template v-if="x.DriverInfo ">
                  <div><span class="q-mr-xs text-grey-6">{{ $t('v102.vehicle.list2.s30') }}：</span>{{ x.DriverInfo }}</div>
                  <div><span class="q-mr-xs text-grey-6">{{ $t('v102.vehicle.list2.s31') }}：</span>{{ x.BusNo }}</div>
                </template>
              </div>
              </q-td>
          </template>
          <template v-slot:body-cell-HandFittingFee="props">
            <q-td :props="props" :colspan="x.DetailList.length" v-if="props.rowIndex == 0" >
              
              <div class="q-mt-sm" v-if="x.OrderStatus!=2">
                <q-btn size="sm" dense outline color="primary" v-if="x.ContractUrl">
                  <a class="text-primary" style="text-decoration:none"
                    :href="x.ContractUrl">{{ $t('v101.downloadHands') }}</a>
                </q-btn>
                <span class="text-grey-500 f12">{{ $t('v101.noneHands') }}</span>
              </div>
              <template v-else>
                {{ moneyFormat(x.ShouPeiFee)?moneyFormat(x.ShouPeiFee):$t('v102.vehicle.nomoney') }}
              </template>
            </q-td>
          </template>
          
          <template v-slot:bottom>
            <div class="msl text-negative">{{ $t('v101.hotelRemark') }}：{{ x.CreateRemark }}</div>
          </template>
        </q-table>
      </div>

      <div class="q-mt-md" v-if="x.OrderStatus != 3" :class="{ row: $q.platform.is.desktop, 'column reverse': $q.platform.is.mobile }">
        <div class="pay row text-grey rounded-borders" :class="{ 'bg-grey-2 q-mt-md': $q.platform.is.mobile, col: $q.platform.is.desktop }">
        </div>
        <div class="col desktop-only"></div>
        <div class="rounded-borders bg-grey-2 q-pa-md"
          :class="{ 'q-mx-md': $q.platform.is.desktop, 'q-mt-md': $q.platform.is.mobile }">
          <div class="row items-center justify-between q-mb-sm q-pb-sm" style="border-bottom: 1px dashed #ddd">
            <div class="f12 text-grey">{{ $t('hotelorder.orderMoney') }}</div>
            <div class="dark q-ml-xl">
              <span class="din text-subtitle1">
                {{ moneyFormat(x.TotalPrice) }}
                <!-- {{ x.totalMoney.toFixed(2) }} -->
              </span>
              <span class="f12">
                {{$t('unit.jp')}}
              </span>
            </div>
          </div>
          <div class="text-right">
            <q-btn dense outline color="accent" size="sm" class="q-mt-sm" :label="$t('hotelorder.payinfo')" @click="ViewPayment"></q-btn>
          </div>
        </div>
        <div class="rounded-borders q-pa-sm justify-between" :class="{ 'column': $q.platform.is.desktop, 'row items-center': $q.platform.is.mobile, 'bg-green-1': x.OrderStatus != '已取消', 'bg-grey-2': x.OrderStatus == '已取消' }">
          <q-list dense :class="{ 'row items-center justify-between full-width': $q.platform.is.mobile }">
            <q-item v-if="x.OrderStatus != '已取消'" class="text-negative" clickable v-close-popup @click="canCelHotel(x.ThirdOrderNo)">
              <q-item-section>
                <q-item-label>{{ $t('hotelorder.opera.cancel') }}</q-item-label>
              </q-item-section>
            </q-item>
            <q-item class="text-positive" clickable v-close-popup @click="getDetail(x.OrderID)">
              <q-item-section>
                <q-item-label>{{ $t('v101.scatteredOrderList.xiangqing') }}</q-item-label>
              </q-item-section>
            </q-item>
          </q-list>
          <!-- <div v-else>
            <div class="f12 text-green-4">{{ $t('hotelorder.payed') }}</div>
            <div class="text-subtitle1 din text-green-14">
              {{ moneyFormat(x.totalMoney) }}
              <span class="text-green-4 f12">
                {{$t('unit.jp')}}
              </span>
            </div>
          </div> -->
        </div>
      </div>
      <div v-if="x.OrderStatus == 3">
        <div class="q-mt-md q-pa-sm row items-center rounded-borders bg-orange-1" style="border-width: 0 5px; border-style: solid; border-color: #ffb74d">
          <div class="text-subtitle2 text-weight-bolder text-grey-900">{{ $t('v101.cancelRemark') }}：</div>
          <div class="f12 text-grey-600 q-ml-sm col">{{ x.CancelRemark ? x.CancelRemark : $t('v101.selfCancle') }}</div>
          <div class="f12 text-grey-900">{{ x.CancelRemark }}
          </div>
        </div>
      </div>
    </q-card>
    <q-card class="light-shadow q-pa-md bg-white rounded-borders q-mb-md" v-if="pages.pageCount > 0" flat>
      <q-pagination class="full-width justify-end" v-model="pages.pageIndex" color="primary" :max="pages.pageCount" :input="true" @update:model-value="changePageHandler" />
    </q-card>
    <q-inner-loading :showing="loading" :label="$t('loading')" label-class="text-grey-6 f12" />
  </div>
  <div class="text-center q-mt-xl" v-if="(!orders || orders.length == 0) && !loading">
    <svg-icon :size="50" color="nav" icon="General/Clipboard.svg"></svg-icon>
    <div class="q-mt-md f12 text-grey-6">{{ $t('noneData') }}</div>
  </div>
  <q-dialog v-model="showOrderdDetail">
    <OrderdDetail :OrderID="OrderID"></OrderdDetail>
  </q-dialog>

  <q-dialog v-model="showBankAccount">
    <BankAccount @change="showBankAccount=false"></BankAccount>
  </q-dialog>

</template>

<script lang="ts">
import { DirtionmaryHelper } from '../../../config/dictionary'
import { defineComponent, inject, reactive, toRefs, watch } from 'vue'
import HotelService from '../../../api/scattered'
import { ApiResult } from '../../../@types/enumHelper'
import message from '../../../utils/message'
import { RoomType, StandardStatus } from '../../../@types'
import { getHotelOrderStatus, getHotelRoomType, moneyFormat } from '../../../utils/tools'
import { useI18n } from 'vue-i18n'
import svgIcon from '../../../global/svg-icon.vue'
import { copyToClipboard, useQuasar } from 'quasar'
import { currentRouter } from '../../../router'
import OrderdDetail from './OrderdDetail.vue'
import BankAccount from '../../BankAccount.vue'

export default defineComponent({
  components: { OrderdDetail, BankAccount },
  setup(props) {
    const search = inject(DirtionmaryHelper.HOTEL_ORDER_SEARCH) as any
    const { t } = useI18n()
    const $q = useQuasar()

    watch(search, (n, o) => {
      if (data.loading) return
      data.pages.pageIndex = 1
      data.orders = []
      methods.initOrders()
    })

    const data = reactive({
      copyId: 0,
      showBankAccount: false,
      OrderID: '',
      showOrderdDetail: false,
      orders: [] as Array<any>,
      status: [] as Array<StandardStatus>,
      rooms: [] as Array<RoomType>,
      pages: {
        pageIndex: 1,
        pageSize: 10,
        pageCount: 0
      },
      loadingCel: false,
      loading: false,
      cols: [
      { name: 'HotelName', label: t('v101.scatteredOrderList.table1'), field: (row: any) => row.HotelName, align: 'left' },
        { name: 'Number', label: t('v101.scatteredOrderList.table3'), field: (row: any) => row.Number, align: 'left' },
        { name: 'DiscountMoney', label: t('v101.scatteredOrderList.youhui'), field: (row: any) => `${row.DiscountMoney.toFixed(2)}`, align: 'left' },
        { name: 'CustomerPayMoney', label: t('v101.scatteredOrderList.table5'), field: (row: any) => row.CustomerPayMoney.toFixed(2), align: 'left' },
        { name: 'Money', label: t('hotelorder.col.m'), field: (row: any) => row.Money.toFixed(2), align: 'left' },
      ] as any,
      mobileCols: [
        { name: 'HotelName', label: t('v101.scatteredOrderList.table1'), align: 'left' },
        { name: 'OrderID', label: t('v101.scatteredOrderList.table2'), field: (row: any) => row.OrderID, align: 'left' },
        { name: 'CustomerPayMoney', label: t('v101.scatteredOrderList.table5'), field: (row: any) => row.CustomerPayMoney.toFixed(2), align: 'left' },
      ]
    })

    const methods = {
      ViewPayment() {
        data.showBankAccount = true
      },
      canCelHotel(ThirdOrderNo) {
        $q.dialog({
          title: t('dialog.titles.horder'),
          message: t('dialog.contents.horder'),
          cancel: true,
          persistent: true
        }).onOk(() => {
          methods.cancellations(ThirdOrderNo)
        })
      },
      cancellations(ThirdOrderNo){
        if(data.loadingCel) return
        let msg ={
            bookingNumber:ThirdOrderNo,
            CancelBy: search.CustomerId,
            CancelType:2
        }
        let param = Object.assign(msg)
        data.loadingCel = true
        HotelService.GetJAPANReservationCancallation(param)
          .then(r => {
            if (r.data.resultCode == ApiResult.SUCCESS) {
              message.successMsg(`${t('success')}`)
              methods.initOrders()
            } else {
              message.errorMsg(r.data.message)
            }
            data.loadingCel = false
          })
          .catch(e => {
            message.errorMsg(e.message)
            data.loadingCel = false
          })
      },
      getDetail(OrderID){
        data.showOrderdDetail = true
        data.OrderID = OrderID
      },
      initOrders() {
        let param = Object.assign(data.pages, search)
        data.loading = true
        HotelService.GetB2BJapanHotelOrderList(param)
          .then(r => {
            if (r.data.resultCode == ApiResult.SUCCESS) {
              r.data.data.pageData.forEach((x: any) => {
                x.typeInfo = data.status.find(y => y.StatusName == x.OrderStatus) ?? data.status[1]
                x.Money = 0
                x.Money += Number(x.CustomerPayMoney-x.DiscountMoney)
              })
              data.orders = r.data.data.pageData
              data.pages.pageCount = r.data.data.pageCount
            } else {
              message.errorMsg(r.data.message)
            }
            data.loading = false
          })
          .catch(e => {
            message.errorMsg(e.message)
            data.loading = false
          })
      },
      changePageHandler(n: any) {
        data.pages.pageIndex = n
        methods.initOrders()
      },
      init() {
        data.status = getHotelOrderStatus()
        data.rooms = getHotelRoomType()
        methods.initOrders()
      },
      setCopyHandler(order: any, ctx?: string) {
        if (ctx) {
          data.copyId = -1
          copyToClipboard(ctx)
        } else {
          data.copyId = order.OrderID
          copyToClipboard(order.ThirdOrderNo)
        }

        setTimeout(() => {
          data.copyId = 0
        }, 2000)
      }
    }

    methods.init()
    return {
      ...toRefs(data),
      ...methods,
      moneyFormat
    }
  }
})
</script>

<style>
.light-border table,
.light-border table td,
.light-border .q-table__middle,
.light-border.borders,
.light-border table th {
  border-color: #eee !important;
  white-space: wrap;
}
.light-border .room-item {
  height: 28px;
  line-height: 28px;
  border-bottom: 1px solid #eee;
}
.light-border .room-item:last-child {
  border-bottom: none;
}
.light-border .max-hotelname {
  width: 140px;
  height: 28px;
  line-height: 28px;
}
.orderListDialog-title{
  position: relative;
}
.orderListDialog-title span{
  font-size: 20px;
}
.orderListDialog-close{
  position: absolute;
  right: 0;
  top: -10px;
  font-size: 26px;
}
.orderListDialog-bg{
  background: #5098FF;
  border-radius: 10px;
}
.orderListDialog-img{
  background: url('../../../assets/images/wallet.png')no-repeat right #5098FF;
  background-size: 97px 100%;
}
</style>
