<template>
    <div>
        <el-descriptions :column="1" border style="width: 600px">
            <!--项目类型-->
            <el-descriptions-item :label="$t('trials:inspection:pullImage:title:toolDownload')">
                <el-button type="text" @click="handleDownload">
                    {{ $t('trials:inspection:pullImage:button:toolDownload') }}
                </el-button>
            </el-descriptions-item>
            <!--项目编号-->
            <el-descriptions-item>
                <span slot="label">
                    {{ $t('trials:inspection:pullImage:title:hirAE') }}
                    <i class="el-icon-warning" style="cursor: pointer;"
                        :title="$t('trials:inspection:pullImage:tip:hirAE')" />
                </span>
                <el-form ref="hirAEform" :model="hirAE" label-width="120px">
                    <el-form-item :label="$t('trials:inspection:pullImage:form:hirAEName')">
                        <span>{{ hirAE.CalledAE }}</span>
                        <i class="el-icon-document-copy copy"
                            @click="copy(`${$t('trials:inspection:pullImage:form:hirAEName')}:${hirAE.CalledAE}\n${$t('trials:inspection:pullImage:form:hirAIP')}:${hirAE.IP}\n${$t('trials:inspection:pullImage:form:hirAPort')}:${hirAE.Port}`)"></i>
                    </el-form-item>
                    <el-form-item :label="$t('trials:inspection:pullImage:form:hirAIP')">
                        <span>{{ hirAE.IP }}</span>
                    </el-form-item>
                    <el-form-item :label="$t('trials:inspection:pullImage:form:hirAPort')">
                        <span>{{ hirAE.Port }}</span>
                    </el-form-item>
                </el-form>
            </el-descriptions-item>
            <el-descriptions-item>
                <span slot="label">
                    {{ $t('trials:inspection:pullImage:title:groupAE') }}
                    <i class="el-icon-warning" style="cursor: pointer;"
                        :title="$t('trials:inspection:pullImage:tip:groupAE')" />
                </span>
                <el-form ref="groupAEAEform" label-width="120px">
                    <el-form-item v-for="item of groupAEList" :key="item.Id" :label="`${item.Name}: `">
                        <span>{{ item.CallingAE }}</span>
                        <i class="el-icon-document-copy copy" @click="copy(item.CallingAE)"></i>
                    </el-form-item>
                </el-form>
            </el-descriptions-item>
        </el-descriptions>
    </div>
</template>
<script>
import { getDicomAEList } from '@/api/dicomAE.js'
import { getUser } from '@/api/admin'
import { getCommonDocument } from '@/api/dictionary'
import { downLoadFile } from '@/utils/stream.js'
export default {
    name: "byTool",
    data() {
        return {
            hirAE: {},
            groupAEList: []
        }
    },
    created() {
        this.getHirAE()
        this.getGroupAE()
    },
    computed: {
        isEN() {
            return this.$i18n.locale !== 'zh'
        },
    },
    methods: {
        copy(str) {
            this.$copyText(str).then(
                res => {
                    this.$message.success(this.$t('trials:researchRecord:message:copySuccessfully'))
                }
            ).catch(() => { this.$message.error(this.$t('trials:researchRecord:message:copyFailed')) })
        },
        async getHirAE() {
            try {
                let data = { PacsTypeEnum: 0, PageIndex: 1, PageSize: 20, }
                let res = await getDicomAEList(data)
                if (res.IsSuccess) {
                    this.hirAE = res.Result.CurrentPageData[0]
                }
            } catch (err) {
                console.log(err)
            }
        },
        async getGroupAE() {
            try {
                let res = await getUser()
                if (res.IsSuccess) {
                    this.groupAEList = res.Result.HospitalGroupList
                }
            } catch (err) {
                console.log(err)
            }
        },
        // 获取工具并下载
        async handleDownload() {
            try {
                this.downloading = true
                let res = await getCommonDocument({ Code: 'DICOMScannerAndSender' })
                if (res.IsSuccess) {
                    let row = res.Result
                    let fileName = this.isEN ? row.Name : row.NameCN;
                    let type = fileName
                        .substring(fileName.lastIndexOf('.'))
                        .toLocaleLowerCase()
                    if (!type) {
                        let extendName = row.Path
                            .substring(row.Path.lastIndexOf('.'))
                            .toLocaleLowerCase()
                        fileName += extendName
                    }
                    const a = document.createElement('a')
                    // xls类型: application/vnd.ms-excel
                    // xlsx类型：application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
                    const href = this.OSSclientConfig.basePath + row.Path
                    a.download = fileName
                    a.href = href
                    a.click()
                    URL.revokeObjectURL(href)
                    // res = await downLoadFile(this.OSSclientConfig.basePath + row.Path, fileName)
                }
                this.downloading = false
            } catch (err) {
                this.downloading = false
            }
        },
    }
}
</script>
<style lang="scss" scoped>
.copy {
    margin-left: 5px;
    cursor: pointer;

    &:hover {
        color: #409EFF;
    }
}
</style>