<template>
  <SdAdvanceSearch
    v-if="initValues.length"
    :class="$style.search"
    @search="search"
    @reset="reset"
    @close="close"
  >
    <sd-form ref="form" :init-values="initValues" :class="$style.form">
      <template>
        <table>
          <tr>
            <sd-form-item-td
              v-for="item in initValues"
              :key="item.name"
              v-bind="item"
              :component="item.attr && item.attr.selectListItem ? 'a-select' : ''"
              :input-props="{
                allowClear: true,
                style: item.attr && item.attr.selectListItem ? 'width: 150px' : '',
              }"
              @keypress="
                (evt) => {
                  if (evt.keyCode === 13) search()
                }
              "
            >
            </sd-form-item-td>
          </tr>
        </table>
      </template>
    </sd-form>
  </SdAdvanceSearch>
</template>

<script>
import SdAdvanceSearch from '@/application-manage/sd-advance-search.vue'
import runtimeUtil from '@/form-designer/runtime-util'
import components from './_import-components/sd-org-list-search-import'

export default {
  name: 'SdOrgListSearch',
  metaInfo: {
    title: 'SdOrgListSearch',
  },
  components: {
    ...components,
    SdAdvanceSearch,
  },
  props: {
    onlListColumnDefs: {
      type: Array,
      default: undefined,
    },
  },
  data() {
    return {
      customFilterExpressions: [],
    }
  },
  computed: {
    initValues() {
      return this.onlListColumnDefs
        .filter((item) => {
          return item.search
        })
        .map((item) => {
          let dataType = item.fieldType.replace(/selectgroup|selectuser/, 'selectperson')
          let selectionType = item.attr?.selectionType || ''
          if (item.fieldId === 'CREATOR_NAME') {
            // 内置字段创建人创建部门特殊处理下，改成选者器
            dataType = 'selectperson'
            selectionType = 'User'
          }
          if (item.fieldId === 'CREATE_DEPT_NAME') {
            dataType = 'selectperson'
            selectionType = 'Group'
          }
          return {
            name: item.fieldId,
            attr: {
              selectionType,
              selectListItem: item.attr?.selectListItem,
            },
            caption: item.fieldName,
            dataType,
            inputProps: { allowClear: true, single: true },
            component:
              dataType === 'date' ? 'ARangePicker' : runtimeUtil.getRender(item.attrFd?.cid),
          }
        })
    },
  },
  methods: {
    search() {
      const t = []
      const fieldsValue = this.$refs.form.getFieldsValue()
      this.initValues.forEach((item) => {
        if (fieldsValue[item.name]) {
          if (item.dataType === 'string' || item.dataType === 'title') {
            t.push({
              dataType: 'str',
              name: item.name,
              op: 'like',
              stringValue: '%' + fieldsValue[item.name] + '%',
            })
          }
          if (item.dataType === 'selectlist') {
            t.push({
              dataType: 'str',
              name: item.name,
              op: 'eq',
              stringValue: fieldsValue[item.name],
            })
          }
          if (item.dataType === 'selectperson' && fieldsValue[item.name].length) {
            let stringValue = fieldsValue[item.name]?.[0]['code']
            if (['CREATE_DEPT_NAME', 'CREATOR_NAME'].includes(item.name)) {
              // 创建人创建部门存的是就是名称字符串，这里特殊处理下
              stringValue = fieldsValue[item.name]?.[0]['name']
            }
            t.push({
              dataType: 'str',
              name: item.name,
              op: 'eq',
              stringValue,
            })
          }
          if (item.dataType === 'date' && fieldsValue[item.name].length) {
            t.push({
              dataType: 'long',
              name: item.name,
              op: 'ge',
              longValue: new Date(
                fieldsValue[item.name][0].format('YYYY-MM-DD') + ' 00:00:00'
              ).valueOf(),
            })
            t.push({
              dataType: 'long',
              name: item.name,
              op: 'le',
              longValue: new Date(
                fieldsValue[item.name][1].format('YYYY-MM-DD') + ' 23:59:59'
              ).valueOf(),
            })
          }
        }
        if (item.dataType === 'number') {
          if (fieldsValue[item.name]) {
            t.push({
              dataType: 'long',
              name: item.name,
              op: 'eq',
              longValue: Number(fieldsValue[item.name]),
            })
          }
        }
      })
      this.$emit('search', t)
    },
    reset() {
      for (const x in this.$refs.form.getFieldsValue()) {
        this.$refs.form.setFieldValue(x, undefined)
      }
      this.search()
    },
    close() {
      this.reset()
      this.$emit('close')
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
