<template>
  <div class="from-content" style="width: 100%">
    <ElPopover
      ref="PopoverRef"
      placement="bottom"
      popper-class="global-input-search-popover"
      :popperOptions="popperOptions"
      :width="this.width"
      trigger="manual"
      v-model="visible"
      @show="handleShow"
      style="width: 100%"
    >
      <div
        class="InputSearch input"
        v-if="params && types == 'input'"
        slot="reference"
        ref="ReferenceInputRef"
      >
        <ElInput
          style="width: 100%"
          v-model="value"
          :placeholder="placeholder"
          @change="handleSearch"
        ></ElInput>
        <img src="./search.svg" @click="visible = !visible" />
      </div>
      <div
        class="InputSearch"
        v-else
        slot="reference"
        ref="ReferenceInputRef"
        @click="visible = !visible"
      >
        <span>{{ placeholder }}</span>
        <img src="./search.svg" />
      </div>

      <div style="width: 100%; max-height: 360px" v-show="visible">
        <slot>
          <Form
            v-if="formList && formList.length"
            class="form"
            ref="FormConfigRef"
            :formList="formList"
            v-bind="getFormConfigOptions"
          />
          <!-- <ElEmpty v-else description="暂无搜索内容"></ElEmpty> -->
          <div v-else class="no-data">暂无数据</div>
        </slot>

        <div class="footer">
          <Button
            style="background-color: rgba(0, 179, 204, 0.3)"
            @click.native="handleResetForm"
          >
            重置
          </Button>
          <Button @click.native="handleSearch"> 搜索 </Button>
        </div>
      </div>
    </ElPopover>
  </div>
</template>

<script>
import Button from "@screen/components/Buttons/Button.vue";
import Form from "@screen/components/FormConfig";
import { cloneDeep } from "lodash";

export default {
  name: "InputSearch",
  components: {
    Button,
    Form,
  },
  props: {
    type: {
      type: String,
      default: "form",
    },
    placeholder: {
      type: String,
      default: "请点击右侧图标筛选",
    },
    valueData: {
      type: String,
    },
    params: {
      type: String,
    },
    queryParams: {
      type: Array,
    },
    formConfigOptions: {
      type: Object,
      default: null,
    },
    formList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      visible: false,
      types: this.type,
      value: "",
      width: null,
      popperOptions: {
        popHideCallBack: () => {
          // console.log("---")
        },
      },
    };
  },
  emit: ["handleSearch"],
  computed: {
    getFormConfigOptions() {
      return {
        column: "1",
        labelWidth: "120px",
        ...this.formConfigOptions,
      };
    },
  },

  methods: {
    handleShow() {
      if (this.width) return;

      this.width = this.$refs.ReferenceInputRef.getBoundingClientRect().width;
    },
    handleResetForm() {
      this.$refs.FormConfigRef?.reset();

      this.$refs.PopoverRef.doClose();

      this.$emit("handleSearch", cloneDeep(this.$refs.FormConfigRef?.formData));
    },
    async handleSearch() {
      if (this.types === "input") {
        let params = {};
        params[this.params] = this.value;
        let result = {};

        await this.$refs.FormConfigRef.validate()
          .then((res) => {
            result = res;
            this.$refs.PopoverRef.doClose();
          })
          .catch((err) => {
            console.log("catch");
          });
        let resultParams = { ...result, ...params };
        this.$emit("handleSearch", resultParams);
      } else {
        this.$refs.FormConfigRef.validate()
          .then((result) => {
            this.$refs.PopoverRef.doClose();
            this.$emit("handleSearch", result);
          })
          .catch((err) => {
            console.log("catch");
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.InputSearch {
  width: 100%;
  height: 26px;
  background: linear-gradient(180deg, #005c79 0%, #009bcc 100%) !important;
  color: #fff;
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 3px 15px;
  padding-right: 9px;
  cursor: pointer;

  font-size: 12px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  color: #ffffff;
  line-height: 14px;
}
.input ::v-deep .el-input__inner {
  background: none;
  height: auto !important;
  font-size: 12px;
  padding: 0px 0;
  line-height: 14px;
}
::v-deep .el-input__inner::placeholder {
  font-size: 12px;
}
</style>
