<template>
  <!-- 订单中心 - 搜索弹窗 -->
  <div class="orderSearch flex-col">
    <van-popup v-model:show="sheetShow" round class="orderSearchPopup">
      <div class="content">
        <van-form @submit="onSubmit">
          <van-field v-model="userName" name="userName" :label="$t('orderCenter.userNameLabel')"
            :placeholder="$t('orderCenter.userNamePlaceholder')" />
          <van-field v-model="sn" name="sn" :label="$t('orderCenter.orderNo')"
            :placeholder="$t('orderCenter.orderNoPlaceholder')" />
          <van-field v-model="trxNo" name="trxNo" :label="$t('orderCenter.orderSerialNumberLabel')"
            :placeholder="$t('orderCenter.orderSerialNumberPlaceholder')" />
          <van-field v-model="clientId" name="clientId" :label="$t('orderCenter.equipmentNo')"
            :placeholder="$t('orderCenter.equipmentNoPlaceholder')" />

          <div class="van-cell van-field">
            <div class="van-cell__title van-field__label">
              <label id="van-field-4-label" for="van-field-4-input">{{ $t('orderCenter.orderDate') }}</label>
            </div>
            <div class="van-cell__value van-field__value">
              <div class="van-field__body">
                <input type="text" class="van-field__control" :placeholder="$t('orderCenter.startTime')"
                  style="text-align: center; width: 50%;" v-model="startTime" @click="startTimeClick()" />
                <span>{{ $t('orderCenter.to') }}</span>
                <input type="text" class="van-field__control" :placeholder="$t('orderCenter.endTime')"
                  style="text-align: center; width: 50%;" v-model="endTime" @click="endTimeClick()" />
              </div>
            </div>
          </div>

          <!-- 操作 -->
          <van-row justify="space-around" style="padding: 1em;">
            <van-button span="5" round type="primary" style="height: 2em; padding: 0 2em;" native-type="submit">
              {{ $t('orderCenter.clickSearch') }}
            </van-button>
          </van-row>
        </van-form>
      </div>
    </van-popup>
    <van-popup v-model:show="startDateShow" position="bottom">
      <van-date-picker :title="$t('orderCenter.selectFullTime')" @confirm="startDateConFirm($event)"
        @cancel="startDateCancel()" :min-date="minDate" :max-date="maxDate" />
    </van-popup>
    <van-popup v-model:show="endDateShow" position="bottom">
      <van-date-picker :title="$t('orderCenter.selectFullTime')" @confirm="endDateConFirm($event)"
        @cancel="endDateCancel()" :min-date="minDate" :max-date="maxDate" />
    </van-popup>
  </div>
</template>

<script>
import { ref } from 'vue';
// import dateUtil from "@/utils/dateUtil";
import { showToast } from 'vant';
export default {
  name: 'orderSearch',
  setup(prop, context) {
    const sheetShow = ref(false);
    const startDateShow = ref(false);
    const endDateShow = ref(false);
    const minDate = new Date(2018, 1, 1);
    const maxDate = new Date();

    const userName = ref(''); // 用户名
    const sn = ref(''); // 订单编号
    const trxNo = ref(''); // 订单流水号
    const clientId = ref(''); // 设备唯一码_设备编号
    const startDate = ref(''); // 订单时间-起始时间
    const endDate = ref('');
    const startTime = ref('');
    const endTime = ref('');
    // 父组件页面触发展示及初始化
    const showSearch = () => { sheetShow.value = true; };
    const startTimeClick = () => {
      startDateShow.value = true;
    }
    const startDateConFirm = ({ selectedValues }) => {
      startDate.value = selectedValues.join("-") + " 00:00:00";
      startDateShow.value = false;
      startTime.value = selectedValues.join("-");
    }
    const startDateCancel = () => {
      startDateShow.value = false;
    }
    const endTimeClick = () => {
      endDateShow.value = true;
    }
    const endDateConFirm = ({ selectedValues }) => {
      endDate.value = selectedValues.join("-") + " 23:59:59";
      endDateShow.value = false;
      endTime.value = selectedValues.join("-");
    }
    const endDateCancel = () => {
      endDateShow.value = false;
    }
    // 提交搜索表单触发搜索
    const onSubmit = () => {
      console.log('startTime.value', startTime.value)
      if (userName.value == '' && sn.value == '' && trxNo.value == '' && clientId.value == '') {
        // if (startTime.value != '' || endTime.value != '') {
        //   Toast("请输入正确时间");
        //   return;
        // } else {
          showToast("请输入搜索条件");
        return;
        // }
      } else {
        if (startTime.value == '' || endTime.value == '') {
          showToast("请输入指定订单日期");
          return;
        }
      }
      const searchParam = {
        userName: userName.value,
        sn: sn.value,
        trxNo: trxNo.value,
        clientId: clientId.value,
        startDate: startDate.value,
        endDate: endDate.value,
      };
      context.emit('search', searchParam);
      sheetShow.value = false;
    }

    return {
      sheetShow,
      startDateShow,
      startDateConFirm,
      startDateCancel,
      endDateShow,
      endDateConFirm,
      endDateCancel,
      minDate,

      userName,
      sn,
      trxNo,
      clientId,
      startDate,
      endDate,

      showSearch,
      startTimeClick,
      endTimeClick,
      onSubmit,
      startTime,
      endTime,
      maxDate,
    };
  },
  components: {},
};
</script>

<style lang="less" scoped>
.orderSearch {
  width: 100%;

  :deep(.orderSearchPopup) {
    width: 90%;
  }

  .content {
    padding-top: 1em;
  }

  .field .van-field__label {
    width: auto;
  }
}
</style>
