<script setup name="Discover">
import { onMounted, ref, toRefs } from 'vue'
import { Split } from 'view-ui-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"

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

const data = reactive({
  editDomainForm: {
    name: "",
  },
  addDomainForm: {
    name: "",
  },
  editRulesForm: {
    name: "",
  },
  addRulesForm: {
    name: "",
  }
});

const { editDomainForm, addDomainForm, editRulesForm, addRulesForm } = 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 = () => {
  const data = [
    {
      name: '测试数据域',
      list: [
        {
          name: '按混合证件号字段查询'
        }
      ]
    },
    {
      name: '通用规则',
      list: [
        {
          name: 'aaaaa'
        },
        {
          name: 'bbbbb'
        }
      ]
    },
    {
      name: '解密数据',
      list: []
    }
  ]
  collapseList.value = data.map(item => {
    const list = item.list.map(itemTwo => {
      return {
        ...itemTwo,
        isCheck: true,
        isDelete: true,
      }
    })
    return {
      ...item,
      list: list,
      isAdd: true,
      isView: true,
      isDelete: true,
    }
  })
}

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

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

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

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

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

// 提示回调
const modalConfirm = () => {
  if (modalData.type === 'domain') {
    console.log('删除数据域确认')
  } else if (modalData.type === 'rules') {
    console.log('删除发现规则确认')
  } else if (modalData.type === 'default') {
    console.log('设置默认项')
  }
  modalData.show = false
}

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

// 规则点击监听
const collapseChange = (item) => {
  editRulesForm.value = item
  rightType.value = 'rules'
}

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

// 新增确认
const AddDomainConfirm = (item) => {
  if (modalPopData.type === 'domain') {
    console.log('新增数据域确认', item)
  } else if (modalPopData.type === 'rules') {
    console.log('新增发现规则确认', item)
  }
  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 38px 10px 0;width: 100%;overflow: auto;height: 100%;">
            <div class="mb20" style="display: flex;align-items: center;justify-content: space-between;">
              <el-input class="mr20" placeholder="数据域名称搜索">
                <template #suffix>
                  <el-icon style="vertical-align: middle;">
                    <search />
                  </el-icon>
                </template>
              </el-input>
              <el-button type="primary" icon="Plus" @click="addDomainClick">新增数据域</el-button>
            </div>
            <CollapseView :list="collapseList" @add="collapseAdd" @mainDeletion="collapseMainDeletion" @childDelete="collapseDelete"
              @change="collapseChange" @view="collapseView" @default="collapseDefault" />
          </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 v-model="editDomainForm" type="edit" v-if="rightType === 'domain'" />
                <AddRules v-model="editRulesForm" type="edit" 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 v-model="addDomainForm" type="add" @cancel="modalPopCancel" @confirm="AddDomainConfirm"
          v-if="modalPopData.type === 'domain'" />
        <AddRules v-model="addRulesForm" type="add" @cancel="modalPopCancel" @confirm="AddDomainConfirm"
          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>