<script>
import components from './_import-components/sd-org-common-mixins-import'

export default {
  name: 'SdOrgCommonMixins',
  components,
  data() {
    return {
      parent: null,
      customFilterExpressions: [],
      showfuzzySearch: true,
      advSearchBtnActived: false,
    }
  },
  computed: {
    filterExpressions() {
      let expressions = []
      if (this.parent) {
        expressions.push({
          dataType: 'str',
          name: 'parentId',
          op: 'eq',
          stringValue: this.parent.id,
        })
      }
      if (this.customFilterExpressions.length > 0) {
        expressions = expressions.concat(this.customFilterExpressions)
      }
      return expressions
    },
  },
  created() {
    this.init()
  },
  methods: {
    processReq(req, listType) {
      if (req.data.expressions.length > 1) {
        // 搜索条件包含了查询条件：模糊查询或高级查询。删除parentId的条件（查询为全局查询）
        req.data.expressions = req.data.expressions.filter((exp) => exp.name !== 'parentId')
        // 搜索时传递该参数显示用户部门全路径
        req.data.searchAction = true
      }
      req.data.orderBy = 'weight asc,id desc'
      if (listType === 'orgGroupCategory') {
        req.data.orderBy = 'sortNumber asc,id desc'
      }
      return req
    },
    // 点击树
    treeSelect(info) {
      this.parent = info
      this.$refs.dataTable.clearSelection(true)
    },
    search(value) {
      this.$refs.dataTable.clearSelection(true)
      this.customFilterExpressions = value
    },
    // 切换查询（高级查询、模糊查询）
    switchSearch() {
      if (this.advSearchBtnActived) {
        // 显示了高级查询，则重置
        this.$refs.searchForm.reset()
      } else {
        // 未显示高级查询，则置空模糊查询
        this.showfuzzySearch = false
        this.$refs.dataTable.onSearchChange('')
        this.$nextTick(() => {
          this.showfuzzySearch = true
        })
      }
      this.advSearchBtnActived = !this.advSearchBtnActived
    },
  },
}
</script>

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