<template>
    <el-dialog :title="fileType === 1 ? `图片素材` : `视频素材`" :visible.sync="visible" width="1100px"
        :before-close="dialogBeforeClose" :close-on-click-modal="false" :close-on-press-escape="false"
        class="material-wrapper">
        <div class="material-cont clearfix">
            <div class="material-side image-side">
                <div class="pictrue-tabs">{{ fileType === 1 ? "我的图片" : "我的视频" }}</div>
                <div class="side-btns">
                    <div class="file-upload">
                        <!-- <el-upload class="upload-demo" action="/api/sys/oss/upload" :on-success="imageUploadSuccess"
                            :show-file-list="false">
                            <el-button size="small" type="primary">{{ fileType === 1 ? "上传图片" : "上传视频" }}</el-button>
                        </el-upload> -->
                        <el-upload class="upload-demo" :action="uploadurl" :on-success="imageUploadSuccess"
                            :show-file-list="false">
                            <el-button size="small" type="primary">{{ fileType === 1 ? "上传图片" : "上传视频" }}</el-button>
                        </el-upload>
                    </div>
                </div>
            </div>
            <div class="material-list image-list" element-loading-text="加载中" v-loading="dataListLoading">
                <div>
                    <!-- <el-input clearable v-model="dataForm.title" placeholder="请输入素材名称" size="mini"
                        style="margin-bottom: 14px; width: 200px; margin-right: 10px;"></el-input>
                    <el-button type="primary" @click="getDataList" size="mini">搜索</el-button> -->
                    <div v-if="dataList.length > 0">
                        <ul class="list-box image-box clearfix" v-if="fileType === 1">
                            <li class="image-item list-item" @click="chooseMediaByIndexHandle(index)"
                                :class="{ active: isAllOption }" v-for="(item, index) in dataList" :key="index">
                                <div class="del-btn" v-if="item.checked">✕</div>
                                <span class="checked-item" v-if="item.checked"></span>
                                <img class="bg-img" :src="item.url" alt="" />
                                <div class="inline-text-editor"><input maxlength="50" readonly="readonly"
                                        v-model="item.title"></div>
                            </li>
                        </ul>
                        <ul class="list-box video-box clearfix" v-if="fileType === 2">
                            <li class="video-item list-item" @click="chooseMediaByIndexHandle(index)"
                                :class="{ active: isAllOption }" v-for="(item, index) in dataList" :key="index">
                                <div class="del-btn" v-if="item.checked">✕</div>
                                <span class="checked-item" v-if="item.checked"></span>
                                <video :src="item.src" poster=""></video>
                                <div class="inline-text-editor"><input maxlength="50" readonly="readonly"
                                        :value="item.title"></div>
                                <div class="video-info clearfix">
                                    <div class="video-info-left" v-if="false">1280*720</div>
                                    <div class="video-info-right" v-if="false">5.65MB</div>
                                </div>
                                <div class="video-duration" v-if="false">00:50</div>
                            </li>
                        </ul>
                    </div>
                    <div class="resourceNone" v-else>
                        <img src="@/assets/images/resouceNone.png"
                            style="display: block;;width:400px;margin:20px auto 0" alt="" />
                        <p style="text-align: center;">Sorry，没有搜到相关内容</p>
                    </div>
                    <div class="list-footer clearfix" v-if="page > 1 || dataList.length > 0">
                        <div class="list-ctrl">
                            <el-button size="small" @click="batchOptions" v-if="!isAllOption">批量操作</el-button>
                            <el-button type="primary" @click="completeMediaHandle" size="small" v-if="isAllOption">完成
                            </el-button>
                            <el-button size="small" @click="deleteMediaHandle" class="ctrl-del" v-if="isAllOption">删除
                            </el-button>
                            <el-checkbox v-model="checked" @change="chooseMediaHandle" v-if="isAllOption">全选
                            </el-checkbox>
                        </div>
                        <div class="list-page">
                            <el-pagination :current-page="page" :page-sizes="[15, 30, 75, 150]" :page-size="limit"
                                :total="total" layout="total, sizes, prev, pager, next, jumper"
                                @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import service from "@/utlis/axios";
