<template>
  <div id="searchBox">
    <div class="inputListBox">
      <el-form :inline="true" :model="formData" class="demo-form-inline">
        <el-form-item
          v-for="(item, index) in formList"
          :key="index"
          :label="item.label"
        >
          <el-select
            v-if="item.scope === 'select'"
            v-model="formData[item.prop]"
            :placeholder="item.placeholder"
            :size="size"
          >
            <el-option
              v-for="(items, indexs) in item.options"
              :key="indexs"
              :label="items.label"
              :value="items.value"
            ></el-option>
          </el-select>
          <div v-else-if="item.scope === 'numList'">
            <el-input-number
            :controls="false"
            v-model="formData[item.prop1]"
            controls-position="right"
            :min="0"
            :max="100"
            size="small"
             @change="handleChanges(formData[item.prop1],formData[item.prop2])"
          ></el-input-number>~
          <el-input-number
          :controls="false"
            v-model="formData[item.prop2]"
            controls-position="right"
            :min="0"
            :max="100"
            size="small"
            @change="handleChanges(formData[item.prop1],formData[item.prop2])"
          ></el-input-number>
          </div>
          <el-input-number
            v-else-if="item.scope === 'inputNumber'"
            v-model="formData[item.prop]"
            controls-position="right"
            :min="0"
            :max="100"
            size="small"
          ></el-input-number>
          <el-cascader
            v-else-if="item.scope === 'cascader'"
            v-model="formData[item.prop]"
            :options="item.options"
            :size="size"
            :props="{
              label: item.props.label,
              value: item.props.value,
              checkStrictly: true,
              emitPath: false,
            }"
          ></el-cascader>
          <el-date-picker
            v-else-if="item.scope === 'datePicker'"
            v-model="formData[item.prop]"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            :size="size"
            value-format="timestamp"
          >
          </el-date-picker>
          <el-date-picker
            v-else-if="item.scope === 'datePickerA'"
            v-model="formData[item.prop]"
            type="datetime"
            :size="size"
            placeholder="选择日期时间"
            value-format="timestamp"
          >
          </el-date-picker>
          <el-input
            v-else
            v-model="formData[item.prop]"
            :placeholder="item.placeholder"
            :size="size"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-form :inline="true" class="btnListBox">
      <el-form-item>
        <el-button :size="size" type="primary" @click="search">查询</el-button>
        <el-button :size="size" @click="init">重置</el-button>
      </el-form-item></el-form
    >
  </div>
</template>

<script>
export default {
  props: ["formList"], //参考文档 component.md
  data() {
    return {
      size: "medium", //输入框尺寸类型
      formData: {}, //表单数据收集
      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]);
            },
          },
        ],
      },
    };
  },
  created(){
    var self = this
    document.onkeydown = function (e) {
      var key = window.event.keyCode;
      if (key === 13) {
        self.search();
      }
    };
  },
  methods: {
    //搜索
    search() {
      this.$emit("search", this.formData);
    },
    //重置
    init() {
      this.formData = {};
      this.$emit("init");
    },
    handleChanges(int1,int2){
      if(int1 !== undefined && int2 !== undefined){
        if(int1 > int2){
          this.$message.error('请规范输入取值范围')
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
#searchBox {
  display: flex;
  .inputListBox {
    flex: 1;
  }
}
</style>
