<script setup name="Discover">
import { onMounted, ref, toRefs } from 'vue'
import { ElMessage } from 'element-plus'
import CollapseView from '@/components/CollapseView/index.vue'
import AddDomain from './modules/AddDomain.vue' // 数据域表单
import AddRules from './modules/AddRules.vue' // 发现规则表单
import ModalPop from "@/components/EditPop/ModalPop.vue"
import pageIcon from '@/assets/images/ruleConfig/DiscoverPage.png'
import { query, delDataArea, delRule, setdefault } from '@/api/ruleConfig/discover'

const fatherIndex = ref(0) // 父级索引
const currentId = ref('') // 当前选择项id
const collapseList = ref([]) // 数据域数据

const data = reactive({
  domainForm: {

  },
  rulesForm: {

  },
  queryParams: {
    areaType: "0",
    rulename: ""
  },
  delData: {
    id: '',
    dataarea: ''
  }
});

const { domainForm, rulesForm, queryParams, delData } = toRefs(data);
const rightType = ref('domain') // 右侧展示类型
const modalData = reactive({
  show: false,
  text: '',
  icon: 'error',
  cancel: true,
  type: ''
})

const modalPopData = reactive({
  show: false,
  title: '',
  type: 'domain'
})

// 获取数据
const getCollapse = () => {
  query(queryParams.value).then(res => {
    const { data } = res
    collapseList.value = data.map(item => {
      const list = item.data.map(itemTwo => {
        return {
          ...itemTwo,
          id: itemTwo.ruleid,
          isCheck: true,
          isDelete: true,
        }
      })
      return {
        ...item.dataarea,
        list: list,
        isAdd: true,
        isView: true,
        isDelete: true,
      }
    })
    if (collapseList.value.length <= 0) {
      fatherIndex.value = null
      domainForm.value = {}
      rightType.value = 'domain'
      return
    }
    fatherIndex.value = 0
    domainForm.value = collapseList.value[0]
    currentId.value = domainForm.value.id
    rightType.value = 'domain'
  })
}

// 新增关闭
const modalPopCancel = () => {
  modalPopData.show = false
}

// 新增数据域
const addDomainClick = () => {
  console.log('新增数据域')
  modalPopData.title = '新增数据域'
  modalPopData.type = 'domain'
  modalPopData.show = true
}

// 新增发现规则
const collapseAdd = (item) => {
  console.log('新增发现规则', item)
  const { id, dataarea } = item
  rulesForm.value = {}
  rulesForm.value = {
    dataarea_id: id,
    type: dataarea
  }
  modalPopData.title = '新增发现规则'
  modalPopData.type = 'rules'
  modalPopData.show = true
}

// 删除发现规则
const collapseDelete = (item) => {
  const { name } = item
  delData.value = item
  modalData.type = 'rules'
  modalData.cancel = true
  modalData.icon = 'error'
  modalData.text = '删除后无法恢复，是否确认删除[' + name + ']?'
  modalData.show = true
}

// 删除数据域
const collapseMainDeletion = (item) => {
  const { dataarea } = item
  delData.value = item
  modalData.type = 'domain'
  modalData.cancel = false
  modalData.icon = 'error'
  modalData.text = '删除后无法恢复，是否确认删除[' + dataarea + ']?'
  modalData.show = true
}

// 提示回调
const modalConfirm = () => {
  if (modalData.type === 'domain') {
    const { id, dataarea } = delData.value
    const data = {
      areaid: id,
      areaname: dataarea
    }
    delDataArea(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('删除成功')
        getCollapse()
      } else {
        ElMessage.error('删除失败')
      }
    })
  } else if (modalData.type === 'rules') {
    const { ruleid, name, tid } = delData.value
    const data = {
      ruleid: ruleid,
      rulename: name,
      tid: tid
    }
    delRule(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('删除成功')
        getCollapse()
      } else {
        ElMessage.error('删除失败')
      }
    })
  } else if (modalData.type === 'default') {
    const { ruleid, name } = delData.value
    const data = {
      ruleid: ruleid,
      rulename: name,
    }
    setdefault(data).then(res => {
      const { flag } = res
      if (flag) {
        ElMessage.success('设置成功')
        getCollapse()
      } else {
        ElMessage.error('设置失败')
      }
    })
  }
  modalData.show = false
}

