<script setup name="Algorithm">
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 formModule from './modules/formModule.vue'
import ModalPop from "@/components/EditPop/ModalPop.vue"

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

const splitNum = ref(0.31) // 左右分割比例
const collapseList = ref([])
const rulenameList = ref([]) // 规则名称列表
const searchValue = ref('')

const data = reactive({
  addForm: {},
  editForm: {},
  delData: {
    id: ''
  }
});

const { addForm, editForm, delData } = toRefs(data);

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

const modalPopShow = ref(false)

// 获取数据
const getCollapse = (type) => {
  query({ dataarea: searchValue.value }).then(res => {
    rulenameList.value = []
    const { data } = res
    collapseList.value = data.map(item => {
      const list = item.ruleList.map(itemTwo => {
        rulenameList.value.push(itemTwo.rulename)
        return {
          ...itemTwo,
          name: itemTwo.rulename,
          defaulttype: itemTwo.defaluttype,
          isCheck: false,
          isDelete: true,
        }
      })
      return {
        ...item,
        list: list,
        isAdd: true,
        isView: false,
        isDelete: false,
      }
    })
    if (type === 'edit') return
    if (collapseList.value.length <= 0) return
    if (collapseList.value[0].ruleList.length > 0) {
      editForm.value = collapseList.value[0].ruleList[0]
    } else {
      const { id } = collapseList.value[0]
      editForm.value = {
        dataarea_id: id
      }
    }

  })
}

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

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

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

// 删除回调
const modalConfirm = () => {
  const { id } = delData.value
  const data = {
    ruleId: id
  }
  del(data).then(res => {
    const { flag } = res
    if (flag) {
      ElMessage.success('删除成功')
      getCollapse()
      modalData.show = false
    } else {
      ElMessage.error('删除失败')
    }
  })
}

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

// 算法确认
const formModuleConfirm = (val) => {
  getCollapse(val)
  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%;display: flex;flex-direction: column;">
            <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>
            <el-scrollbar style="width: 100%;flex: 1;padding-right: 38px;">
              <CollapseView :list="collapseList" @add="collapseAdd" @childDelete="collapseDelete"
              @change="collapseChange" />
            </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;">脱敏规则</span>
              </div>
              <div class="right-content">
                <formModule :itemData="editForm" :nameList="rulenameList" type="edit" @confirm="formModuleConfirm" />
              </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 :itemData="addForm" :nameList="rulenameList" 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>