<template>
    <hc-new-dialog is-table widths="90%" :footer="false" :show="isShow" :title="`${dataTitle} - 文件元数据`" @close="addModalClose">
        <table class="hc-csc-meta-table" border="1">
            <template v-for="(item, index) in metaDataTable" :key="index">
                <tr v-if="item.type === 2" class="hc-csc-meta-table-tr1">
                    <td colspan="2" class="hc-csc-meta-table-td">{{ item.title }}</td>
                </tr>
                <tr v-else class="hc-csc-meta-table-tr">
                    <td class="hc-csc-meta-table-td name">{{ item.title }}</td>
                    <td class="hc-csc-meta-table-td val">{{ item.val }}</td>
                </tr>
            </template>
        </table>
    </hc-new-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getArrValue, isNullES } from 'js-fast-way'
import { sql_find, sql_table } from '../../utils/dbConnect'
const props = defineProps({
    ids: {
        type: [String, Number],
        default: '',
    },
    title: {
        type: [String, Number],
        default: '',
    },
})

//事件
const emit = defineEmits(['close'])

defineOptions({
    name: 'QueryMetaData',
})

//双向绑定
const isShow = defineModel('modelValue', {
    default: false,
})

//监听
const dataId = ref(props.ids)
const dataTitle = ref(props.title)
watch(() => [
    props.ids,
    props.title,
], ([id, title]) => {
    dataId.value = id
    dataTitle.value = title
}, { deep: true })

//监听
watch(isShow, (val) => {
    if (val) getTableData()
})

const metaDataTable = ref([
    { type: 2, title: '来源', val: '' },
    { type: 1, title: '全宗名称', val: '/' },
    { type: 1, title: '立档单位名称', val: '/' },
    { type: 1, title: '电子文件号', val: '/' },
    { type: 2, title: '文件联', val: '' },
    { type: 1, title: '目录文件', val: '/' },
    { type: 1, title: '文件件数', val: '/' },
    { type: 1, title: '文件页数', val: '/' },
    { type: 1, title: '元数据目录文件', val: '/' },
    { type: 1, title: '验证码', val: '/' },
    { type: 2, title: '内容描述', val: '' },
    { type: 1, title: '题名', val: '/' },
    { type: 1, title: '关键词', val: '/' },
    { type: 1, title: '摘要', val: '/' },
    { type: 1, title: '生成方式', val: '/' },
    { type: 1, title: '责任者', val: '/' },
    { type: 1, title: '文件创建日期', val: '/' },
    { type: 2, title: '组件', val: '' },
    { type: 1, title: 'IP地址', val: '/' },
    { type: 1, title: '桩号', val: '/' },
    { type: 1, title: '上传时间', val: '/' },
    { type: 2, title: '文件标识码', val: '' },
    { type: 1, title: '文号', val: '/' },
    { type: 1, title: '表单标识码', val: '/' },
    { type: 2, title: '竣工图', val: '' },
    { type: 1, title: '图号', val: '/' },
    { type: 1, title: '图幅', val: '/' },
    { type: 1, title: '图表来源', val: '/' },
    { type: 1, title: '引用变更令编号', val: '/' },
    { type: 2, title: '照片文件', val: '' },
    { type: 1, title: '主题', val: '/' },
    { type: 1, title: '拍摄时间', val: '/' },
    { type: 1, title: '拍摄地点', val: '/' },
    { type: 1, title: '摄影者', val: '/' },
    { type: 1, title: '背景', val: '/' },
    { type: 1, title: '分组号', val: '/' },
    { type: 1, title: '组内照片编号', val: '/' },
    { type: 1, title: '水平分辨率', val: '/' },
    { type: 1, title: '垂直分辨率', val: '/' },
    { type: 1, title: '保管期限', val: '/' },
    { type: 1, title: '格式信息', val: '/' },
    { type: 2, title: '电子属性', val: '' },
    { type: 1, title: '位置', val: '/' },
    { type: 1, title: '计算机文件名', val: '/' },
    { type: 1, title: '计算机文件大小', val: '/' },
    { type: 2, title: '数字化属性', val: '' },
    { type: 1, title: '扫描分辨率', val: '/' },
    { type: 1, title: '扫描色彩模式', val: '/' },
    { type: 2, title: '电子签名', val: '' },
    { type: 1, title: '签名类型', val: '/' },
    { type: 1, title: '签名时间', val: '/' },
    { type: 1, title: '签名人', val: '/' },
    { type: 1, title: '建设项目', val: '/' },
    { type: 2, title: '业务事项', val: '' },
    { type: 1, title: '单位工程', val: '/' },
    { type: 1, title: '分部工程', val: '/' },
    { type: 1, title: '分项工程', val: '/' },
    { type: 1, title: '单位工程编码', val: '/' },
    { type: 1, title: '分部工程编码', val: '/' },
    { type: 1, title: '分项工程编码', val: '/' },
    { type: 2, title: '责任者', val: '' },
    { type: 1, title: '责任者名称', val: '/' },
    { type: 1, title: '个人职位', val: '/' },
    { type: 1, title: '责任者手机号', val: '/' },
    { type: 2, title: '关系实体', val: '' },
    { type: 1, title: '关系标识', val: '/' },
    { type: 1, title: '关系类型', val: '/' },
    { type: 1, title: '关系', val: '/' },
    { type: 1, title: '相关实体标识', val: '/' },
])

