<script setup name="Discover">
import { onMounted, ref, toRefs } from 'vue'
import { Split } from 'view-ui-plus';

import CollapseView from '@/components/CollapseView/index.vue'
import AddEncryption from './modules/AddEncryption.vue' // 加密表单
import AddKey from './modules/AddKey.vue' // 密钥表单
import ModalPop from "@/components/EditPop/ModalPop.vue"

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

const data = reactive({
  editEncryptionForm: {
    name: "",
  },
  addEncryptionForm: {
    name: "",
  },
  editKeyForm: {
    name: "",
  },
  addKeyForm: {
    name: "",
  }
});

const { editEncryptionForm, addEncryptionForm, editKeyForm, addKeyForm } = toRefs(data);
const rightType = ref('encryption') // 右侧展示类型
const modalData = reactive({
  show: false,
  text: '',
  icon: 'error',
  cancel: true,
  type: ''
})

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

// 获取数据
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: false,
        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 = 'encryption'
  addEncryptionForm.value = {}
  modalPopData.show = true
}

// 新增密钥
const collapseAdd = (item) => {
  console.log('新增密钥', item)
  modalPopData.title = '新增密钥'
  modalPopData.type = 'key'
  addKeyForm.value = {}
  modalPopData.show = true
}

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

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

// 提示回调
const modalConfirm = () => {
  if (modalData.type === 'encryption') {
    console.log('删除加密确认')
  } else if (modalData.type === 'key') {
    console.log('删除密钥确认')
  } else if (modalData.type === 'default') {
    console.log('设置默认项')
  }
  modalData.show = false
}

// 加密查看
const collapseView = (item) => {
  editEncryptionForm.value = item
  rightType.value = 'encryption'
}

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

// 新增确认
const AddDomainConfirm = (item) => {
  if (modalPopData.type === 'encryption') {
    console.log('新增加密确认', item)
  } else if (modalPopData.type === 'key') {
    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;flex-direction: row-reverse;">
              <el-button icon="Upload" type="success">恢复</el-button>
              <el-button class="mr10" icon="Download" type="primary">备份</el-button>
            </div>
            <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" />
          </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 === 'encryption' ? '加密定义' : '密钥定义' }}</span>
              </div>
              <div class="right-content">
                <AddEncryption v-model="editEncryptionForm" type="edit" v-if="rightType === 'encryption'" />
                <AddKey v-model="editKeyForm" type="edit" v-if="rightType === 'key'" />
              </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="'850'" v-model="modalPopData.show" :title="modalPopData.title" @cancel="modalPopCancel">
      <template #content>
        <AddEncryption v-model="addEncryptionForm" type="add" @cancel="modalPopCancel" @confirm="AddDomainConfirm"
          v-if="modalPopData.type === 'encryption'" />
        <AddKey v-model="addKeyForm" type="add" @cancel="modalPopCancel" @confirm="AddDomainConfirm"
          v-if="modalPopData.type === 'key'" />
      </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>