<template>
    <!-- 修改弹框 -->
    <el-dialog 
        width="60%"
        class="outerDialog file_modify_dialog"
        title="修改资料"
        @close="closeModal"
        append-to-body
        :close-on-click-modal="false"
        :append-to-body="true"
        v-model="changeSupervisionVisible">
        <div class="scroll_wrapper">
            <div 
                style="width:49%;overflow-y:auto;min-height: 50vh;max-height:58vh;float:left;border:1px solid #d5d5ff;margin-right:5px;">
                <p style="font-weight:bolder;background-color:#d5d5ff;padding:3px;">基本信息</p>
                <el-form ref="changeSupFrom" :model="changeSupervision" label-width="120px">
                    <el-form-item label="资料名称">
                        <el-input v-model="changeSupervision.catalogName" clearable autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="编制单位">
                        <el-input v-model="changeSupervision.estUnit" clearable autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="编制时间">
                        <el-date-picker
                        v-model="changeSupervision.estTm"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                </el-form>
            </div>
            <div
                style="width: 49%;float: left;border: 1px solid rgb(213, 213, 255);margin-right: 5px;overflow-y: auto;min-height: 50vh;max-height: 58vh;">
                <p style="font-weight:bolder;background-color:#d5d5ff;padding:3px;">多媒体信息</p>
                <el-tabs style="margin:0px 5px">
                <el-tab-pane :label="filetabName">
                    <el-row style="text-align:right;">
                        <el-button
                            :v-if="!addPicPanel?addPicPanel:addPicPanel"
                            size="large"
                            type="text"
                            :icon="!addPicPanel?'el-icon-circle-plus-outline':'el-icon-circle-close-outline'"
                            @click="addPic"
                        >{{!addPicPanel?"添加":"取消"}}</el-button>
                    </el-row>
                    <el-row v-show="addPicPanel">
                        <upload
                            ref="uploadFile"
                            :url="`/pdcApi/file/insert?bizId=`"
                            @uploadOver="removeNewSupervision"
                        ></upload>
                    </el-row>
                    <el-row>
                        <el-table border :data="fileArray" style="width: 100%;margin: 0 auto;">
                            <el-table-column header-align="center" align="center" label="文档">
                                <template #default="scope">
                                    <p>
                                        <a :href="`${hostUrl}/${scope.row.filePath}`" target="_blank" rel="noopener noreferrer">
                                            {{scope.row.fileName}}
                                        </a>
                                    </p>
                                </template>
                            </el-table-column>
                            <el-table-column header-align="center" align="center" label="操作">
                                <template #default="scope">
                                <el-button
                                    size="small"
                                    @click="removeFile(scope.row.id,scope.$index)"
                                    type="danger"
                                >删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-row>
                </el-tab-pane>
                <el-tab-pane :label="picturetabName">
                    <el-row>
                    <div style="overflow-y:auto;overflow-x:hidden;max-height:30vh;padding:10px;">
                        <div class="upload-container">
                        <div
                            v-for="(item, index) in pictureArray"
                            :key="index"
                            class="image-preview image-app-preview"
                        >
                            <div class="image-preview-wrapper">
                            <img :src="`${hostUrl}/${item.filePath}`">
                            <div class="image-preview-action">
                                <i @click="removeFile(item.id)" class="el-icon-delete"></i>
                            </div>
                            </div>
                        </div>
                        </div>
                    </div>
                    </el-row>
                </el-tab-pane>
                <el-tab-pane :label="audiotabName">
                    <el-table border :data="audioArray" style="width: 100%;margin: 0 auto;">
                    <el-table-column header-align="center" align="center" label="音频">
                        <template #default="scope">
                        <elaudio
                            style="margin-left:15%;width:100px;height:30px;"
                            :theUrl="`${hostUrl}/${scope.row.filePath}`"
                        ></elaudio>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="操作">
                        <template #default="scope">
                        <el-button
                            size="small"
                            @click="removeFile(scope.row.id,scope.$index)"
                            type="danger"
                        >删除</el-button>
                        </template>
                    </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane :label="videotabName">
                    <el-table border :data="videoArray" style="width: 100%;margin: 0 auto;">
                    <el-table-column header-align="center" align="center" label="视频">
                        <template #default="scope">
                        <video-player
                            class="vjs-custom-skin"
                            ref="videoPlayer"
                            :options="scope.row"
                            :playsinline="true"
                        ></video-player>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" label="操作">
                        <template #default="scope">
                        <el-button
                            size="small"
                            @click="removeFile(scope.row.id,scope.$index)"
                            type="danger"
                        >删除</el-button>
                        </template>
                    </el-table-column>
                    </el-table>
                </el-tab-pane>
                </el-tabs>
            </div>
        </div>
        <span slot="footer">
        <el-button type="primary" @click="submitFile">保存</el-button>
        <el-button @click="closeModal">取消</el-button>
        </span>
    </el-dialog>
