<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({
  formData: {
    name: "",
  },
  queryParams: {
    dataarea: ''
  },
  delData: {
    id: ''
  }
});

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

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

const modalPopShow = ref(false)

// 获取数据
const getCollapse = () => {
  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 (collapseList.value.length <= 0) return
    if (collapseList.value[0].secretkeyList.length > 0) {
      formData.value = {
        dataarea_id: collapseList.value[0].id,
        ...collapseList.value[0].secretkeyList[0]
      }
    } else {
      formData.value = {
        dataarea_id: collapseList.value[0].id
      }
    }
  })
}

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

// 新增加密算法
const collapseAdd = (item) => {
  // console.log('添加事件', item)
  const { id } = item
  formData.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)
  formData.value = {
    ...item.item
  }
}

// 算法确认
const formModuleConfirm = (item) => {
  console.log('新增算法', item)
  getCollapse()
  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%;">
            <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>
            <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 :itemData="formData" :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" title="编辑器" @cancel="modalPopCancel">
      <template #content>
        <formModule :itemData="formData" :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>