<script setup name="Discover">
import { onMounted, ref, toRefs } from 'vue'
import { Split } from 'view-ui-plus';
import { ElMessage } from 'element-plus'
import CollapseView from '@/components/CollapseView/index.vue'
import AddDomain from './modules/AddDomain.vue' // 数据域表单
import AddRules from './modules/AddRules.vue' // 发现规则表单
import ModalPop from "@/components/EditPop/ModalPop.vue"

import { query, delDataArea, delRule, setdefault } from '@/api/ruleConfig/discover'

const splitNum = ref(0.31) // 左右分割比例
const collapseList = ref([]) // 数据域数据

const data = reactive({
  domainForm: {

  },
  rulesForm: {

  },
  queryParams: {
    areaType: "0",
    rulename: ""
  },
  delData: {
    id: '',
    dataarea: ''
  }
});

const { domainForm, rulesForm, queryParams, delData } = toRefs(data);
const rightType = ref('domain') // 右侧展示类型
const modalData = reactive({
  show: false,
  text: '',
  icon: 'error',
  cancel: true,
  type: ''
})

const modalPopData = reactive({
  show: false,
  title: '',
  type: 'domain'
})

// 获取数据
const getCollapse = () => {
  query(queryParams.value).then(res => {
    const { data } = res
    collapseList.value = data.map(item => {
      const list = item.data.map(itemTwo => {
        return {
          ...itemTwo,
          isCheck: true,
          isDelete: true,
        }
      })
      return {
        ...item.dataarea,
        list: list,
        isAdd: true,
        isView: true,
        isDelete: true,
      }
    })
    if (collapseList.value.length <= 0) return
    domainForm.value = collapseList.value[0]
    rightType.value = 'domain'
  })
}

// 新增关闭
const modalPopCancel = () => {
  modalPopData.show = false
}

// 新增数据域
const addDomainClick = () => {
  console.log('新增数据域')
  modalPopData.title = '新增数据域'
  modalPopData.type = 'domain'
  modalPopData.show = true
}

// 新增发现规则
const collapseAdd = (item) => {
  console.log('新增发现规则', item)
  const { id, dataarea } = item
  rulesForm.value = {
    dataarea_id: id,
    type: dataarea
  }
  modalPopData.title = '新增发现规则'
  modalPopData.type = 'rules'
  modalPopData.show = true
}

// 删除发现规则
const collapseDelete = (item) => {
  console.log('删除发现规则', item)
  delData.value = item
  modalData.type = 'rules'
  modalData.cancel = true
  modalData.icon = 'error'
  modalData.text = '删除后无法恢复，是否确认删除[' + item.name + ']?'
  modalData.show = true
}

// 删除数据域
const collapseMainDeletion = (item) => {
  console.log('删除数据域事件', item)
  delData.value = item
  modalData.type = 'domain'
  modalData.cancel = false
  modalData.icon = 'error'
  modalData.text = '删除后无法恢复，是否确认删除[' + item.dataarea + ']?'
  modalData.show = true
}

// 提示回调
const modalConfirm = () => {
  if (modalData.type === 'domain') {
    console.log('删除数据域确认')
    const data = {
      areaid: delData.value.id,
      areaname: delData.value.dataarea
    }
    delDataArea(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('删除成功')
        getCollapse()
      } else {
        ElMessage.error('删除失败')
      }
    })
  } else if (modalData.type === 'rules') {
    console.log('删除发现规则确认')
    const data = {
      ruleid: delData.value.ruleid,
      rulename: delData.value.name,
      tid: delData.value.tid
    }
    delRule(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('删除成功')
        getCollapse()
      } else {
        ElMessage.error('删除失败')
      }
    })
  } else if (modalData.type === 'default') {
    console.log('设置默认项')
    const data = {
      ruleid: delData.value.ruleid,
      rulename: delData.value.name,
    }
    setdefault(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('设置成功')
        getCollapse()
      } else {
        ElMessage.error('设置失败')
      }
    })
  }
  modalData.show = false
}

// 数据域查看
const viewClick = (item) => {
  domainForm.value = item
  rightType.value = 'domain'
}

