<script setup name="AssetLibrary">
import { ref, toRefs, reactive, getCurrentInstance, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import QueryForm from './QueryForm.vue'
import FormGroup from './modules/formGroup.vue'
import img from '@/assets/images/assetLibrary/index.js'
import { queryAll, checkDatasystemUsed, del } from '@/api/assetLibrary/index'
import { getCueryCharset } from "@/api/assetLibrary/dict";

const emit = defineEmits(['page'])

const { proxy } = getCurrentInstance()

const data = reactive({
  form: {
    name: ''
  },
  queryParams: {
    sysname: '',
    dbtype: '',
    ip: '',
    userId: '1',
    projectId: ''
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

const modalData = reactive({
  show: false,
  text: ''
})

// 数据源信息
const dataSource = reactive({
  current: 0, // 当前索引
  currentId: '', // 当前id
  currentFlag: '',
  list: [], // 数据源列表
  del: {
    id: '',
    sysname: ''
  }
})

// 新增
const handleAdd = () => {
  emit('page', 'add')
}

// 筛选查询
const onQuery = () => {
  console.log('onQuery')
  getList()
}
// 查询组件事件重置
const onReset = (formQuery) => {
  console.log('onReset')
  formQuery.resetFields()
  getList()
}

// 预览
const onPreview = (index) => {
  dataSource.current = index
  dataSource.currentId = dataSource.list[dataSource.current].id
  dataSource.currentFlag = dataSource.list[dataSource.current].flag
}

// 删除
const onDelete = (val) => {
  const { id, sysname } = val
  checkDatasystemUsed({ id: id }).then(res => {
    console.log(res)
    const flag = { res }
    if (flag) {
      dataSource.current = 0
      dataSource.del.id = id
      dataSource.del.sysname = sysname
      modalData.text = `删除后无法恢复，是否确认删除[${sysname}]？`
      modalData.show = true
    }
  })
}

// 确认删除回调
const modalConfirm = () => {
  del(dataSource.del).then(res => {
    const flag = { res }
    if (flag) {
      ElMessage.success({
        message: '删除成功',
        type: 'success',
      })
      getList()
    }
  })
  modalData.show = false
}

// 编辑成功回调
const onConfirm = () => {
  // console.log('编辑成功')
  getList()
}

// 列表查询
const getList = () => {
  queryAll(queryParams.value).then(res => {
    // console.log(res)
    dataSource.list = res.data
    dataSource.currentId = dataSource.list[dataSource.current].id
    dataSource.currentFlag = dataSource.list[dataSource.current].flag
  })
}

onMounted(() => {
  getCueryCharset()
  getList()
})
</script>

<template>
  <div class="app-container">
    <PageTitle>
      <template #title>
        资产库
      </template>
      <template #buttons>
        <el-button type="primary" icon="Plus" @click="handleAdd">新增数据源</el-button>
      </template>
    </PageTitle>
    <div class="app-container__body">
      <query-form ref="QueryFormRef" v-model="queryParams" @query="onQuery" @reset="onReset" />
      <el-scrollbar>
        <div class="flex-content">
          <el-card class="box-card" :class="dataSource.current === index ? 'active' : ''"
            :body-style="{ padding: '0px !important' }" v-for="(item, index) in dataSource.list" :key="index">
            <img class="img" :src="img[item.dbtype]" alt="">
            <div class="title">
              <div class="title-content">{{ item.sysname }}</div>
            </div>
            <div class="mask">
              <div class="maskbutton">
                <div class="maskbutton-item" @click="onDelete(item)">
                  <el-icon :size="18">
                    <delete />
                  </el-icon>
                  <br>
                  <span>删除</span>
                </div>
                <div class="maskbutton-item" @click="onPreview(index)" v-if="dataSource.current !== index">
                  <el-icon :size="18">
                    <View />
                  </el-icon>
                  <br>
                  <span>预览</span>
                </div>
              </div>
            </div>
          </el-card>
        </div>
      </el-scrollbar>
      <div class="mt20" v-if="dataSource.list.length > 0" style="padding-bottom: 20px;">
        <div class="mb20">数据源明细：{{ form.name }}</div>
        <FormGroup :id="dataSource.currentId" :flag="dataSource.currentFlag" page="edit" @confirm="onConfirm" />
      </div>
    </div>
    <Modal v-model="modalData.show" icon="error" :cancel="true" :text="modalData.text" @confirm="modalConfirm"></Modal>
  </div>
</template>

<style lang="scss" scoped>
.flex-content {
  display: flex;
  height: 205px;
  .box-card {
    position: relative;
    flex-shrink: 0;
    margin-right: 20px;
    height: 138px;
    width: 188px;
    border-radius: 10px;
    -webkit-transform: translateY(0);
    transform: translateY(0);
    -webkit-transition: all 0.1s;
    transition: all 0.1s;
    :deep(.el-card__body) {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
    }
    .img {
      width: 100%;
      border-radius: 10px 10px 0 0;
    }

    .title {
      width: 100%;
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #7a8495;
      &-content {
        padding-left: 16px;
        padding-right: 16px;
        font-size: 14px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    .mask {
      position: absolute;
      z-index: -1;
      opacity: 0;
      -webkit-transition: all 0.1s;
      transition: all 0.1s;
      width: 100%;
      height: 104px;
      left: 0;
      top: 0;
      background: #191919;
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      justify-content: center;
      border-radius: 10px 10px 0 0;
      .maskbutton {
        color: #fff;
        width: 100%;
        position: absolute;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-around;
        &-item {
          width: 70px;
          font-size: 14px;
          cursor: pointer;
          text-align: center;
          color: #fff;
        }
      }
    }
  }
  .active {
    .title {
      background: #2c9ef7;
      color: #fff;
    }
  }
  .box-card:hover {
    width: 250px;
    height: 190px;
    margin: 0 16px;
    .mask {
      opacity: 1;
      width: 100%;
      height: 140px;
      display: block;
      position: absolute;
      z-index: 50;
    }
    .title {
      &-content {
        font-size: 18px;
      }
    }
  }
  .box-card:last-child {
    margin-right: 0;
  }
}
</style>