import breadcrumbComponent from '@/components/breadcrumb.vue'
import tableComponent from '@/components/table.vue'
import list from '@/mixins/list'
import routerUserMenu from '@/mixins/router-user-menu'

export default {
  name: 'DatabaseList',
  components: {breadcrumbComponent, tableComponent},
  mixins: [list, routerUserMenu],
  data () {
    return {
      currentSelected:[],
      originalData:[],
      resourceName: '',
      searchForm: {
        objectDatabase: '',
        databaseId: '',
        pageSize: 100000,
        page: 1
      },
      pageNum: 0,  //跨页勾选时用到
      databaseOptions: [],
      objectDatabaseOptions: [],
      // 列表数据
      tableData: {
        data: [],
        total: 0
      },
      // 列表设置
      tableOption: {
        // 是否显示序号
        index: true,
        // 是否显示勾选框
        select: true,
        // 默认排序
        defaultSort: {prop: 'updateTime', order: 'desc'},
        // 列
        tableLabel: [
          {prop: 'tableName', title: '表名', width: '100'},
          {prop: 'tableComment', title: '表中文名', width: '100'},
          {prop: 'resourceName', title: '所属数据库', width: '100'},
        ],
        // 操作
        tableOperate: false,
      },
      multiSelection: [],
      multiSelectionAllPage: {}
    }
  },
  created () {
  },
  mounted () {
    this.listDatabaseOption()
  },
  methods: {
    getFilterList(){
      this.updateMultiSelection()

    //  筛选数据库
      let filterData = this.originalData.filter((data)=>{
        return data.tableName.indexOf(this.resourceName)>=0 || data.tableComment.indexOf(this.resourceName)>=0
      })

      this.tableData={
        data: filterData,
        total:0
      }

      this.$nextTick(()=>{

        let selected=this.multiSelection

        selected.forEach((current)=>{
          this.$refs.table.$children[0].toggleRowSelection(this.tableData.data.find(d => { return d.id === current }),true)
        })

      })


      //
      // this.$refs.table.$children[0].toggleRowSelection(this.tableData.data.find(d => d.value === 1))
    },
    //扫描加载列表
    getList () {
      let param = {}
      if (this.searchForm.databaseId =='') {
        this.$message.error('请先选择所属数据库')
        return
      } else {
        param = this.searchForm
      }
      if (this.searchForm.databaseId == this.searchForm.objectDatabase) {
        this.$message.error('所属数据库与目标数据库不能相同')
        return
      }
      this.$api.storage.listTableScan (param)
        .then(res => {
          if (res.code ==1){
            this.originalData=res.data
            this.tableData.data=res.data
            this.tableData.total = res.totalNumber
          }
        })
    },

    /* 加载所属数据库下拉列表 */
    listDatabaseOption () {
      this.$api.storage.listDatabaseOption()
        .then(res => {
          this.objectDatabaseOptions = res.data
        })
      this.$api.storage.listDatabaseOptionAll()
        .then(res => {
          this.databaseOptions = res.data
        })
    },

    // 选中时的回调函数
    selectChange(arr) {
      this.currentSelected=arr.filter((item)=>{
        return item
      }).map(data=>{
        return data.id
      })
    },

    updateMultiSelection(){
      // arr 当前筛选后的选中的table
      let arr=this.currentSelected

      // 全量数据下已选的数据库信息
      let selectedTableArr=[...this.multiSelection]

      // 筛选后的可选数据库信息
      let currentTableArr=this.tableData.data.map((data)=>{
        return data.id
      })

      // 先去除selectedTableArr下包含的currentTableArr中的值
      selectedTableArr=selectedTableArr.filter((data)=>{
        return currentTableArr.indexOf(data)<0
      })

      // 然后再把arr加进去
      selectedTableArr=selectedTableArr.concat(arr)

      this.multiSelection=selectedTableArr

    },

    //检查对象是否为空
    checkObjNull(obj){
      for (let o in obj){
        if (obj[o].length>0){
          return false
        }
      }
      return true
    },

    add(){

      this.updateMultiSelection()

      // 判断组件设置是否通过验证
      this.$refs.searchForm.validate((valid) => {
        if (valid) {
          // 提交后台并在成功后回到上一页
          if (this.multiSelection.length===0){
            this.$message.error("请先勾选表")
            return
          }
          let params = {}
          params['databaseId'] = this.searchForm.databaseId
          params['targetDatabaseId'] = this.searchForm.objectDatabase
          params['tableIds'] = this.multiSelection
          this.$api.storage.createTableByCopy(params)
            .then(res => {
              if (res.code == 1) {
                this.$message.success(res.msg)
                this.go('MetaData')
              }
            })
        } else {
          return false
        }
      })
    }
  }
}
