<script setup name="CalculationMethod">
import { onMounted, ref, toRefs } from 'vue'
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, queryenc, del } from '@/api/ruleConfig/calculationMethod'

const fatherIndex = ref(0) // 父级索引
const collapseList = ref([])
const encryptionList = ref([]) // 加密规则列表

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

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

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

const modalPopShow = ref(false)

// 获取数据
const getCollapse = (type) => {
  query(queryParams.value).then(res => {
    const { data } = res
    collapseList.value = data.map(item => {
      const list = item.secretkeyList.map(itemTwo => {
        return {
          ...itemTwo,
          name: itemTwo.enc_name,
          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].secretkeyList.length > 0) {
      fatherIndex.value = 0
      editForm.value = {
        dataarea_id: collapseList.value[0].id,
        ...collapseList.value[0].secretkeyList[0]
      }
    } else {
      fatherIndex.value = null
      editForm.value = {
        dataarea_id: collapseList.value[0].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 { name } = item
  delData.value = item
  modalData.show = true
  modalData.icon = 'error'
  modalData.text = '确认删除[' + name + ']?'
}

// 删除回调
const modalConfirm = () => {
  const { id } = delData.value
  const data = {
    relationid: id
  }
  del(data).then(res => {
    const { flag } = res
    if (flag) {
      ElMessage.success('删除成功')
      getCollapse()
      modalData.show = false
    } else {
      ElMessage.error('删除失败')
    }
  })
}
// 一级点击
const collapseView = (index) => {
  console.log(index)
  fatherIndex.value = index
}

// 点击监听
const collapseChange = (item) => {
  // console.log('修改事件', item)
  editForm.value = {
    ...item.item
  }
}

// 算法确认
const formModuleConfirm = (val) => {
  // console.log('新增算法', val)
  getCollapse(val)
  modalPopShow.value = false
}

// 查询加密规则
const getQueryenc = () => {
  queryenc({}).then((res) => {
    const { data } = res;
    encryptionList.value = data.map((item) => {
      const { encryption_name, id } = item;
      return {
        value: id,
        label: encryption_name,
      };
    });
  });
};


onMounted(() => {
  getQueryenc()
  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="queryParams.dataarea" 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"
              @change="collapseChange" @view="collapseView" />
          </div>
        </div>
        <div class="right">
          <div class="line"></div>
          <div style="padding: 26px 28px 0 28px;position: relative;flex: 1;">
            <DividerTitle title-name="加密算法" />
            <div class="right-content" v-if="collapseList.length > 0">
              <formModule :itemData="editForm" :encryptionDict="encryptionList" type="edit" @cancel="modalPopCancel"
                  @confirm="formModuleConfirm" />
            </div>
            <el-empty description="暂无数据" v-if="collapseList.length <= 0"
            style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);"></el-empty>
          </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="800">
      <div style="padding: 30px;">
        <formModule v-if="modalPopShow" :itemData="addForm" :encryptionDict="encryptionList" 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 {
      display: flex;
      flex-direction: column;
      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>