<template>
  <div :key="'simple-filter-panel-' + windowNo + '-tabIndex-' + tabIndex">
    <div>
      <div class="input-group">
        <div class="input-group-addon m-input-group-addon" @click="clearSearchCondition">
          <span
            class="fa fa-list-ul"
            :class="{ 'red-icon': (searchText != null && searchText.length > 0)}"
          />
        </div>
        <input
          id="simple-filter-panel-input"
          autocomplete="off"
          :value="searchText"
          
          type="text"
          class="form-control"
          :placeholder="$t('lang.simpleFilterPanel.inputCondition')"
          :readonly="disabled == true"
          @input="$emit('update:searchText', $event.target.value)"
          @keydown.enter="commonSearchOk"
          @focus="showHistoryQueryCondition=true"
        />
        <div
          class="input-group-addon m-input-group-addon1"
          @click="$emit('showComplexFilterPanel')"
        >
          <span class="glyphicon glyphicon-list-alt" />
        </div>
        <div
          class="input-group-addon m-input-group-addon"
          @click="commonSearchOk"
        >
          <span class="glyphicon glyphicon-search" />
        </div>
      </div>

      <div style="position:relative">
        <ul
          v-if="showHistoryQueryCondition && conditions.length > 0 && (disabled == null || disabled == false)"
          v-click-outside="onClickOutside"
          class="select-history"
        >
          <li
            v-for="item in conditions"
            :key="'history-query-condition-' + item.id"
            @click="selectCommonHistoryQueryCondition(item)"
          >
            <span>{{ item.condition }}</span>
            <i
              class="fa fa-times"
              @click="deleteHistoryQueryById($event, item.id)"
            />
          </li>
          <li
            v-if="conditions.length>0"
            class="btn-clear-records"
          >
            <a
              type="button"
              @click="deleteHistoryQuery()"
            >
              {{ $t('lang.simpleFilterPanel.clearHistoryRecord') }}
            </a>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Common from '../../common/Common.js';

import HistoryQueryResource from '../../api/system/HistoryQueryResource.js';
import vClickOutside from 'click-outside-vue3';
import { Notify, Uuid } from 'pc-component-v3';
export default {
  directives: {
    clickOutside: vClickOutside.directive,
  },
  components: {
  },

  props: {
    /**
     * CURD窗口编号
     */
    windowNo: {
      type: String,
      default: null,
    },
    /**
     * 页签顺序号
     */
    tabIndex: {
      type: Number,
      default: null,
    },
    /**
     * 过滤方案
     */
    filterSchema: {
      type: Object,
      default: null,
    },
    /**
     * 简单过滤方案只读
     */
    disabled: {
      type: Boolean,
      default: false,
    },
    /**
     * 搜索的文本
     */
    searchText: {
      type: String,
      default: '',
    },
  },

  emits: ['simpleSearch', 'showComplexFilterPanel', 'update:searchText'],



  data: function () {
    return {
      showHistoryQueryCondition: false,
      conditions: [],
      simpleFilterHistoryQueryNumber: 10,     //简单查询显示记录数
    };
  },

  watch: {
    /**
     * 切换窗口(页签Id发生改变)的时候重新加载数据
     */
    showHistoryQueryCondition: {
      handler: function (newValue, oldValue) {
        if(newValue === true){
          this.getHistoryQueries();
        }
      },
    },
  },


  methods: {

    /**
     * 简单查询
     */
    commonSearchOk: function () {
      var _self = this;

      if (this.disabled === true) {
        return;
      }

      if (this.filterSchema == undefined || this.filterSchema.filterSchemaSimpleLines == undefined) {
        Notify.error('错误:未定义过滤方案', '请联系系统管理员定义过滤方案', false);
        return;
      }

      var historyQuery = {
        type: 'SimpleFilter',
        condition: _self.searchText,
        queryName: '',
        windowNo: _self.windowNo,
        tabIndex: _self.tabIndex,
      };

      HistoryQueryResource.save(historyQuery).then(successData => {
        _self.getHistoryQueries();
      }, errorData => {
        Common.processException(errorData);
      });

      this.showHistoryQueryCondition = false;

      this.$emit('simpleSearch');
    },


    /**
     * 选择了历史查询条件
     */
    selectCommonHistoryQueryCondition: function (item) {
      this.$emit('update:searchText', item.condition);
      this.showHistoryQueryCondition = false;
      this.commonSearchOk();
    },


    /**
     * 清空搜索条件
     */
    clearSearchCondition: function () {
      this.showHistoryQueryCondition = false;
      this.$emit('update:searchText', '');
      this.$emit('simpleSearch');
    },

    
    /**
     * 获取查询历史条件
     */
    getHistoryQueries: function () {
      var _self = this;

      _self.conditions = [];

      if(_self.windowNo == null 
          || _self.windowNo === ''
          || _self.tabIndex == null
          || _self.tabIndex === ''){
        return;
      }

      HistoryQueryResource.listByCondition('SimpleFilter', _self.simpleFilterHistoryQueryNumber, _self.windowNo, _self.tabIndex).then(successData => {
        _self.conditions = successData;
      }, errorData => {
        Common.processException(errorData);
      });

    },

    /**
     * 删除查询历史条件
     */
    deleteHistoryQuery: function () {
      var _self = this;
      HistoryQueryResource.deleteByType('SimpleFilter', _self.windowNo, _self.tabIndex).then(successData => {
        _self.getHistoryQueries();
        _self.showHistoryQueryCondition = false;
        _self.$emit('update:searchText', '');
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
     * 根据id删除查询历史条件
     */
    deleteHistoryQueryById: function (event, id) {
      event.stopPropagation();
      var _self = this;
      HistoryQueryResource.deleteById('SimpleFilter', id).then(successData => {
        _self.getHistoryQueries();
        _self.showHistoryQueryCondition = false;
      }, errorData => {
        Common.processException(errorData);
      });
    },


    /**
     * 当点击'历史查询条件列表'以外的DOM，隐藏'历史查询条件列表'
     */
    onClickOutside: function(e){
      if(e.srcElement != null && e.srcElement.id == 'simple-filter-panel-input'){
        return;
      }
      this.showHistoryQueryCondition = false;
    },
  },
};

</script>

<style scoped>
.m-input-group-addon {
  cursor: pointer;
}

.m-input-group-addon1 {
  cursor: pointer;
  border-left: none;
}

.red-icon {
  color: red;
}

.select-history {
  background: #fff;
  border: 1px solid #ccc;
  width: calc(100% - 118px);
  height: 150px;
  overflow-x: hidden;
  margin-left: 40px;
  position: absolute;
  list-style: none;
  z-index: 999;
  text-align: left;
  font-size: 14px;
  border-radius: 4px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
  cursor: pointer;
  padding-left: 0px;
  z-index: 1002;
}

.select-history > li {
  display: block;
  padding: 3px 5px 3px 10px;
  clear: both;
  font-weight: 400;
  line-height: 1.42857143;
  color: #333;
  white-space: nowrap;
  height: 25px;
}

.select-history > li > span {
  float: left;
}

.select-history > li > i {
  float: right;
}

.select-history > li:hover {
  color: #262626;
  text-decoration: none;
  background-color: #f5f5f5;
}

.fa-times {
  float: right;
  margin-right: 15px;
}
.f:hover {
  height: 120%;
  width: 120%;
}
.delete {
  font-size: 800px;
  color: blue;
  float: right;
}
#deleteHistory {
  color: blue;
}

.btn-clear-records {
  cursor: pointer;
}
</style>