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

import CollapseView from '@/components/CollapseView/index.vue'
import formModule from './modules/formModule.vue'
import ModalPop from "@/components/EditPop/ModalPop.vue"

import { query } from '@/api/ruleConfig/algorithm.js'

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

const data = reactive({
  formEdit: {
    name: "",
  },
  formAdd: {
    name: "",
  }
});

const { formEdit, formAdd } = toRefs(data);

const modalData = reactive({
  show: false,
  text: '',
  icon: 'error',
  cancel: true
})

const modalPopShow = ref(false)

// 获取数据
const getCollapse = () => {
  query({dataarea: searchValue.value}).then(res => {
    
  })
  // collapseList.value = data.map(item => {
  //   const list = item.list.map(itemTwo => {
  //     return {
  //       ...itemTwo,
  //       isCheck: false,
  //       isDelete: true,
  //     }
  //   })
  //   return {
  //     ...item,
  //     list: list,
  //     isAdd: true,
  //     isView: false,
  //     isDelete: false,
  //   }
  // })
}

// 新增算法关闭
const modalPopCancel = () => {
  modalPopShow.value = false
}

// 新增算法
const collapseAdd = (item) => {
  console.log('添加事件', item)
  formAdd.value = {}
  modalPopShow.value = true
}

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

// 删除回调
const modalConfirm = () => {
  modalData.show = false
}

// 点击监听
const collapseChange = (item) => {
  formEdit.value = item
}

// 算法确认
const formModuleConfirm = (item) => {
  console.log('新增算法', item)
  modalPopShow.value = 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%;">
            <el-input class="mb20" v-model="searchValue" placeholder="脱敏规则搜索">
              <template #suffix>
                <el-icon style="vertical-align: middle;cursor: pointer;" @click="getCollapse">
                  <search />
                </el-icon>
              </template>
            </el-input>
            <CollapseView :list="collapseList" @add="collapseAdd" @childDelete="collapseDelete" @change="collapseChange" />
          </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;">脱敏规则</span>
              </div>
              <div class="right-content">
                <formModule v-model="formEdit" type="edit" />
              </div>
            </div>
          </div>
        </template>
      </Split>
    </div>
    <!-- 提示框 -->
    <Modal v-model="modalData.show" :icon="modalData.icon" :cancel="modalData.cancel" :text="modalData.text"
      @confirm="modalConfirm"></Modal>

    <!-- 新增算法 -->
    <ModalPop v-model="modalPopShow" title="编辑器" @cancel="modalPopCancel">
      <template #content>
        <formModule v-model="formAdd" type="add" @cancel="modalPopCancel" @confirm="formModuleConfirm" />
      </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;
      .openEditor {
        position: absolute;
        bottom: 0;
        right: 0;
      }
    }
    &-btn {
      padding: 20px 20px 20px 100px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>