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

const splitNum = ref(0.31) // 左右分割比例
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) {
      editForm.value = {
        dataarea_id: collapseList.value[0].id,
        ...collapseList.value[0].secretkeyList[0]
      }
    } else {
      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 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">
    <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="queryParams.dataarea" 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" :encryptionDict="encryptionList" type="edit" @cancel="modalPopCancel"
                  @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" width="800px" title="新增加密算法" @cancel="modalPopCancel">
      <template #content>
        <formModule v-if="modalPopShow" :itemData="addForm" :encryptionDict="encryptionList" 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>