<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
        @input="handleInputChange"
      ></el-input>
      <el-select
        v-if="item.type == 'select'"
        v-model="searchData[item.prop]"
        :placeholder="`请选择${item.placeholder || item.label}`"
        @change="handleSelectChange"
        clearable
      >
        <el-option
          :label="option.label"
          :value="option.value"
          v-for="option in item.selectOptions"
          :key="option.value"
        ></el-option>
      </el-select>
    </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: {},
    };
  },
  methods: {
    handleSearch() {
      this.$emit("search", this.searchData);
    },
    handleReset() {
      this.searchData = {};
      this.$emit("reset");
    },
    handleSelectChange() {
      this.handleSearch();
    },
    handleInputChange: Debounce(function(e) {
      this.handleSearch();
    }, 500),
  },
};
</script>

<style>
</style>