/*
 *@description:全司表格自定义列
 *@author: hudw
 *@date: 2022-03-24 18:08:14
 */
import { SERVICE_PLATFORM_POWER_BASE } from '@/config/serviceSettings.js'

import store from '@/store'
import request from '@/utils/request'

function _onConfirm(columns, close) {
  const { id, extendedObjectCode } = this.$attrs
  const { menuId } = this.$route.meta

  let customize = columns
  store.dispatch('getUserColumn', {
    menuId,
  })
  return request({
    method: 'post',
    url: SERVICE_PLATFORM_POWER_BASE + '/customize/edit',
    data: {
      menuId,
      listId: id,
      extendedObjectCode: extendedObjectCode || 0,
      customize: JSON.stringify(customize),
    },
  }).then((response) => {
    if (response.success) {
      store.dispatch('updateColumnPosition', {
        data: customize,
        menuId,
        listId: id,
      })

      this.$message.success(response.message)
    } else {
      this.$exception(response)
    }
  })
}
function _onReset(fn) {
  let confirmLoading = false
  let _this = this
  return this.$confirm({
    title: '是否重置默认序列?',
    confirmLoading,
    onOk(clear) {
      confirmLoading = true
      const { id } = _this.$attrs
      const { menuId } = _this.$route.meta
      return request({
        url: SERVICE_PLATFORM_POWER_BASE + '/customize/reset',
        method: 'post',
        data: {
          menuId,
          listId: id,
        },
      }).then((response) => {
        confirmLoading = false
        if (response.success) {
          clear()
          store
            .dispatch('getUserColumn', {
              menuId,
            })
            .then((response) => {
            //  _this.setState()

            })
          fn()

          _this.$message.success(response.message)
        } else {
          _this.$exception(response)
        }
        return response
      })
    },
  })
}
export default {
  data() {
    let sign = this.$attrs.sign
    return {
      confirmLoading: false,
    }
  },
  beforeMount() {},
  watch: {
    '$parent.columns'(value,lastValue) {
      if (value.length !== lastValue.length) {
        this.state = this.infoState(value);
      }
    },
    columns(value, lastValue) {
      if (value.length !== lastValue.length) {
        this.state = this.infoState(value);
      }
    },
    '$attrs.id'() {
      this.dragState = {}
      this.state = null
      if (this.$parent && !this.$parent.columns) {
        //重置数据
        this.state = this.infoState(this.columns)
      } else if (this.$parent?.columns?.length > 0) {
        this.state = this.infoState(this.$parent.columns)
      }
      this.sortCol(this.columns)
    },

  },
  mounted() {

    if (this.$attrs.id && this.filterDropdownColumn) {
      this.filterDropdownColumn.onReset = _onReset
      this.filterDropdownColumn.onConfirm = _onConfirm

      this.sortCol(this.columns)
    }
  },
  methods: {
    closure() {
      return null
    },
    parseCol(columns, baseData) {
      // columns.splice();
      this.$nextTick(() => {
        this.dragState = baseData

        console.log('排序完成', baseData)
      })
    },
    sortCol(columns) {
      if (!this.$route || (!this.$route.meta && this.$options.name == 'PTable')) return
      const { id } = this.$attrs
      const { menuId } = this.$route.meta
      let { columnPosition } = store.getters
      try {
        let _columnPosition = columnPosition[menuId]
        if (_columnPosition && id && Object.prototype.toString.call(_columnPosition[id]) === '[object Object]' && Object.keys(_columnPosition[id]).length > 0) {
          return this.parseCol(columns, _columnPosition[id])
        }
      } catch (e) {
        throw new Error(e)
        //TODO handle the exception
      }
    },
  },
}
