<template>
  <el-form :inline="true" size="mini">
    <el-form-item
      :label="item.label"
      v-for="(item, index) in config"
      :key="index"
    >
      <el-input
        v-if="item.type == 'input'"
        v-model="searchForm[item.prop]"
        :placeholder="`请输入${item.placeholder || item.label}`"
        @keyup.enter.native="handleSearch"
        clearable
      ></el-input>
      <el-select
        v-if="item.type == 'select'"
        v-model="searchForm[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>
      <el-date-picker
        v-if="item.type == 'dateTimeRange'"
        v-model="searchForm[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-checkbox
        v-if="item.type == 'checkbox'"
        v-model="searchForm[item.prop]"
      ></el-checkbox>
    </el-form-item>
    <el-form-item style="margin-left: 26px">
      <el-button
        type="primary"
        icon="el-icon-search"
        @click.native="handleSearch"
        size="mini"
      >
        查询</el-button
      >
      <el-button
        type="primary"
        plain
        icon="el-icon-refresh"
        @click.native="handleReset"
        size="mini"
      >
        重置</el-button
      >
    </el-form-item>
  </el-form>
</template>

<script>
import { Debounce } from "@/utils/index";
export default {
  name: "SearchBar",
  props: {
    config: {
      type: Array,
      default: () => [],
    },
  },
  watch: {
    config: {
      handler: function (value) {
        if (value) {
          this.searchForm = this.getSearchFormInitialValue(value);
        }
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      searchForm: {},
      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: {
    // 获取searchForm初始值
    getSearchFormInitialValue(config) {
      let form = {};
      config.map((item) => {
        if (item.type == "checkbox") {
          form[item.prop] = false;
        } else {
          form[item.prop] = "";
        }
      });
      return JSON.parse(JSON.stringify(form));
    },
    handleSearch() {
      this.$emit("search", this.searchForm);
    },
    handleReset() {
      this.searchForm = this.getSearchFormInitialValue(this.config);
      this.$emit("reset");
    },
    handleSelectChange() {
      this.handleSearch();
    },
    handleInputChange: Debounce(function (e) {
      this.handleSearch();
    }, 600),
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-form-item {
  margin-bottom: 0;
}
</style>
