import $ from 'jquery'

import { Local } from "@/utils/storage";
// 查询表格
const mixTableConstants = {
  data() {
    let { ...listeners } = this.$listeners;
    return {
      loading: true,
      listeners: listeners,
      collapse: false,
      tableId: undefined,
    }
  },
  props: {
    pageFixed: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    showLoading: {
      type: Boolean,
      default: () => {
        return false;
      }
    },
    height: {
      type: [ String, Number, Boolean ],
      default: () => {
        return false
      }
    },
    fixedHeight: {
      type: [ Boolean ],
      default: () => {
        return false
      },
    },
    disabled: {
      type: [ Boolean ],
      default: () => {
        return false
      },
    },
    formItemConfig: {
      type: [ Array, Object ],
      default: () => {
        return []
      }
    },
    /**
     * 多选框配置
     */
    checkboxConfig: {
      type: Object,
      default: () => {
        return {
          checkField: 'checked',
          trigger: 'row',
        }
      },
    },

    /** 工具栏配置 */
    rightToolbars: {
      type: [ Boolean, Object ],
      default: () => {
        return {
          perfect: true,
          export: {
            icon: 'el-icon-download',
          },
          custom: {
            icon: 'el-icon-s-grid',
          },
          slots: {
            buttons: 'buttons',
            tools: 'tools'
          },
        }
      }
    },
    /**
     * 分页
     */
    pagination: {
      type: [ Boolean ],
      default: () => {
        return true
      },
    },
    /**
     * 是否显示搜索表单
     */
    showSearchForm: {
      type: [ Boolean ],
      default: () => {
        return true
      },
    },
    showOverflow: {
      type: [ Boolean ],
      default: () => {
        return true
      },
    },
    toolsBar: {
      type: [ Boolean, Array, String ],
      default: () => {
        return false
      },
    },
    checkMethod: {
      type: [ Function ],
    },
    radioConfig: {
      type: Object,
      default: () => {
        return {
          checkRowKey: undefined,
        }
      },
    },
    /** 表格请求地址 */
    requestCall: { // 回调地址
      type: [ Function, Array, Object ],
      default: () => {
        return {
          data: [],
          total: 0,
        }
      },
    },
    constantLoad: Function,
    /**
     * 查询参数
     */
    queryParams: {
      type: Object,
      default: () => {
        return {}
      },
    },
    /** 查询验证条件 */
    queryRules: {
      type: Object,
      default: () => {
        return {}
      },
    },
    labelWidth: {
      type: String,
      default: () => {
        return '100px'
      }
    },
    tablePage: {
      type: Object,
      default: () => {
        return {
          total: 0,
          perfect: true,
          currentPage: 1,
          pageSize: 20,
          layouts: [ 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total' ],
        }
      },
    },
    callback: Function,
    columns: {
      type: Array,
      default: () => {
        return []
      },
    },
    editConfig: {
      type: [ Boolean, Object ],
      default: () => {
        return true
      }
    },
    searchMethod: {
      type: [ Boolean, Function ],
      default: () => {
        return false
      }
    },
    customRefreshTable: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    collapse: {
      handler() {
        if (this.$el)
          this.initTableHieght = this.$el.querySelector('.vxe-table--main-wrapper>.vxe-table--body-wrapper').clientHeight
        $(this.$el).find('.vxe-table--main-wrapper>.vxe-table--body-wrapper').height(this.initTableHieght)
      },
      immediate: true,
    },
  },
  methods: {
    seqMethod({ rowIndex }) {
      return (rowIndex + 1) * this.tablePage.currentPage
    },
    getTable() {
      return this.$refs['tableRef']
    },
    cellClick({ row }) {
      this.$emit('cell-click', row)
    },
    radioChange({ row }) {
      this.$emit('radio-change', row)
    },
  },
};

/**
 * 页面常量加载混入
 * @type {{data(): {mixDictionaries: {}, mixConstants: {}}, methods: {mixGetDictionaries(*, *=): Promise<unknown>, mixGetConstants(String, Array=): Promise<undefined>}}}
 */
const dictionaryMix = {
  data() {
    return {
      mixConstants: {},
      mixDictionaries: {},
    }
  },
  methods: {
    /**
     * 获取常量判断数据
     * @param requestCall url 常量判断数据请求地址
     * @param {Array} dictionariesConfig 常量判断数据配置,要传入后台的数据
     */
    mixGetDictionaries(requestCall, dictionariesConfig = []) {
      return new Promise((resolve, reject) => {
        requestCall(dictionariesConfig).then((res) => {
          if (res) {
            const { code, data } = res;
            if (code === 200) {
              let dictionaryObj = {};
              Object.keys(data).forEach(key => {
                dictionaryObj[key] = data[key] ? data[key] : data[key]
              });
              return resolve(dictionaryObj)
            } else {
              console.log('mixGetDictionaries res====', res)
              return reject(res)
            }
          }
        })
      })
    },
  },
};

/**
 * 表单配置
 * @type {{data(): {viewVisible: boolean, editVisible: boolean, insertVisible: boolean, editFormParams: {}}, created(), props: {formVisible: {default: (function(): boolean), type: BooleanConstructor, required: boolean}, callback: {type: FunctionConstructor}, formWidth: {default: (function(): number), type: [StringConstructor | String, NumberConstructor], required: boolean}, query: {default: (function(): {}), type: ObjectConstructor, required: boolean}}}}
 */
const formConfigMix = {
  data() {
    return {
      insertVisible: false,
      editVisible: false,
      viewVisible: false,
      editFormParams: {},
    }
  },
  props: {
    formVisible: {
      required: false,
      type: Boolean,
      default: () => {
        return false
      }
    },
    formWidth: {
      required: false,
      type: [ String, Number ],
      default: () => {
        let w = document.body.clientWidth / 2;
        if (w > 1600) {
          w = 700
        }
        return w
      }
    },
    query: {
      required: false,
      type: Object,
      default: () => {
        return {}
      }
    },
    callback: Function,
    closeCurrentDialog: Function,
  },
  created() {

  }
};

/**
 * 页面初始化使用
 * @type {{data(): {loaded: boolean, currentUser: {}, h: *}, created(): void, methods: {openPage(*=): void, success(*=, *=): void, error(*=, *=): void, mixMatchConstant(*, *, *): (string|boolean|undefined), info(*=, *=): void}}}
 */
const pageInitMix = {
  data() {
    return {
      loaded: false,
      h: undefined,
      currentUser: {},
      // context: this.$parent.$parent.$parent.$children[0]
    }
  },
  activated() {
    this.h = this.$createElement
    this.currentUser = JSON.parse(Local.get(process.env.VUE_APP_USER_KEY))
  },
  methods: {
    /**
     * 常量值判断--匹配常量值
     * value要判断的值
     * constantObj常量对象
     * 每个常量值需要返回的结果值配置
     */
    mixMatchConstant(value, constantObj, matchConfig) {
      let isMatch = false;
      for (let key in constantObj) {
        if (value === constantObj[key]) {
          isMatch = true;
          // 返回matchConfig中配置的值
          return matchConfig[key] ? 'el-link el-link--' + matchConfig[key] : false
        }
      }
      if (!isMatch) {
        // 当未匹配到任何值时返回false
        return false
      }
    },

    /**
     * 错误提示框
     * @param msg
     * @param config
     */
    error(msg, config) {
      this.$Notice.error(Object.assign({
        title: this.$t('common.tipMessage.title'),
        duration: 1.5,
        desc: msg
      }, config || {}))
    },
    /**
     * 成功提示框
     * @param msg
     * @param config
     */
    success(msg, config) {
      this.$Notice.success(Object.assign({
        title: this.$t('common.tipMessage.title'),
        duration: 3,
        desc: msg
      }, config || {}))
    },
    /**
     * 普通消息提示框
     * @param msg
     * @param config
     */
    info(msg, config) {
      this.$Notice.info(Object.assign({
        title: this.$t('common.tipMessage.title'),
        duration: 1.5,
        desc: msg
      }, config || {}))
    }
  }
}

// 定义表格默认元素
let mixTableConfig = {
  seq: {
    type: 'seq',
    width: '60',
    align: 'center',
    fixed: 'left',
    title: '编号',
    field: 'seq'
  },
  radio: {
    type: 'radio',
    width: '60',
    align: 'center',
    fixed: 'left',
    field: 'radio'
  },
  checkbox: {
    type: 'checkbox',
    width: 50,
    align: 'center',
    fixed: 'left',
    field: 'checkbox'
  },
  expand: {
    type: 'expand',
    width: 60,
    align: 'center'
  },
  html: {
    type: 'html',
    width: 60,
    align: 'center'
  },
  default: {
    minWidth: 200,
    align: 'left'
  },
  multiple: {
    minWidth: 300,
    className: 'multiple-cell',
    visible: true,
    sortable: true
  },

  fixedColumn: {
    minWidth: 200,
    align: 'left',
    fixed: 'left'
  },
  operate: {
    align: 'center',
    fixed: 'right',
    field: 'opts',
    width: 140,
  },
  amount: {
    minWidth: 120,
    align: 'right',
    cellRender: {
      name: 'money'
    }
  }
}

/**
 * ifrmae混入配置
 */
const iframeMinix = {
  mounted() {
    // 添加混入当前对象到上下文中
    window.$bus = this;
  }
}
export { mixTableConstants, dictionaryMix, pageInitMix, mixTableConfig, formConfigMix, iframeMinix }
