<template>
  <div id="c-search">
    <el-form :inline="true" v-if="fields.length > 0">
      <el-row>
        <el-col :span="6" v-for="i in fields" :key="i.model">
          <el-form-item :label="i.label">
            <template v-if="i.type === 'select'">
              <el-select v-model="form[i.model]" filterable clearable style="width: 100%">
                <slot :name="i.model"></slot>
              </el-select>
            </template>
            <template v-else>
              <el-input v-model="form[i.model]" :placeholder="`输入${i.label}`"></el-input>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button size="mini" type="primary" @click="toSearch">查询</el-button>
          <el-button size="mini" @click="toReset">重置</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
const _ = require('lodash');
export default {
  name: 'c-search',
  props: {
    form: { type: Object, default: () => ({}) },
    fields: { type: Array, default: () => [] },
  },
  model: {
    prop: 'form',
    event: 'change',
  },
  data: function () {
    return {};
  },
  methods: {
    toSearch() {
      this.$emit('query');
    },
    toReset() {
      this.$emit('change', {});
      this.$emit('query');
    },
  },
};
</script>

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