var that = null
export default {
    name: 'materialManage',
    props: {
        fileType: {
            type: Number,
            required: true
        },
        itemOrSrc: {
            type: String,
        },
        onSuccess: {
            type: Function,
            required: true
        }
    },
    data: function () {
        return {
            uploadurl:process.env.NODE_ENV == 'development'?'/api/sys/oss/upload':'/sys/oss/upload',
            visible: false,
            mixinViewModuleOptions: {
                getDataListURL: '/material/page',
            },
            dataForm: {
                fileType: 1,
                title: ''
            },
            dataList: [],
            page: 1,                    // 当前页码
            limit: 15,                  // 每页数
            total: 0,                   // 总条数
            dataListLoading: false,     // 数据列表，loading状态
            dataListSelections: [],     // 数据列表，多选项

            isAllOption: false,
            checked: false
        }
    },
    beforeCreate: function () {
        that = this;
    },
    created: function () {
        that.dataForm.fileType = that.fileType;
        that.visible = true;
        that.query()
    },
    methods: {
        imageUploadSuccess: function (json) {
            that.getDataList();
        },
        query: function () {
            that.dataListLoading = true;
            service.get("/sys/oss/page", {
                params: {
                    page: that.page,
                    limit: that.limit,
                    fileType: that.dataForm.fileType,
                }
            }).then((res) => {
                that.dataListLoading = false;
                if (res.data.code != 0) {
                    that.dataList = [];
                    that.total = 0;
                    return that.$message.error(res.data.data.msg);
                }
                that.dataList = res.data.data.list;
                that.total = res.data.data.total;
            }).catch(function () {
                that.dataListLoading = false;
            });
        },
        // 分页, 每页条数
        pageSizeChangeHandle: function (val) {
            that.page = 1;
            that.limit = val;
            that.query();
        },
        // 分页, 当前页
        pageCurrentChangeHandle: function (val) {
            that.page = val;
            that.query();
        },
        getDataList: function () {
            that.page = 1;
            that.query();
        },
        dialogBeforeClose: function () {
            that.visible = false;
            that.$parent.materialVisible = false;
        },
        batchOptions: function () {
            that.dataList = that.dataList.map(s => {
                s.checked = true;
                return s;
            })
            that.checked = true;
            that.isAllOption = true;
        },
        chooseMediaByIndexHandle: function (index) {
            if (that.isAllOption) { //操作
                that.$set(that.dataList[index], 'checked', !that.dataList[index].checked);
                that.$forceUpdate();
            } else { //选择图片
                that.visible = false;
                if (that.itemOrSrc && that.itemOrSrc == 'item') {
                    that.onSuccess.call(this, that.dataList[index]);
                } else {
                    that.onSuccess.call(this, that.dataList[index].src);
                }
            }
        },
        chooseMediaHandle: function () {
            //全部操作
            that.dataList = that.dataList.map(s => {
                s.checked = that.checked;
                return s;
            })
        },
        completeMediaHandle: function () {
            that.dataList = that.dataList.map(s => {
                s.checked = false;
                return s;
            })
            that.checked = false;
            that.isAllOption = false;
        },
        deleteMediaHandle: function () {
            if (!that.dataList.some(ss => ss.checked)) {
                return that.$message.error('未选择文件');
            }
            that.$confirm('删除？', '删除', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                service.delete('/sys/oss', { 'data': that.dataList.filter(item=> {return item.checked==true}).map(item => item.id) })
                    .then(function (res) {
                        if (res.data.code !== 0) return that.$message.error(res.data.msg);
                        that.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 500,
                            onClose: function () {
                                that.query();
                            }
                        });
                    }).catch(function () {
                    });
            }).catch(function () {
            });
        }
    }
}
</script>

<style scoped>
.clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    height: 0;
}

