<template>
  <a-row style="margin-bottom: 20px;" justify="space-between">
    <a-col>
      <a-tooltip title="展开收起当前机场">
        <a-button type="primary" style="margin-right: 10px;" @click="onClickCollapsed">
          <MenuOutlined />
        </a-button>
      </a-tooltip>
      <a-button type="primary" @click="onClickCreateTask">
        新建机场任务
      </a-button>
    </a-col>
    <a-col>
      <a-form ref="formRef" layout="inline" :model="formModel" :colon="false">
        <a-form-item name="rangeDate">
          <a-range-picker style="width: 250px;" valueFormat="YYYY-MM-DD" :placeholder="['开始时间', '结束时间']"
            v-model:value="formModel.rangeDate" />
        </a-form-item>
        <a-form-item name="search_info">
          <a-input style="width: 200px;" placeholder="任务名称、航线名称" 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 { MenuOutlined, SearchOutlined, ReloadOutlined } from '@ant-design/icons-vue';
import moment from 'moment';

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

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

});

const formRef = ref();

const formModel = reactive({
  rangeDate: undefined,
  search_info: undefined,
})

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

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

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