//获取表格数据
const tableLoading = ref(false)
const getTableData = async () => {
    const id = dataId.value
    if (isNullES(id)) return
    //获取文件元数据
    const { code: code1, data: data1 } = await sql_find(`select * from u_metadata_file where file_id = '${id}'`)
    const fileData = code1 === 200 ? data1 : {}
    //获取节点类型
    const { code: code2, data: data2 } = await sql_find(`select atc.storage_type from u_archive_file uaf left join m_archive_tree_contract atc on uaf.node_id = atc.id where uaf.id = '${id}'`)
    const storage_type = code2 === 200 ? data2.storage_type : 3
    let treeType = getStorageType(storage_type)
    //获取文件字段
    const { code: code3, data: data3 } = await sql_table('select * from u_metadata_classification')
    const fieldData = code3 === 200 ? getArrValue(data3) : []
    //判断是否包含
    let metadataList = []
    for (let i = 0; i < fieldData.length; i++) {
        if (fieldData[i].file_storage_type.indexOf(treeType) !== -1) {
            // console.log(fieldData[i].code)
            fieldData[i].keyValue = fileData[fieldData[i].field_key]
            // console.log(fieldData[i])
            let r = { title: '', val:'' }
            r.title = fieldData[i].container_name
            r.val = fileData[fieldData[i].field_key] ?? '/'
            if (r.title.indexOf('生成方式') !== -1) {
                // eslint-disable-next-line eqeqeq
                if (r.val == 1) {
                    r.val = '原生'
                } else {
                    r.val = '数字化'
                }
            }
            if (r.title.indexOf('密级') !== -1) {
                // eslint-disable-next-line eqeqeq
                if (r.val == 1) {
                    r.val = '机密'
                    // eslint-disable-next-line eqeqeq
                } else if (r.val == 2) {
                    r.val = '绝密'
                } else {
                    r.val = '秘密'
                }
            }
            metadataList.push(r)
        }
    }
    //数据存在
    if (metadataList.length > 0) {
        metaDataTable.value = metadataList
    }
}

//获取节点类型
const getStorageType = (storage_type) => {
    let treeType = 'c'
    if (storage_type == 1) {
        treeType = 'a'
    } else if (storage_type == 2) {
        treeType = 'b'
    } else if (storage_type == 3) {
        treeType = 'c'
    } else if (storage_type == 4) {
        treeType = 'd'
    } else if (storage_type == 5) {
        treeType = 'e'
    } else if (storage_type == 6) {
        treeType = 'f'
    } else if (storage_type == 7) {
        treeType = 'g'
    } else if (storage_type == 8) {
        treeType = 'h'
    } else if (storage_type == 9) {
        treeType = 'i'
    }
    return treeType
}

//关闭弹窗
const addModalClose = () => {
    isShow.value = false
    emit('close')
}
</script>

<style lang="scss">
.hc-csc-meta-table {
    border-spacing: 0;
    border: 1px solid #F2F2F2;
    border-collapse: collapse;
    width: 100%;
    .hc-csc-meta-table-tr {
        position: relative;
        background: white;
        color: #50545e;
        transition: background-color .25s ease;
        &:hover {
            background: #e6f2ff;
        }
    }
    .hc-csc-meta-table-tr1 {
        position: relative;
        color: #1a1a1a;
        text-align: center;
        font-weight: bold;
        background-color: #F4F4F4;
    }
    .hc-csc-meta-table-td {
        text-align: left;
        padding: 6px 10px;
        font-size: 13px;
        border: 1px solid #F2F2F2;
        &.name {
            width: 200px;
            color: #787878;
        }
        &.val {
            color: #787878;
        }
    }
}
</style>
