<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 collapseList = ref([])
const rulenameList = ref([]) // 规则名称列表
const fatherIndex = ref(0) // 父级索引
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 => {
    const { data } = res
    collapseList.value = data.map(item => {
      const list = item.ruleList.map(itemTwo => {
        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) {
      fatherIndex.value = 0
      editForm.value = collapseList.value[0].ruleList[0]
    } else {
      fatherIndex.value = null
      const { id } = collapseList.value[0]
      editForm.value = {
        dataarea_id: id
      }
    }
  })
}

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

// 一级点击
const collapseView = (index) => {
  console.log(index)
  fatherIndex.value = index
}

// 新增算法
const collapseAdd = (item) => {
  // console.log('添加事件', item)
  const { id } = item
  addForm.value = {
    dataarea_id: id
  }
  rulenameList.value = []
  item.list.forEach(itemLi => {
    rulenameList.value.push(itemLi.rulename)
  })
  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 = (itemData) => {
  rulenameList.value = []
  const { parentItem, item } = itemData
  parentItem.list.forEach(itemLi => {
    rulenameList.value.push(itemLi.rulename)
  })
  editForm.value = item
}

// 算法确认
const formModuleConfirm = (val) => {
  getCollapse(val)
  modalPopShow.value = false
}

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

</script>
<template>
  <div class="app-container scroller">
    <div class="app-container__body">
      <div class="pageTitle">
        <img class="icon" src="@/assets/images/ruleConfig/algorithmPage.png" alt="">
        <span>脱敏算法</span>
      </div>
      <div class="box">
        <div class="left">
          <el-input v-model="searchValue" placeholder="脱敏规则搜索" style="width: 369px;margin-bottom: 15px;">
            <template #prefix>
              <el-icon>
                <search />
              </el-icon>
            </template>
            <template #append>
              <el-button icon="search" class="searchBut" @click="getCollapse">搜索</el-button>
            </template>
          </el-input>
          <div class="collapseView">
            <CollapseView :list="collapseList" :fatherIndex="fatherIndex" :currentId="editForm.id" @add="collapseAdd"
              @childDelete="collapseDelete" @view="collapseView" @change="collapseChange" />
          </div>
        </div>
        <div class="right">
          <div class="line"></div>
          <div style="padding: 26px 28px 0 28px;">
            <DividerTitle title-name="脱敏规则" />
            <div class="right-content">
              <formModule :itemData="editForm" :nameList="rulenameList" type="edit" @confirm="formModuleConfirm" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 提示框 -->
    <Modal v-model="modalData.show" :icon="modalData.icon" :cancel="modalData.cancel" :text="modalData.text"
      @confirm="modalConfirm"></Modal>

    <!-- 新增算法 -->
    <el-dialog title="新增算法" v-model="modalPopShow" :before-close="modalPopCancel" width="900">
      <div style="padding: 30px 120px;">
        <formModule v-if="modalPopShow" :itemData="addForm" :nameList="rulenameList" type="add" @cancel="modalPopCancel"
          @confirm="formModuleConfirm"/>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  padding: 20px 0 0 0;
  .pageTitle {
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    letter-spacing: 0px;
    line-height: 22px;
    color: rgba(53, 64, 79, 1);

    .icon {
      margin-right: 6px;
      width: 16px;
      height: 16px;
    }
  }
  .box {
    display: flex;
    flex: 1;
    overflow: hidden;
    .left {
      margin-right: 30px;
      width: 565px;
      overflow: auto;
      height: 100%;
      display: flex;
      flex-direction: column;
      .searchBut {
        height: 40px;
        background: var(--el-color-primary);
        color: var(--el-color-white);
        border-radius: 0px 5px 5px 0px;
      }
      .collapseView {
        width: 100%;
        flex: 1;
        overflow: hidden;
      }
    }
    .right {
      flex: 1;
      height: 100%;
      background: rgba(255, 255, 255, 1);
      .line {
        width: 100%;
        height: 2px;
        border-radius: 5px 5px 0px 0px;
        background: linear-gradient(
          90deg,
          rgba(33, 103, 217, 1) 0%,
          rgba(17, 200, 250, 1) 34.03%,
          rgba(33, 103, 217, 1) 64.58%,
          rgba(48, 128, 255, 1) 100%
        );
      }
      &-content {
        width: 100%;
        .openEditor {
          position: absolute;
          bottom: 0;
          right: 0;
        }
        :deep(.formBox) {
          border-radius: 5px;
          background: #ebebeb;
          border: 1px solid rgba(29, 178, 245, 1);
          overflow: hidden;
          .el-form-item__content {
            margin-left: 1px;
            background: #f5fcff;
          }
          .el-form-item__label {
            height: 40px;
            line-height: 40px;
            background: #f5fcff;
            color: rgba(148, 148, 148, 1);
          }
          .el-form-item {
            margin-bottom: 1px;
          }
          .el-input {
            height: 40px;
            line-height: 40px;
          }
          .is-disabled {
            background: #f5fcff;
            .el-input__wrapper {
              background: #f5fcff;
              box-shadow: none;
              border-radius: 0;
            }
            .el-input__inner {
              -webkit-text-fill-color: rgba(53, 64, 79, 1);
            }
          }
          .el-form-item__error {
            top: 30% !important;
            left: auto !important;
            right: 89px !important;
          }
        }
      }
      &-btn {
        padding: 20px 20px 20px 100px;
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>