<template>
  <uni-popup ref="popup" type="top">
    <view class="switch-filter">
      <view class="condition">查询条件</view>

      <view class="item" v-if="orderState == 7">
        <text class="title">时间类型：</text>
        <picker
          class="value"
          mode="selector"
          :value="isDateFinishSearch"
          :range="dateFinish"
          range-key="name"
          @change="pickerChange1"
          >{{ dateFinish[isDateFinishSearch].name }}</picker
        >
      </view>
      <view class="item">
        <text class="title">查询时间：</text>
        <view class="root">
          <picker
            class="value"
            mode="date"
            :value="date1"
            start="2010-01-01"
            :end="endDate"
            @change="dateChange1"
            >{{ date1 }}</picker
          >
          <text>至</text>
          <picker
            class="value"
            mode="date"
            :value="date2"
            start="2010-01-01"
            :end="endDate"
            @change="dateChange2"
            >{{ date2 }}</picker
          >
        </view>
      </view>
      <view class="item">
        <text class="title">外卖平台：</text>
        <picker
          class="value"
          mode="selector"
          :value="vendor"
          :range="vendorArr"
          range-key="name"
          @change="dateChange3"
          >{{ vendorArr[vendor].name }}</picker
        >
      </view>
      <view class="item" v-if="orderState == 7">
        <text class="title">配送方式：</text>
        <picker
          class="value"
          mode="selector"
          :value="waybillVendor"
          :range="waybillVendorArr"
          range-key="name"
          @change="dateChange4"
          >{{ waybillVendorArr[waybillVendor].name }}</picker
        >
      </view>
      <view class="item" v-if="orderState == 6">
        <text class="title">售后方式：</text>
        <picker
          class="value"
          mode="selector"
          :value="appealTypes1"
          :range="appealTypesArr"
          range-key="name"
          @change="dateChange5"
          >{{ appealTypesArr[appealTypes1].name }}</picker
        >
      </view>
      <view class="item" v-if="orderState == 6">
        <text class="title">售后状态：</text>
        <picker
          class="value"
          mode="selector"
          :value="afsStatus1"
          :range="afsStatusArr"
          range-key="name"
          @change="dateChange6"
          >{{ afsStatusArr[afsStatus1].name }}</picker
        >
      </view>
      <view class="btnRoot">
        <text class="cancel" @tap="popup.close()">取消</text>
        <text class="confirm" @tap="confirm">确定</text>
      </view>
    </view>
  </uni-popup>
</template>

<script lang="ts" setup>
import { timeFormatD } from '@/utils/tools'
import { ref,watch } from 'vue'

/**
 * 筛选条件实例
 */
const popup = ref<any>(null)

interface propsType {
  orderState: number,
  orderStateInfo:AnyObject
}
const props = defineProps<propsType>()

/**
 * 创建时间
 */
const isDateFinishSearch = ref<number>(0)
type dateFinishType = {
  key: number
  name: string
}
const dateFinish = ref<Array<dateFinishType>>([
  { key: 0, name: '创建时间' },
  { key: 1, name: '完成时间' },
])
function pickerChange1(e: AnyObject) {
  isDateFinishSearch.value = +e.detail.value
}

/**
 * 起始时间,结束时间
 */
 const date1 = ref<string>(
  timeFormatD(+new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7))
  // timeFormatD(+new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 2))
)
watch(
  () => props.orderStateInfo,
  (val) => {
    if (val && val.id === 6 && val.count) {
      afsStatus1.value = 1
      date1.value = timeFormatD(+new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 2))
    }else {
      afsStatus1.value = 0
      date1.value = timeFormatD(+new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 7))
    }
  }
)

const date2 = ref<string>(timeFormatD(+new Date()))
const endDate = ref<string>('')
function dateChange1(e: AnyObject) {
  date1.value = e.detail.value
}
function dateChange2(e: AnyObject) {
  date2.value = e.detail.value
}

/**
 * 外卖平台
 */
const vendor = ref<number>(0)
type vendorArrType = {
  id: number
  name: string
}
const vendorArr = ref<Array<vendorArrType>>([
  { id: -1, name: '全部' },
  { id: 0, name: '京东到家' },
  { id: 1, name: '美团外卖' },
  { id: 3, name: '淘宝闪购新零售' },
  { id: 5, name: '京东商城' },
  { id: 9, name: '京西商城' },
])
function dateChange3(e: AnyObject) {
  vendor.value = e.detail.value
}

/**
 * 配送方式
 */
const waybillVendor = ref<number>(0)
const waybillVendorArr = ref<Array<vendorArrType>>([
  { id: -1, name: '全部' },
  { id: 0, name: '京东到家专送' },
  { id: 1, name: '美团专送' },
  { id: 3, name: '淘宝闪购专送' },
  { id: 101, name: '达达众包' },
  { id: 102, name: '美团配送' },
  { id: 103, name: '蜂鸟配送' },
  { id: 105, name: 'UU跑腿' },
  { id: 106, name: '顺丰同城' },
])
function dateChange4(e: AnyObject) {
  waybillVendor.value = e.detail.value
}

/**
 * 售后方式
 */
const appealTypes1 = ref<number>(0)
const appealTypesArr = ref<Array<vendorArrType>>([
  { id: -1, name: '全部' },
  { id: 1, name: '仅退款' },
  { id: 2, name: '退货退款' },
  { id: 3, name: '重发商品' },
])
function dateChange5(e: AnyObject) {
  appealTypes1.value = e.detail.value
}

/**
 * 售后状态
 */
// const afsStatus1 = ref<number>(0)
const afsStatus1 = ref<number>(1)
const afsStatusArr = ref<Array<vendorArrType>>([
  { id: -1, name: '全部' },
  { id: 155, name: '待审核' },
  { id: 160, name: '已审核' },
  { id: 165, name: '退货待确认' },
  { id: 167, name: '退货已收到' },
  { id: 180, name: '售后成功' },
  { id: 190, name: '售后失败' },
])
function dateChange6(e: AnyObject) {
  afsStatus1.value = e.detail.value
}

/**
 * 确定筛选条件
 */
type emitType = {
  fromTime: string
  toTime: string
  vendorID: number | string
  waybillVendorID?: number | string
  isDateFinish?: boolean
  appealTypes?: number | string
  afsStatus?: number | string
}
const emit = defineEmits<{
  (e: 'filterData', data: emitType): void
}>()
function confirm() {
  let data = {
    fromTime: date1.value,
    toTime: date2.value,
    vendorID: vendorArr.value[vendor.value].id,
    waybillVendorID: waybillVendorArr.value[waybillVendor.value].id,
    isDateFinish: Boolean(isDateFinishSearch.value),
    appealTypes: appealTypesArr.value[appealTypes1.value].id,
    afsStatus: afsStatusArr.value[afsStatus1.value].id,
  }
  emit('filterData', data)
  popup.value.close()
}

/**
 * 打开弹窗
 */
function openRef() {
  popup.value.open()
}
/**
 * 导出外部访问函数
 */
defineExpose({
  openRef,
})
</script>

<style lang="scss" scoped>
@import './order-filter.scss';
</style>