import { views } from '@/utils/constants'

// 视图
export const $view = {
  props: {
    view: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      views
    }
  },
  computed: {
    hasView() {
      return (views) => {
        if (views == null || views.length === 0 || this.view == null) {
          return false;
        }
        let list = views;
        if (views instanceof String) {
          list = views.split(',');
        }
        if (!(list instanceof Array)) {
          list = [list]
        }
        return list.includes(this.view);
      }
    },
    notHasView() {
      return (views) => {
        return !this.hasView(views);
      }
    }
  }
}


/**
 * 显隐列
 **/
export const $showColumns = {
  props: {
    hideColumns: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      columns: []
    }
  },
  computed: {
    showColumns() {
      if (this.columns == null) {
        return [];
      }
      return this.columns.filter(item => item.visible);
    },
    isShow() {
      return (key) => {
        if (this.columns == null) {
          return false;
        }
        let column = this.columns.find(item => item.key === key);
        return column != null && column.visible;
      }
    }
  },
  methods: {
    // 初始化列
    initColumns() {
      this.hideColumn(this.hideColumns);
    },
    // 隐藏列
    hideColumn(columns) {
      if (this.columns != null) {
        this.columns.filter(item => columns.includes(item.key))
          .forEach(item => {
            item.visible = false;
          })
      }
    },
    // 删除列
    removeColumn(columns) {
      if (columns != null) {
        columns.forEach(column => {
          let index = this.columns.findIndex(item => column === item.key);
          if (index != null && index > -1) {
            this.columns.splice(index, 1);
          }
        })
      }
    }
  }
}

export const $showSearch = {
  props: {
    // 删除的查询列
    removeSearchColumns: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      searchColumns: [],
    }
  },
  methods: {
    // 初始化查询列
    initSearchColumns() {
      this.removeSearchColumn(this.removeSearchColumns);
    },
    // 删除查询列
    removeSearchColumn(columns) {
      if (columns != null) {
        columns.forEach(column => {
          let index = this.searchColumns.findIndex(item => column === item.key);
          if (index != null && index > -1) {
            this.searchColumns.splice(index, 1);
          }
        })
      }
    },
  }
}

/**
 * 列表选择
 */
export const $listSelect = {
  props: {
    // 开启选择模式
    enableSelect: {
      type: Boolean,
      default: false,
    },
    // 开启多选
    enableMultiple: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    // 切换一行的选中状态
    toggleRowSelection(row, flag) {
      this.$refs.table.toggleRowSelection(row, flag);
    },
    // 刷新表格的选中状态
    refreshTableSelection() {
      if(this.enableMultiple){
        this.tableData.forEach(item => {
          if (this.selected.map(j => j[this.prop]).includes(item[this.prop])) {
            this.toggleRowSelection(item, true);
          } else {
            this.toggleRowSelection(item, false);
          }
        });
      }
    },
  }
}

// 列表页配置项
export const $listConfig = {
  props: {
    // 用户自定义配置
    customConfig: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      globalDefaultConfig: {
        containerClass: 'app-container',  // 容器class
        showQuery: true,  // 搜索栏
        showRowOpera: true, // 行操作
        showTableOpera: true, // 列操作
        showRefresh: true,  // 刷新按钮
        showSearch: true, // 搜索按钮
        showTools: true,  // 展示工具栏
        showPage: true,   // 分页
        showSelection: true,  // 选择栏
      },
      defaultConfig: {}
    }
  },
  computed: {
    // 最终的配置
    listConfig() {
      return {
        ...this.globalDefaultConfig,
        ...this.defaultConfig,
        ...this.customConfig
      }
    }
  }
}
