<template>
  <el-form :inline="true">
    <el-form-item
      :label="item.label"
      v-for="(item, index) in config"
      :key="index"
    >
      <el-input
        v-if="item.type == 'input'"
        v-model="searchData[item.prop]"
        :placeholder="`请输入${item.placeholder || item.label}`"
        @keyup.enter.native="handleSearch"
        clearable
      ></el-input>
      <!-- @input="handleInputChange" -->
      <el-select
        v-if="item.type == 'select'"
        v-model="searchData[item.prop]"
        :placeholder="`请选择${item.placeholder || item.label}`"
        clearable
        filterable
        allow-create
      >
        <el-option
          :label="option.label"
          :value="option.value"
          v-for="option in item.selectOptions"
          :key="option.value"
         
        ></el-option>
      </el-select>
      <!-- @change="handleSelectChange" -->
      <el-date-picker
        v-if="item.type == 'dateTimeRange'"
        v-model="searchData[item.prop]"
        type="datetimerange"
        align="right"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :default-time="['00:00:00', '23:59:59']"
        :picker-options="pickerOptions"
        :value-format="'yyyy-MM-dd HH:mm:ss'"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click.native="handleSearch"
      >
        查询</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-refresh"
        @click.native="handleReset"
      >
        重置</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { Debounce } from "@/utils/index";
export default {
  name: "SearchBar",
  props: {
    config: {
      type: Array,
      default: () => [],
    },

    /**
     * 
     * form:{
          name:'',
          status:''
        }
      
       config: [
        {
          prop: "name",
          type: "input",
          label: "展览名称",
        },
        {
          prop: "status",
          type: "select",
          label: "状态",
          selectOptions: [
            {
              label: "在线",
              value: "true",
            },
            {
              label: "离线",
              value: "false",
            },
          ],
        },
      ],
     * 
     * 
     */
  },
  watch: {
    config: {
      handler: function (value) {
        if (value) {
          var obj = {};
          this.config.map((item) => {
            obj[item.prop] = "";
          });
          this.searchData = JSON.parse(JSON.stringify(obj));
        }
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      searchData: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  methods: {
    handleSearch() {
      this.$emit("search", this.searchData);
    },
    handleReset() {
      this.searchData = {};
      this.$emit("reset");
    },
    handleSelectChange() {
      this.handleSearch();
    },
    handleInputChange: Debounce(function (e) {
      this.handleSearch();
    }, 600),
  },
};
</script>

<style>
</style>