</template>
<script>
import {updatelist,deleteFileById} from "@/api/duChaAPI.js";
import {showDetailsApi,addFileApi} from "@/api/InspectionReport.js";
import request from "@/utils/gw_ajax_request.js";
import { hostUrl } from "@/utils/global_variable.js";
import elaudio from "@/widgets/elAudioShort.vue";
export default {
    props: ['fileId'],  //fileId  资料id
    data(){
        return{
            changeSupervisionVisible: true,
            changeSupervisionLoading: false,
            changeSupervision: {},
            addPicPanel: false,
            gwComFileList: [],
            picturetabName: "",
            audiotabName: "",
            videotabName: "",
            filetabName: "",
            pictureArray: [],
            audioArray: [],
            videoArray: [],
            fileArray: [],
            updateIdXiuGai: "",
            pblmId: "",
            //稽察添加问题用到的参数
        }
    },
    components:{
        elaudio: elaudio,
        upload: resolve => {
            require(["../uploadFile.vue"], resolve);
        },
    },
    computed:{
        hostUrl(){
            return hostUrl
        },
        persId() {
            return localStorage.getItem("userid") ? localStorage.getItem("userid") : "1098101939614724096";
        }
    },
    mounted(){
        this.changeSup(this.fileId)
    },
    methods:{
        // 修改弹窗显示
        changeSup(id) {
            this.pictureArray = [];
            this.audioArray = [];
            this.videoArray = [];
            this.fileArray = [];
            this.updateIdXiuGai = id;
            let _this = this
            showDetailsApi(this.updateIdXiuGai).then(res => {
                // if(this.usersId==)
                this.changeSupervision = res.data;
                res.data.gwComFileList.forEach(item => {
                    if (
                        item.fileExt == "mp3" ||
                        item.fileExt == "MP3" ||
                        item.fileExt == "m4a"
                    ) {
                        this.audioArray.push(item);
                    } else if (
                        item.fileExt == "jpg" ||
                        item.fileExt == "JPG" ||
                        item.fileExt == "png" ||
                        item.fileExt == "PNG" ||
                        item.fileExt == "JPEG" ||
                        item.fileExt == "jpeg"
                    ) {
                        this.pictureArray.push(item);
                    } else if (item.fileExt == "mp4" || item.fileExt == "MP4") {
                        this.videoArray.push({
                            width: "130",
                            height: "130",
                            autoplay: false,
                            muted: true,
                            language: "en",
                            playbackRates: [0.7, 1.0, 1.5, 2.0],
                            sources: [
                                {
                                    type: "video/mp4",
                                    src: this.hostUrl + item.filePath
                                }
                            ],
                            id: item.id
                        });
                    } else if (
                        item.fileExt == "pdf" ||
                        item.fileExt == "doc" ||
                        item.fileExt == "docx" ||
                        item.fileExt == "txt" ||
                        item.fileExt == "xls" ||
                        item.fileExt == "xlsx"
                    ) {
                        this.fileArray.push(item);
                    }
                });

                this.picturetabName = "图片资料(" + this.pictureArray.length + ")";
                this.audiotabName = "音频资料(" + this.audioArray.length + ")";
                this.videotabName = "视频资料(" + this.videoArray.length + ")";
                this.filetabName = "文档资料(" + this.fileArray.length + ")";
            });
        },
        // 提交修改信息
        submitFile(){
            addFileApi(this.changeSupervision).then(res=>{
                if(res.success){
                    this.gwComFileList = [];
                    if (this.$refs.uploadFile.submitUpload(res.data.id)) {
                        this.removeNewSupervision();
                    }
                    this.$message.success('修改成功')
                }else{
                    this.$message.error(res.message)
                }
            }).catch(err=>{
                this.$message.error(err)
            })
        },
        removeNewSupervision(arg) {
            if (arg) {
                var args = this.gwComFileList.concat(arg);
            }
            this.changeSup(this.fileId)
            this.$emit('dialogClose')
            this.gwComFileList = [];
        },
        changeSupervisionVisiblefalse() {
            this.$emit('closeEditDialog')
            this.$refs.uploadFile.init();
            this.addPicPanel = false;
        },
        addPic() {
            this.addPicPanel = !this.addPicPanel;
        },
        // 删除图片文件
        removeFile(id, index) {
            // this.updateIdXiuGai = this.updateIdXiuGai;
            var _self = this;
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "返回",
                type: "warning"
            }).then(
                res => {
                if (res === "confirm") {
                    deleteFileById(id, this.persId).then(res => {
                    if (res.success) {
                        this.changeSupervision.gwComFileList.forEach(item => {
                        if (
                            item.fileExt == "mp3" ||
                            item.fileExt == "MP3" ||
                            item.fileExt == "m4a"
                        ) {
                            this.audioArray.splice(index, 1);
                        } else if (
                            item.fileExt == "jpg" ||
                            item.fileExt == "JPG" ||
                            item.fileExt == "png" ||
                            item.fileExt == "PNG" ||
                            item.fileExt == "JPEG" ||
                            item.fileExt == "jpeg"
                        ) {
                            this.pictureArray.splice(index, 1);
                        } else if (item.fileExt == "mp4" || item.fileExt == "MP4") {
                            this.videoArray.splice(index, 1);
                        }
                        });
                        _self.changeSup(this.updateIdXiuGai);
                    } else {
                        this.failInfo();
                    }
                    });
                }
                },
                res => {
                console.log("返回");
                }
            )
        },
        // 操作成功反馈
        successInfo() {
            this.$message({
                type: "success",
                message: "操作成功!"
            });
        },
        // 操作失败反馈
        failInfo(val) {
            this.$message.error(val);
        },
        // 文件上传
        submitUpload() {
            this.$refs.upload.submit();
        },
        closeModal(){
            this.$emit('dialogClose')
        }
    },
    watch:{
        fileId(val){
            this.changeSup(val)
        }
    }
}
</script>
<style>
    .scroll_wrapper{
        overflow: hidden;
    }
    .scroll_wrapper .el-form{
        padding-top: 5px;
        padding-right: 5px;
    }
    .myForm .el-form-item__label{
        font-size:large;
    }
    .upload-container {
        width: 100%;
        position: relative;
        .image-uploader {
            width: 35%;
            float: left;
        }
        .image-preview {
            width: 200px;
            height: 200px;
            position: relative;
            border: 1px dashed #d9d9d9;
            float: left;
            // margin-left: 50px;
            .image-preview-wrapper {
                position: relative;
                width: 100%;
                height: 100%;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .image-preview-action {
                position: absolute;
                width: 100%;
                height: 100%;
                left: 0;
                top: 0;
                color: #fff;
                opacity: 0;
                font-size: 20px;
                background-color: rgba(0, 0, 0, 0.5);
                transition: opacity 0.3s;
                cursor: pointer;
                text-align: center;
                line-height: 200px;
                .el-icon-delete {
                    font-size: 36px;
                }
            }
            &:hover {
                .image-preview-action {
                    opacity: 1;
                }
            }
        }
        .image-app-preview {
            margin-bottom: 10px;
            width: 40vh;
            height: 180px;
            position: relative;
            border: 1px dashed #d9d9d9;
            float: left;
            // margin-left: 50px;
            .app-fake-conver {
                height: 44px;
                position: absolute;
                width: 100%; // background: rgba(0, 0, 0, .1);
                text-align: center;
                line-height: 64px;
                color: #fff;
            }
        }
    }
    .file_modify_dialog{
        .el-icon-circle-plus,.el-icon-remove{
            margin-left: 10px;
            color: #009fff;
            cursor: pointer;
        }
        .el-form-item__label{
            font-size: large;
        }
    }
</style>
