<template>
    <div class="pb_content" style="background: unset">
        <div class="pb_content_body" style="
          background: #fff;
          padding: 0px 25px;
          box-sizing: border-box;
          border-radius: 5px;
          font-size: 16px;
        ">
            <div class="reBox">
                <div class="reTop">
                    <div>分类筛选</div>
                    <div>
                        <!-- @click="getCourse" -->
                        <div class="search" @click="getSource">
                            <img src="../../../assets/icon/search.png" alt="" />
                        </div>
                        <input class="sInput" type="text" placeholder="请输入关键字" v-model="sourceName"
                            @keyup.enter="getSource" />
                    </div>
                </div>
                <div class="choose">
                    <div class="all_choose" v-for="(item, index) in CourseType[0]" :key="index"
                        :style="{ margin: !CourseTypeJson[item.id].length && 0 }"  v-if="(item.name != '类型') || typea != '' || typeE.indexOf('e4cb3395-5602-4441-801c-f380e8935a74') != -1">
                        <span v-if="CourseTypeJson[item.id].length">{{ item.name }}:</span>
                        <div class="typeCss" v-if="CourseTypeJson[item.id].length">
                            <div class="cName" @click="getCourse2(item.name, '', item.id, 1)"
                                :class="typeE.indexOf(item.id) != -1 ? 'isCType' : ''">
                                全部
                            </div>
                            <div v-for="(item1, index1) in ctype(item.id)" :key="index + '-' + index1"
                                :label="item1.id" @click="getCourse2(item.name, item.id, item1.id, 2)">
                                <div class="cName" :class="typea == item1.id || typeb == item1.id
                                        ? 'isCType'
                                        : ''
                                    ">
                                    {{ item1.name }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="pb_content_body">
            <div class="student_table" v-loading="loading">
                <div class="source-box" v-for="(item, index) in res" :key="index">
                    <img class="checkImg" src="../../../assets/icon/source/checka.png" @click="checka(item)" v-if="sourceData[item.id]"/>
                    <img class="checkImg" src="../../../assets/icon/source/check.png" @click="checka(item)" v-else/>
                    <!-- <div class="iamge"><img src="../../../assets/icon/source/image.png" alt=""></div>
                    <div class="title"><span>{{ item.name }}</span></div> -->
                    <div class="fengmian">
                        <img src="../../../assets/icon/source/fengmian.jpg" alt="">
                        <div class="ftitle"><span>{{ item.name }}</span></div>
                    </div>
                    <div class="detail">{{ item.detail }}</div>
                    <div class="label"><span v-for="(k, ki) in item.label.split(',')" :key="index + '-' + ki">{{ k ? k : "无" }}</span>
                    </div>
                    <div class="button"><span @click="check(item.url)">查看</span></div>
                </div>
                <div v-if="!res.length" style="text-align: center; width: 100%;">暂无数据</div>
            </div>
            <div class="student_page">
                <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="total" v-if="page && total"
                    @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </div>
        <el-dialog title="添加资源" :visible.sync="dialogVisible" :append-to-body="true" width="800px"
            :before-close="handleClose" class="dialog_diy">
            <div class="add-box">
                <div class="span-box">
                    <span><span style="color: red">*</span>工具名称</span><el-input class="input" v-model="s_title"
                        placeholder="请输入工具名称"></el-input>
                </div>
                <div class="type-box">
                    <div class="both">
                        <div class="choose2">
                            <div class="all_choose2" v-for="(item, index) in CourseType[0]" :key="index">
                                <span v-if="CourseTypeJson[item.id].length > 0
                                    ">{{ item.name }}</span>
                                <el-checkbox-group v-model="courseTypeId2" v-if="CourseTypeJson[item.id].length > 0
                                    ">
                                    <el-checkbox v-for="item1 in CourseTypeJson[item.id]" :key="item1.id"
                                        :label="item1.id">{{ item1.name }}</el-checkbox>
                                </el-checkbox-group>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="span-box">
                    <span><span style="color: red">*</span>网址来源</span><el-input class="input" v-model="s_url"
                        placeholder="请输入网址来源"></el-input>
                </div>
                <div class="span-box">
                    <span><span style="color: red">*</span>工具描述</span><el-input class="input" type="textarea" resize="none"
                        rows="5" v-model="s_detail" placeholder="请输入工具描述"></el-input>
                </div>
                <div class="span-box">
                    <span>标签</span><el-select class="input" v-model="s_label" multiple placeholder="请选择标签">
                        <el-option label="无需登录" value="无需登录"></el-option>
                        <el-option label="扫码登录" value="扫码登录"></el-option>
                        <el-option label="注册登录" value="注册登录"></el-option>
                        <el-option label="免费" value="免费"></el-option>
                        <el-option label="付费" value="付费"></el-option>
                        <el-option label="中文" value="中文"></el-option>
                        <el-option label="英文" value="英文"></el-option>
                    </el-select>

                </div>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="add()">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
  
<script>
export default {
    props: {
        sourceData: {
            type: Object,
        },
    },
    data() {
        return {
            page: 1,
            pageSize: 20,
            total: 0,
            isLoading: false,
            dialogVisible: false,
            title: "",
            tableData: [],
            res: [],
            userid: this.$route.query.userid,
            org: this.$route.query.org,
            oid: this.$route.query.oid,
            CourseType: [],
            CourseTypeJson: {},
            courseTypeId: {},
            courseTypeSon: [],
            isChoose: 0,
            sourceName: "",
            typea: "",
            typeb: "",
            typeE: [],
            courseTypeId2: [],
            s_title: "",
            s_url: "",
            s_detail: "",
            s_label: [],
        };
    },
    mounted() {
        this.selectAllType();
    },
    computed: {
        ctype() {
            return function (tid) {
                if(tid == 'e4cb3395-5602-4441-801c-f380e8935a74'){
                    return this.CourseTypeJson[tid]
                }else{
                    if(this.typea == 'b107f98f-1dc4-4ede-80b4-178ba22b6a59'){
                        return this.CourseTypeJson[tid].filter(e => {
                            return ['fd7fc4a9-e934-47d5-9edf-f478a59010f0','5143a350-b20f-4a07-9d19-5ad305fefaac','3777ef66-96ff-4f08-8e6d-a7b6acc6c789'].indexOf(e.id) != -1 
                        })
                    }else if(this.typea == '3b12e8d9-866f-46bb-aa22-76cb3b7788b3'){
                        return this.CourseTypeJson[tid].filter(e => {
                            return ['fd7fc4a9-e934-47d5-9edf-f478a578789x','fd7fc4a9-e934-47d5-9edf-f478a5789631','1ab97776-71fa-40b1-84c3-5b311d9f6fd9','7c222102-cc32-4613-8f0f-e117ab3f49c0','d8c176a9-9c94-46a0-a824-d2b740323e62','7e79947e-5445-4ed2-935b-e078661a6256','df502b3d-8836-4449-b8c0-09d9d5f9b86d','fd7fc4a9-e934-47d5-9edf-f478a5789631','9fc98487-8e9c-47fa-ae5e-05122f0bf742','1de1bf4d-d4c9-4874-8993-4d3037a10fa1'].indexOf(e.id) != -1 
                        })
                    }else if(this.typea == 'd760341a-5ab7-417f-bfc8-0fd3f183408a'){
                        return this.CourseTypeJson[tid].filter(e => {
                            return  ['d8c176a9-9c94-46a0-a824-d2b740323abc','5f9afbf8-9c7b-403a-8629-162c1d6adea4'].indexOf(e.id) != -1 
                        })
                    }else if(this.typea == '926efcf3-da23-4123-aa57-50341bf7d85f'){
                        return this.CourseTypeJson[tid].filter(e => {
                            return  ['c3eaac53-3599-43c6-a224-a9e8b1e6eb7a','65196690-f7bc-4fc2-9175-b9ddc7dcba07','1874291f-96e5-4d9f-8747-170f87edb171','3777ef66-96ff-4f08-8e6d-a7b6acc6cb89','56ef86e8-c379-4b35-b752-f9a12e0dd2e7'].indexOf(e.id) != -1 
                        })
                    }else if(this.typea == '8b21473c-eac2-4a40-8fb5-ac38985e8952'){
                        return this.CourseTypeJson[tid].filter(e => {
                            return  ['e3365b81-49bb-4de9-a1c2-060e9dc593f3','5670c02b-7cbc-4788-8241-cf6e53dd61fd','25b66132-a125-4c51-8bbe-2653f8be4f0a','da94e547-ba90-4fd5-b95e-d13efca1e248','ba1db98a-0613-4568-92d2-59d53b600675'].indexOf(e.id) != -1 
                        })
                    }else if(this.typeE.indexOf('e4cb3395-5602-4441-801c-f380e8935a74') != -1){
                        return this.CourseTypeJson[tid]
                    }else{
                        return []
                    }
                }
            };
        }
    },
    methods: {
        checka(item){
            let _sourceData = this.sourceData
            if(_sourceData[item.id]){
                delete _sourceData[item.id]
            }else{
                _sourceData[item.id] = item
            }
            this.$forceUpdate()
            this.$emit("update:sourceData",_sourceData)
        },
        check(url){
            window.open(url)
        },
        handleCurrentChange(val) {
            this.page = val;
            this.getSource();
        },
        handleClose(done) {
            done();
        },
        selectAllType() {
            let params = {
                org: this.org && this.org != "" ? this.org : "",
                oid: this.oid && this.oid != "" ? this.oid : "",
            };
            this.ajax
                .get(this.$store.state.api + "selectAllSourceType", params)
                .then((res) => {
                    this.CourseType = res.data;
                    for (var i = 0; i < res.data[0].length; i++) {
                        if(res.data[0][i].id == "37076c23-7053-46eb-b25b-756824806608"){
                            res.data[0][i].name = "类型";
                        }else if(res.data[0][i].id == "e4cb3395-5602-4441-801c-f380e8935a74"){
                            res.data[0][i].name = "场景";
                        }
                        if (!this.cid) {
                            this.courseTypeId[res.data[0][i].id] = [];
                        }
                        if (!this.CourseTypeJson[res.data[0][i].id]) {
                            this.CourseTypeJson[res.data[0][i].id] = [];
                        }

                        if (res.data[2].length == 0 && res.data[3].length == 0) {
                            for (var j = 0; j < res.data[1].length; j++) {
                                if (res.data[0][i].id == res.data[1][j].pid) {
                                    this.CourseTypeJson[res.data[0][i].id].push(res.data[1][j]); // 去除公共分类
                                }
                            }
                        } else {
                            if (res.data[2].length > 0) {
                                for (var j = 0; j < res.data[2].length; j++) {
                                    if (res.data[0][i].id == res.data[2][j].pid) {
                                        this.CourseTypeJson[res.data[0][i].id].push(res.data[2][j]); // 去除公共分类
                                    }
                                }
                            }
                            if (res.data[3].length > 0) {
                                for (var j = 0; j < res.data[3].length; j++) {
                                    if (res.data[0][i].id == res.data[3][j].pid) {
                                        this.CourseTypeJson[res.data[0][i].id].push(res.data[3][j]); // 去除公共分类
                                    }
                                }
                            }
                        }
                    }
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        getCourse2(typeName, ftypeId, typeid, type) {
            this.page = 1;
            if (typeid == "e4cb3395-5602-4441-801c-f380e8935a74" || ftypeId == 'e4cb3395-5602-4441-801c-f380e8935a74') {
                this.typeb = "";
                if (type == 1) {
                    if (this.typeE.indexOf(typeid) != -1) {
                        this.typeE.splice(this.typeE.indexOf(typeid), 1);
                    } else {
                        this.typeE.push(typeid);
                        if (this.typea != "") {
                            this.typea = "";
                        }
                    }
                } else {
                    if (this.typea == typeid) {
                        this.typea = "";
                    } else {
                        this.typea = typeid;
                        if (this.typeE.indexOf(ftypeId) != -1) {
                            this.typeE.splice(this.typeE.indexOf(ftypeId), 1);
                        }
                    }
                }
            } else if (typeid == "37076c23-7053-46eb-b25b-756824806608" || ftypeId == '37076c23-7053-46eb-b25b-756824806608') {
                if (type == 1) {
                    if (this.typeE.indexOf(typeid) != -1) {
                        this.typeE.splice(this.typeE.indexOf(typeid), 1);
                    } else {
                        this.typeE.push(typeid);
                        if (this.typeb != "") {
                            this.typeb = "";
                        }
                    }
                } else {
                    if (this.typeb == typeid) {
                        this.typeb = "";
                    } else {
                        this.typeb = typeid;
                        if (this.typeE.indexOf(ftypeId) != -1) {
                            this.typeE.splice(this.typeE.indexOf(ftypeId), 1);
                        }
                    }
                }
            }
            if(this.typea == "" && this.typeE.indexOf('e4cb3395-5602-4441-801c-f380e8935a74') == -1){
                this.typeE = []
            }
            this.getSource();
        },
        getSource() {
            this.loading = true
            let params = {
                uid: this.userid,
                oid: this.oid,
                org: this.org,
                typea: this.typea != undefined ? this.typea : "",
                typeb: this.typeb != undefined ? this.typeb : "",
                typeE: this.typeE.join(","),
                cn: this.sourceName,
                page: this.page,
                pageSize: this.pageSize,
            };
            this.ajax
                .get(this.$store.state.api + "selectSource", params)
                .then((res) => {
                    this.loading = false
                    this.res = res.data[0];
                    this.total = res.data[0].length ? res.data[0][0].num : 0;
                })
                .catch((err) => {
                    console.error(err);
                });
        },
        addSource() {
            this.courseTypeId2 = []
            this.s_title = ""
            this.s_url = ""
            this.s_detail = ""
            this.s_label = []
            this.dialogVisible = true
        },
        add() {
            if (this.s_title == '') {
                this.$message.error('请填写工具名称')
                return
            }
            if (this.s_url == '') {
                this.$message.error('请填写网址来源')
                return
            }
            if (this.s_detail == '') {
                this.$message.error('请填写工具描述')
                return
            }
            let params = [{
                name: this.s_title,
                url: this.s_url,
                detail: this.s_detail,
                label: this.s_label.join(","),
                userid: this.userid,
                courseType: JSON.stringify(this.courseTypeId2)
            }]
            this.ajax
                .post(this.$store.state.api + "addSource", params)
                .then((res) => {
                    this.$message({
                        message: "添加成功",
                        type: "success",
                    });
                    this.dialogVisible = false
                    this.getSource();
                })
                .catch((err) => {
                    this.$message.error("网络不佳");
                    console.error(err);
                });
        }
    },
    created() {
        this.getSource();
    },
};
</script>
  
<style scoped>
.pb_head {
    display: flex;
    justify-content: space-between;
}

.student_head {
    margin-bottom: 20px;
}

.student_page {
    margin-top: 10px;
}

.pb_head {
    margin: 0 !important;
    width: 100% !important;
}

.bgColor {
    background: #466b99;
}

.reBox {
    background: #fff;
    margin: 0 auto;
    padding: 10px 5px;
    box-sizing: border-box;
}

.reTop {
    padding: 0 0 5px;
    /* padding: 20px 0 0 0; */
    /* border-bottom: 1px solid #eee; */
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: space-between;
}

.reTop>div:nth-child(1) {
    font-weight: bold;
    /* width: 40px; */
    /* border-bottom: 1px solid #205cc6; */
    /* padding-bottom: 20px; */
    color: #205cc6;
    /* font-size: 20px; */
}

.reTop>div:nth-child(2) {
    display: flex;
    flex-direction: row;
    align-items: center;
    border: 1px solid #ccced3;
    width: 300px;
    border-radius: 8px;
    padding: 5px 0;
    /* margin-bottom: 10px; */
    background: #fafafa;
}

.search {
    width: 20px;
    padding: 0 5px;
}

.search>img {
    width: 100%;
    height: 100%;
}

.sInput {
    border: none;
    width: 85%;
    background: #fafafa;
}

.sInput:focus-visible {
    outline: none;
}

.typeCss {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
}

.choose {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    height: 100%;
    justify-content: space-evenly;
    align-items: flex-start;
    padding: 10px 0;
}

.all_choose {
    display: flex;
    flex-direction: row;
    align-items: baseline;
    margin: 2px 0;
    width: 100%;
}

.all_choose>span {
    min-width: 80px;
    display: block;
    letter-spacing: 14px;
}

.all_choose>span:nth-child(1) {
    font-weight: bold;
}


.cName {
    cursor: pointer;
    margin: 0 10px 5px 0;
    color: #b9b6b9;
    min-width: 80px;
    width: 80px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.isCType {
    color: #6282c2;
}

.dialog_diy>>>.el-dialog {
    height: 100% !important;
    margin: 0 auto !important;
}

.dialog_diy>>>.el-dialog__header {
    background: #3c3c3c !important;
    padding: 15px 20px;
}

.dialog_diy>>>.el-dialog__title {
    color: #fff;
}

.dialog_diy>>>.el-dialog__headerbtn {
    top: 19px;
}

.dialog_diy>>>.el-dialog__headerbtn .el-dialog__close {
    color: #fff;
}

.dialog_diy>>>.el-dialog__headerbtn .el-dialog__close:hover {
    color: #fff;
}

.dialog_diy>>>.el-dialog__body,
.dialog_diy>>>.el-dialog__footer {
    background: #fafafa;
}

.dialog_diy>>>.el-dialog__body {
    height: calc(100% - 125px);
    box-sizing: border-box;
}


.both {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
    align-items: center;
    justify-content: flex-start;
    margin: 15px 0;
}

.all_choose2 {
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    width: 100%;
}

.all_choose2>span {
    min-width: 100px;
    display: block;
    letter-spacing: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.all_choose2>span:nth-child(1) {
    font-weight: bold;
}

.all_choose2>>>.el-checkbox-group {
    display: flex;
    flex-direction: row;
    width: 100%;
    flex-wrap: wrap;
    align-content: center;
    justify-content: flex-start;
    align-items: center;
    margin-top: 3px;
}

.all_choose2>.el-checkbox-group>>>.el-checkbox {
    margin-bottom: 10px;
    display: flex;
    flex-direction: row;
    align-items: center;
}

.all_choose2>.el-checkbox-group>.el-checkbox>>>.el-checkbox__label {
    min-width: 80px;
    overflow: hidden;
    width: 80px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.all_choose2>.el-checkbox-group>.el-checkbox>>>.el-checkbox__label:hover {
    width: auto;
}

.choose2 {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    height: 100%;
    justify-content: space-evenly;
    align-items: flex-start;
}

.add-box {
    height: 100%;
    overflow: auto;
    width: 100%;
    background: #fff;
    border-radius: 10px;
    box-sizing: border-box;
    padding: 20px;
}

.span-box {
    display: flex;
    margin-bottom: 20px;
}

.span-box>span {
    font-weight: bold;
    min-width: 100px;
}

.span-box>.input {
    width: 100%;
}

.type-box {
    width: 100%;
}

.student_table {
    display: flex;
    flex-wrap: wrap;
}

.source-box {
    width: calc(100% / 4 - 15px);
    background: #fff;
    overflow: hidden;
    margin: 0 20px 20px 0;
    position: relative;
    display:flex;
    flex-direction: column;
}
.source-box:nth-child(4n){
    margin-right: 0;
}
.checkImg{
    position: absolute;
    top: 10px;
    z-index: 10;
    right: 10px;
    cursor: pointer;
    width: 30px;
    border-radius:5px;
}

.fengmian{
    width: 100%;
    position: relative;
}
.fengmian>img{
    width: 100%;
}

.fengmian>.ftitle{
    position: absolute;
    bottom: 10%;
    color: rgb(88, 121, 198);
    width: 90%;
    text-align: center;
    font-weight: 700;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 24px;
    left: 50%;
    transform: translateX(-50%);
}
.source-box .iamge {
    width: 150px;
    height: 125px;
    margin: 0 auto;
}

.source-box .iamge>img {
    width: 100%;
    height: 100%;
}

.source-box .title {
    width: 90%;
    font-size: 18px;
    text-align: center;
    margin: 5px auto;
    font-weight: 700;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    height:24px;
}

.source-box .detail {
    text-align: center;
    width: 90%;
    margin: 5px auto;
    color: #acacac;
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    height: 42px;
    font-size: 15px;
}

.source-box .label {
    width: 90%;
    margin: 10px auto 5px;
    display: flex;
    flex-wrap: wrap;
}

.source-box .label>span {
    padding: 5px 10px;
    background: rgb(244, 244, 244);
    border-radius: 5px;
    margin: 0 0 5px 5px;
}

.source-box .button {
    width: 100%;
    text-align: center;
    display: flex;
    height: 40px;
    background: rgb(88, 121, 198);
    color: #fff;
    margin-top: auto;
}


.source-box .button>span {
    width: 100%;
    cursor: pointer;
    height: 100%;
    line-height: 40px;
}</style>
  