// 数据域查看
const viewClick = (val) => {
  const { index, item } = val
  fatherIndex.value = index
  domainForm.value = item
  currentId.value = domainForm.value.id
  rightType.value = 'domain'
}

// 规则点击监听
const collapseChange = (item) => {
  const { id, dataarea } = item.parentItem
  rulesForm.value = {
    ...item.item,
    ...{
      dataarea_id: id,
      type: dataarea
    }
  }
  currentId.value = rulesForm.value.ruleid
  rightType.value = 'rules'
}

// 发现规则设置默认项
const collapseDefault = (item) => {
  const { name } = item
  delData.value = item
  modalData.type = 'default'
  modalData.cancel = false
  modalData.icon = 'error'
  modalData.text = '确定设置[' + name + ']为默认规则?'
  modalData.show = true
}

// 新增确认
const AddFormConfirm = (item) => {
  if (modalPopData.type === 'domain') {
    getCollapse()
  } else if (modalPopData.type === 'rules') {
    getCollapse()
  }
  modalPopData.show = false
}

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

</script>
<template>
  <div class="app-container scroller">
    <div class="app-container__body">
      <IconTitle :icon="pageIcon" title="发现规则" />
      <div class="box">
        <div class="left">
          <div class="searchBox">
            <el-input v-model="queryParams.rulename" 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>
            <el-button type="primary" icon="Plus" class="addBut" @click="addDomainClick">新增数据域</el-button>
          </div>
          <div class="collapseView">
            <CollapseView :list="collapseList" :fatherIndex="fatherIndex" :currentId="currentId" @add="collapseAdd" @mainDeletion="collapseMainDeletion"
                @childDelete="collapseDelete" @change="collapseChange" @view="viewClick" @default="collapseDefault" />
          </div>
        </div>
        <div class="right">
          <div class="line"></div>
          <div style="padding: 26px 28px 0 28px;position: relative;flex: 1;">
            <DividerTitle :title-name="rightType === 'domain' ? '数据域' : '发现规则'" />
            <div class="right-content" v-if="collapseList.length > 0">
              <AddDomain :itemData="domainForm" type="edit" @confirm="AddFormConfirm" v-if="rightType === 'domain'" />
              <AddRules :itemData="rulesForm" type="edit" @confirm="AddFormConfirm" v-if="rightType === 'rules'" />
            </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="modalPopData.title" v-model="modalPopData.show" :before-close="modalPopCancel" :width="modalPopData.type === 'domain'? '900' : '750'">
      <div v-if="modalPopData.type === 'domain'" style="padding: 30px 120px;">
        <AddDomain type="add" @cancel="modalPopCancel" @confirm="AddFormConfirm" />
      </div>
      <div v-if="modalPopData.type === 'rules'" style="padding: 30px;">
        <AddRules type="add" :itemData="rulesForm" @cancel="modalPopCancel" @confirm="AddFormConfirm" />
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.app-container__body {
  padding: 20px 0 0 0;
  .box {
    display: flex;
    flex: 1;
    overflow: hidden;
    .left {
      margin-right: 30px;
      width: 565px;
      overflow: auto;
      height: 100%;
      display: flex;
      flex-direction: column;
      .searchBox{
        display: flex;
        width: 100%;
        .searchBut {
          height: 40px;
          background: var(--el-color-primary);
          color: var(--el-color-white);
          border-radius: 0px 5px 5px 0px;
        }
        .addBut{
          height: 40px;
          margin-left: 10px;
          background: rgba(0, 189, 207, 1);
          border-color: rgba(0, 189, 207, 1);
        }
      }
      .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 {
            border-left: 1px solid #ebebeb;
            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;
            background: #f5fcff;
          }
          .el-input {
            height: 40px;
            line-height: 40px;
          }
          .is-disabled {
            background: #f5fcff;
            .el-input__wrapper,.el-textarea__inner {
              background: #f5fcff;
              box-shadow: none;
              border-radius: 0;
            }
            .el-input__inner,.el-textarea__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>