// 规则点击监听
const collapseChange = (item) => {
  const { id, dataarea } = item.parentItem
  rulesForm.value = {
    ...item.item,
    ...{
      dataarea_id: id,
      type: dataarea
    }
  }
  rightType.value = 'rules'
}

// 发现规则设置默认项
const collapseDefault = (item) => {
  delData.value = item
  modalData.type = 'default'
  modalData.cancel = false
  modalData.icon = 'error'
  modalData.text = '确定设置[' + item.name + ']为默认规则?'
  modalData.show = true
}

// 新增确认
const AddFormConfirm = (item) => {
  if (modalPopData.type === 'domain') {
    console.log('新增数据域确认', item)
    getCollapse()
  } else if (modalPopData.type === 'rules') {
    console.log('新增发现规则确认', item)
    getCollapse()
  }
  modalPopData.show = false
}

onMounted(() => {
  getCollapse()
})

</script>
<template>
  <div class="app-container scroller">
    <PageTitle>
      <template #title>
        发现规则
      </template>
    </PageTitle>
    <div class="app-container__body">
      <Split v-model="splitNum">
        <template #left>
          <div class="demo-split-pane"
            style="padding: 0 0 10px 0;width: 100%;overflow: auto;height: 100%;display: flex;flex-direction: column;">
            <div class="mb20"
              style="padding-right: 38px;display: flex;align-items: center;justify-content: space-between;">
              <el-input class="mr20" v-model="queryParams.rulename" placeholder="数据域名称搜索">
                <template #suffix>
                  <el-icon style="vertical-align: middle;cursor: pointer;" @click="getCollapse">
                    <search />
                  </el-icon>
                </template>
              </el-input>
              <el-button type="primary" icon="Plus" @click="addDomainClick">新增数据域</el-button>
            </div>
            <el-scrollbar style="width: 100%;flex: 1;padding-right: 38px;">
              <CollapseView :list="collapseList" @add="collapseAdd" @mainDeletion="collapseMainDeletion"
                @childDelete="collapseDelete" @change="collapseChange" @view="viewClick" @default="collapseDefault" />
            </el-scrollbar>
          </div>
        </template>
        <template #right>
          <div class="demo-split-pane">
            <div class="right">
              <div class="right-title">
                <el-icon>
                  <info-filled />
                </el-icon>
                <span style="margin-left: 5px;">{{ rightType === 'domain' ? '数据域' : '发现规则' }}</span>
              </div>
              <div class="right-content">
                <AddDomain :itemData="domainForm" type="edit" @confirm="AddFormConfirm" v-if="rightType === 'domain'" />
                <AddRules :itemData="rulesForm" type="edit" @confirm="AddFormConfirm" v-if="rightType === 'rules'" />
              </div>
            </div>
          </div>
        </template>
      </Split>
    </div>
    <!-- 提示框 -->
    <Modal v-model="modalData.show" :icon="modalData.icon" :cancel="modalData.cancel" :text="modalData.text"
      @confirm="modalConfirm"></Modal>

    <!-- 新增数据域 / 发现规则 -->
    <ModalPop :width="'740'" v-model="modalPopData.show" :title="modalPopData.title" @cancel="modalPopCancel">
      <template #content>
        <AddDomain type="add" @cancel="modalPopCancel" @confirm="AddFormConfirm"
          v-if="modalPopData.type === 'domain'" />
        <AddRules type="add" :itemData="rulesForm" @cancel="modalPopCancel" @confirm="AddFormConfirm"
          v-if="modalPopData.type === 'rules'" />
      </template>
    </ModalPop>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  height: calc(
    100vh - var(--navbar-height) - var(--container-pd) - var(--container-pd)
  ) !important;
  .right {
    padding: 6px 10px 10px;
    height: 100%;
    &-title {
      padding: 0px 0px 10px 10px;
      display: flex;
      align-items: center;
      font-weight: 700;
      color: #515a6e;
    }
    &-content {
      padding: 10px 0px;
      width: 80%;
      margin: 20px auto;
    }
  }
}
</style>