<template>
  <a-row style="margin-bottom: 20px;" justify="end">
    <a-col>
      <a-form ref="formRef" layout="inline" :model="formModel" :colon="false">
        <a-form-item name="date">
          <a-range-picker style="width: 250px;" v-model:value="formModel.date" />
        </a-form-item>
        <a-form-item name="status">
          <a-select style="width: 200px;" placeholder="上传状态" v-model:value="formModel.status">
            <a-select-option :value="1">
              上传中
            </a-select-option>
            <a-select-option :value="2">
              成功
            </a-select-option>
            <a-select-option :value="3">
              取消
            </a-select-option>
            <a-select-option :value="4">
              失败
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item name="username">
          <a-input style="width: 200px;" placeholder="反馈人" v-model:value="formModel.username" />
        </a-form-item>
        <a-form-item name="search_info">
          <a-input style="width: 200px;" placeholder="设备SN、设备异常描述" v-model:value="formModel.search_info" />
        </a-form-item>
        <a-form-item>
          <a-button style="margin-right: 10px;" @click="handleClickSearch">
            <template #icon>
              <SearchOutlined />
            </template>
          </a-button>
          <a-button @click="handleClickReset">
            <template #icon>
              <ReloadOutlined />
            </template>
          </a-button>
        </a-form-item>
      </a-form>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { SearchOutlined, ReloadOutlined } from '@ant-design/icons-vue';
import moment from 'moment';

interface Props {
  onClickSearch: (query: any) => Promise<any>,
  onClickReset: (query: any) => Promise<any>,
};

const props = withDefaults(defineProps<Props>(), {

});

const formRef = ref();

const formModel = reactive({
  date: [],
  status: undefined,
  username: undefined,
  search_info: undefined,
})

// 点击查询
const handleClickSearch = async () => {
  const values = formRef.value?.getFieldsValue();
  const data = { ...values };
  delete data.date;
  if (values.date.length === 2) {
    data.begin_time = moment(values.date[0]).valueOf();
    data.end_time = moment(values.date[1]).valueOf();
  }
  await props.onClickSearch(data);
}

// 点击重置
const handleClickReset = async () => {
  formRef.value?.resetFields();
  const values = formRef.value?.getFieldsValue();
  const data = { ...values };
  delete data.date;
  await props.onClickReset(data);
}
</script>

<style lang="scss" scoped></style>