<template>
  <CusDialog
    :show="show"
    title="索引列表"
    @onClose="$emit('update:show', false)"
    width="1000px"
    height="80%"
    :show-close="false"
    :show-submit="false"
  >
    <div class="__cus-manage_content">
      <CusForm labelWidth="100px" @handleEnter="onSearch">
        <CusFormColumn
          :span="8"
          v-model:param="state.query.form.keyword"
          placeholder="请输入索引中文名称或索引表名进行搜索"
        />
        <CusButton style="margin-left: 20px" type="main" title="搜索" @click="onSearch"/>
        <CusButton type="main" title="重置" @click="onReset"/>
        <CusButton style="margin-left: auto" type="main" title="一键同步" @click="onTaskAll"/>
      </CusForm>
      <div class="__cus-manage_content-main" v-loading="state.query.loading">
        <CusTable
          :page-num="state.query.page.pageNum"
          :page-size="state.query.page.pageSize"
          :total="state.query.result.total"
          :data="state.query.result.data"
          :table-head="state.query.tableHead"
          @handlePage="onPage"
        >
          <template #do-column-value="{scope}">
            <CusButton type="table" icon="relation" title="同步历史数据" @click="onTask(scope.row)"/>
          </template>
        </CusTable>
      </div>
    </div>
  </CusDialog>
</template>

<script setup lang="ts">
import {computed, getCurrentInstance, nextTick, reactive, ref, watch} from "vue";
import {useDictionaryStore} from "@/stores";
import {ElMessage, ElMessageBox} from "element-plus";
import {sysIndexFindIndexByPage} from "@/api/modules/manage";
import {jobCreate} from "@/api/modules/manage/task";

const emit = defineEmits(['update:show', 'refresh'])
const {proxy} = getCurrentInstance()
const DictionaryStore = useDictionaryStore()
const props = defineProps({
  show: {default: false},
  transfer: {}
})
const state: any = reactive({
  query: {
    loading: false,
    page: {
      pageNum: 1,
      pageSize: 20
    },
    tableHead: [
      {value: "indexName", label: "索引中文"},
      {value: "indexTableName", label: "索引表名", popover: true},
      {value: "do", label: "操作", width: 200, fixed: 'right'},
    ],
    form: {},
    formReal: {},
    result: {
      total: 0,
      data: []
    }
  },
  detail: {
    show: false,
    transfer: {}
  },
})
const onPage = (pageNum, pageSize) => {
  state.query.page = {
    pageNum: pageNum,
    pageSize: pageSize
  }
  const params = {
    page: state.query.page.pageNum,
    size: state.query.page.pageSize,
    isCreateEsIndex: 1
  }
  //  添加表单参数
  for (const [k, v] of Object.entries(state.query.formReal)) {
    if (proxy.$util.isValue(v)) {
      params[k] = v
    }
  }
  state.query.loading = true
  sysIndexFindIndexByPage(proxy.$util.formatGetParam(params)).then(res => {
    state.query.result.total = res.data.totalElements
    state.query.result.data = res.data.content
    state.query.loading = false
  })
}
const onSearch = () => {
  state.query.formReal = JSON.parse(JSON.stringify(state.query.form))
  onPage(1, state.query.page.pageSize)
}
const onReset = () => {
  state.query.page = {
    pageNum: 1,
    pageSize: 20
  }
  state.query.form = {}
  onSearch()
}
watch(() => props.show, (n) => {
  if (n) {
    initDictionary()
    onReset()
  }
})
const onTask = (row) => {
  ElMessageBox.confirm(`确定要同步索引${row.indexName}(${row.indexTableName})吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const params = {
      type: 0,
      name: `${DictionaryStore.taskTypeMap.get(String(0))}_${proxy.$util.YMDHms(new Date())}`,
      effect: 1,
      indexCode: row.indexTableName,
    }
    jobCreate(params).then(res => {
      ElMessage.success(res.data.respMsg)
    })
  }).catch(() => {})
}
const onTaskAll = () => {
  ElMessageBox.confirm(`确定要同步全部索引吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const params = {
      type: props.transfer.type,
      name: `${DictionaryStore.taskTypeMap.get(String(props.transfer.type))}_${proxy.$util.YMDHms(new Date())}`,
      effect: 1,
      indexCode: 'all',
    }
    jobCreate(params).then(res => {
      ElMessage.success(res.data.respMsg)
    })
  }).catch(() => {})
}
const initDictionary = () => {
  DictionaryStore.initDict('task_type')
}
</script>

<style lang="scss" scoped>
.__cus-manage_content {
  margin-bottom: 0;
  height: calc(100% - 24px);
}
</style>