.clearfix:after {
    content: "";
    display: table;
    clear: both;
}

/* 弹窗 */
.el-dialog__wrapper .el-dialog .el-dialog__header {
    padding-top: 16px;
    padding-bottom: 16px;
    border-bottom: 1px solid #dcdcdc;
}

/* 图片素材 */
.material-wrapper .el-dialog__body {
    padding: 0 !important;
}

.material-side {
    float: left;
    position: relative;
    width: 180px;
    text-align: center;
    border-right: 1px solid #eaeff4;
    box-sizing: border-box;
    padding-right: 20px;
}

.material-side.image-side {
    height: 640px;
}

.pictrue-tabs {
    width: 160px;
    height: 50px;
    line-height: 50px;
    padding-left: 24px;
    text-align: left;
    background: #edf1ff;
    box-sizing: border-box;
}

.material-side .side-btns {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding-bottom: 14px;
}

.file-upload {
    display: inline-block;
}

.material-side .side-btns .el-button {
    display: block;
    width: 132px;
    margin: 10px auto;
    border-radius: 16px;
}

.material-list {
    float: left;
    width: 820px;
    box-sizing: border-box;
    overflow: hidden;
}

.material-list.image-list {
    padding: 24px 18px;
}

.material-list .list-box.image-box {
    height: 510px;
}

.material-list .list-box.video-box {
    height: 450px;
}

.material-list .list-box .list-item {
    position: relative;
    box-sizing: border-box;
    cursor: pointer;
}

.material-list .list-box .image-item {
    float: left;
    display: block;
    width: 130px;
    height: 130px;
    margin: 0 6px 40px;
    background: #e0e0e0;
}

.material-list .list-box .image-item .bg-img {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: 50%;
}

.material-list .list-box .video-item {
    float: left;
    display: block;
    width: 130px;
    height: 130px;
    margin: 0 6px 40px;
    background: #e0e0e0;
}

.material-list .list-box .video-item video {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

.inline-text-editor input {
    display: block;
    width: 100%;
    height: 20px;
    margin: 5px 0;
    line-height: 20px;
    border: 1px solid transparent;
    outline: none;
    font-size: 14px;
    color: #53586b;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.material-list .list-box .list-item .del-btn {
    z-index: 10;
    position: absolute;
    top: 5px;
    right: 5px;
    display: none;
    width: 14px;
    height: 14px;
    color: #fff;
    font-size: 12px;
    text-align: center;
    line-height: 13px;
    background: rgba(0, 0, 0, .4);
    border-radius: 18px;
    cursor: pointer;
}

.material-list .list-box .list-item:hover .del-btn {
    display: block;
}

.material-list .list-box .list-item.active .checked-item {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    height: 40px;
    width: 40px;
    background: url(../assets/images/check_gou.png) no-repeat;
}

.material-list .list-box .list-item.active .bg-img,
.material-list .list-box .list-item.active video {
    border: 2px solid #409eff;
}

.material-list .list-box .video-item .video-info {
    width: 100%;
    font-size: 12px;
    color: #999;
}

.material-list .list-box .video-item .video-info-left {
    float: left;
    width: 50%;
    text-align: left;
}

.material-list .list-box .video-item .video-info-right {
    float: left;
    width: 50%;
    text-align: right;
}

.material-list .list-box .video-item .video-duration {
    position: absolute;
    bottom: 8px;
    right: 8px;
    padding: 5px 9px;
    font-size: 12px;
    text-align: center;
    color: #fff;
    background: #000;
    border-radius: 2px;
    opacity: .7;
}

.material-list .list-footer {
    margin-top: 8px;
}

.material-list .list-footer .list-ctrl {
    float: left;
    width: 230px;
    height: 32px;
}

.material-list .list-footer .list-ctrl .el-button {
    padding: 9px 24px;
}

.material-list .list-footer .list-ctrl .ctrl-del {
    margin: 0 6px;
}

.material-list .list-footer .list-page {
    float: right;
    width: 542px;
    text-align: right;
}
</style>