<template>
    <div class="my_material_modal">
        <!-- <a-modal class="my_material_modal" 
            v-model:visible="myMaterialModalShow" 
            :footer="null" 
            width="80%"
            :maskClosable="false"
            :centered="true"
        > -->
            <div class="my_material_header">
                <div class="my_material_header_right">
                    <div class="content_search_block">
                        <input class="search_input" :placeholder="$t('Material.inputContent1')" v-model="searchPictureName"  @keydown.enter="getLibraryList()">
                        <div class="search_icon_block" @click.stop="getLibraryList()"><span class="icon iconfont icon-sousuo"></span></div>
                    </div>
                </div>
            </div>

            <div class="my_material_content">
             
                <div class="material_content_body scroll_style">
					<div v-for="item,index in imgList" :key="item" class="content_img_item" :class="[ item?.checked ? 'active':''  , selectCode == 'Moodboard' ? 'moodb':''  ]" >
						<img :src="item.imgUrl" @click.stop="selectImgItem(item)">
						<div v-if="selectCode == 'Sketchboard'" class="operate_file_block">
                            <div class="select_img_type">
                                <div
                                    class="select_category"
                                    @click.stop="showFileCategory(item)"
                                >
                                    {{ item?.category }}
                                    <div
                                        :class="[
                                            'icon',
                                            'iconfont',
                                            'icon-xiala',
                                            item?.categoryShow
                                                ? 'icon_rotate'
                                                : '',
                                        ]"
                                    ></div>
                                </div>
                                <div
                                    class="category_list"
                                    v-show="item?.categoryShow"
                                >
                                    <div
                                        :class="[
                                            'category_item',
                                            item?.category == cate.value
                                                ? 'select_category_item'
                                                : '',
                                        ]"
                                        v-for="(
                                            cate, index
                                        ) in disignTypeList"
                                        :key="index"
                                        @click.stop="selectFileCategory(item, cate)"
                                    >
                                        {{ cate.label }}
                                    </div>
                                </div>
                            </div>
                        </div>
						<div v-if="selectCode == 'Sketchboard' || selectCode == 'Printboard'" class="pin_block">
							<a-checkbox v-model:checked="item.pin">{{ $t('Material.PIN') }}</a-checkbox>
						</div>
					</div>
					<div v-show="total > imgList.length" class="material_content_list_loding" v-observe>
						<img src="@/assets/images/homePage/loading.gif" alt="">
					</div>
                </div>
                <div class="no_data_block loading_block" v-show="isShowLoading">
                    <a-spin size="large"></a-spin>
                </div>
             
            </div>
        <!-- </a-modal> -->
    </div>
    
</template>
<script lang="ts">
import { defineComponent, computed, ref} from 'vue'
import { Https } from "@/tool/https";
import { message } from 'ant-design-vue';
import { useStore } from "vuex";
import GO from '@/tool/GO';
import { useI18n } from 'vue-i18n';

export default defineComponent({
    props: ["msg",'disignTypeList'],
    setup(prop) {
        let myMaterialModalShow = ref(false)
        let imgList = ref([
		])
        let isShowLoading:any = ref(false)
        let selectCode:any = ref('')
        let currentPage:any = ref(1)
        let searchPictureName = ref('')
        let pageSize = ref(12)
        let total = ref(0)
        let searcMaterialhName:any = ref('') //搜索名字
        let designType:any = ref(null)
        // let disignTypeList:any = ref([
				
        //     ])
		let workspace:any = ref({})

		let {t} = useI18n()
        return{
            myMaterialModalShow,
            imgList,
            isShowLoading,
            selectCode,
            currentPage,
            searchPictureName,
            pageSize,
            total,
            searcMaterialhName,
            designType,
            // disignTypeList,
			workspace,
			t
        }
    },
	data (prop) {
		return {
			type_ : {
				type1:'material',
				type2:prop.msg
			},
            store: useStore(),
		}
	},
	directives:{
		observe:{
			mounted (el,binding) {
				// console.log(binding.instance);
				const ob = new IntersectionObserver(callback,{
					root:null,
					threshold:[0.1]
				})
				ob.observe(el)
				// this.currentPage = 1
				// this.pageSize = 12
				let this_:any = binding.instance
				function callback(entries:any, observer:any) {
					entries.forEach((entry:any) => {
						if (entry.isIntersecting) {
							this_.getLibraryList()
						} else {
						}
					});
				}
			},
		},
	},
	computed: {
        getSketchLabel(value:any) {
            return (value: any) => {
                let lable = "";
                for (let item of this.disignTypeList) {
                    if (item.value === value) {
                        lable = item.label;
                        break;
                    }
                }
                return lable;
            };
        },
		sketchboardList(){
			return this.store.state.UploadFilesModule.sketchMaterialFiles
		},
		moodboardList(){
			return this.store.state.UploadFilesModule.moodboardMaterialFiles
		},
		printboardList(){
			return this.store.state.UploadFilesModule.printMaterialFiles
		},
    },
	mounted () {
		
		// let loding = document.getElementsByClassName("material_content_list_loding")[0]
		// let bodyLoding = document.getElementsByClassName("material_content_body")[0]
		// const ob = new IntersectionObserver(callback,{
		// 	root:null,
		// 	threshold:[0.1]
		// })
		// ob.observe(loding)
		// console.log(loding);
		// // this.currentPage = 1
		// // this.pageSize = 12
		// // let this_ = this
		// function callback(entries:any, observer:any) {
		// 	entries.forEach((entry:any) => {
		// 		if (entry.isIntersecting) {
		// 			console.log(111);
		// 			// this_.getLibraryList()
		// 		} else {
		// 		}
		// 	});
		// }
	},
	watch:{
		sketchboardList:{
			handler(newVal:any,oldVal:any){
				// this.store.state.Workspace.workspace
				oldVal.forEach((item:any) => {
					if(newVal.findIndex((newValItem:any) =>newValItem.id == item.id) == -1){
						this.imgList.forEach((i:any)=>{
							if(i.id == item.id){
								i.checked = false
							}
						})
					}
				});
			},
		},
		moodboardList:{
			handler(newVal:any,oldVal:any){
				// this.store.state.Workspace.workspace
				oldVal.forEach((item:any) => {
					if(newVal.findIndex((newValItem:any) =>newValItem.id == item.id) == -1){
						this.imgList.forEach((i:any)=>{
							if(i.id == item.id){
								i.checked = false
							}
						})
					}
				});
				
			}
		},
		printboardList:{
			handler(newVal:any,oldVal:any){
				// this.store.state.Workspace.workspace
				oldVal.forEach((item:any) => {
					if(newVal.findIndex((newValItem:any) =>newValItem.id == item.id) == -1){
						this.imgList.forEach((i:any)=>{
							if(i.id == item.id){
								i.checked = false
							}
						})
					}
				});
				
			}
		},
		
	},
	
    methods:{
        init(code:any){
            this.selectCode = code
            // this.myMaterialModalShow = true
			if(this.imgList.length == 0){
			}
			this.getLibraryList()
			
        },
        selectImgItem(imgData:any){
			imgData.type_ = this.type_
			imgData.resData = JSON.parse(JSON.stringify(imgData))
			this.imgList.forEach((v:any)=>{
				v.categoryShow = false
			})
			imgData.jsContent1 = this.t('uploadFile.jsContent1')
			this.store.commit("addGenerateMaterialFils", imgData);
        },
        //改变页码
        changePage(current: number, pageSize: number){
            this.currentPage = current
            this.pageSize = pageSize
            this.getLibraryList()
        },

        handleChange(){
            this.getLibraryList()
        },

        //选择所有的图片
        getLibraryList(){
			let workspace = this.store.state.Workspace.workspace
            let data = {
                level1Type:this.selectCode,
                // level2Type:this.designType,
				modelSex:workspace?.sex,
                page:this.currentPage,
                pictureName:this.searchPictureName,
                size:this.pageSize+this.imgList.length,
            }
            this.isShowLoading = true
            Https.axiosPost(Https.httpUrls.queryLibraryPage,data).then(
                (rv: any) => {
					let aa:any = []
					rv.content.forEach((item:any,index:any) => {
						let arr
						if(this.type_.type2 == 'Sketchboard'){
							arr = this.store.state.UploadFilesModule.sketchMaterialFiles
							item.category = item.level2Type;
							item.categoryShow = false;
						}else if(this.type_.type2 == 'Moodboard'){
							arr = this.store.state.UploadFilesModule.moodboardMaterialFiles
						}else if(this.type_.type2 == 'Printboard'){
							arr = this.store.state.UploadFilesModule.printMaterialFiles
						}
						item.imgUrl = item.url
						// aa.push(item)
						if(!item.id_){
							item.id_ = GO.id++
							arr.forEach((v:any)=>{
								if(item.id == v.id){
									item.id_ = v.id_?v.id_:GO.id++
									item.checked = true
								}
							})
						}else{
						}
						aa.push(item)
					});
					this.imgList = aa
                    this.total = rv.total
                    this.isShowLoading = false
                }
            ).catch((res)=>{
                this.isShowLoading = false
            });
        },
        closeModal(){
            this.myMaterialModalShow = false
            this.searchPictureName = ''
            this.designType = null
            this.imgList = []
            this.currentPage = 1
            this.pageSize = 10
            this.total = 0
        },
		showFileCategory(file: any) {
            file.categoryShow = true;
            document.addEventListener("click", this.hiddenFileCategory);
        },
		 selectFileCategory(file: any, cate: any) {
            file.category = cate.value;
			 for (let item of (this.imgList as any)) {
                item.categoryShow = false;
            }
			this.setSketchLibrary(file)
        },
		hiddenFileCategory() {
            for (let item of (this.imgList as any)) {
                item.categoryShow = false;
            }
            document.removeEventListener("click", this.hiddenFileCategory);
        },
		setSketchLibrary(item:any){
			let data = {
				libraryId:item.id,
				level2Type:item.category,
			}
			Https.axiosPost(Https.httpUrls.setSketchLibrary,data).then(
                (rv: any) => {
                }
            ).catch((res)=>{
            });
		},
    }
})
</script>
<style lang="less">
.my_material_modal{
	height: 30rem;
    overflow-x: hidden;
	border-right: 1px solid #e5e5e5;
	flex: 1;
	border-radius: 0;
	&.my_material_modal::-webkit-scrollbar{display: none;}
    .ant-modal-close{
        width: 3.6rem;
        height: 3.6rem;
        position: absolute;
        top: -1.8rem;
        right: -1.8rem;
    }

    .ant-modal-header{
        display: none;
    }

    .ant-modal-body{
        background: #F2F3FB;
        height: 80vh;
        min-height: 72rem;
        overflow-y: hidden;
        padding: 0;
    }

    .my_material_header{
		display: flex;
		justify-content: flex-end;
        align-items: center;
        width: 100%;
		height: auto;
		padding: 0;
		padding-top: 2.5rem;
		padding-bottom: 2rem;
		background-color: #fff;
		position: sticky;
		top: 0;
		z-index: 2;
        .my_material_header_right{
            display: flex;
            justify-content: space-between;
            align-items: center;

            .content_search_block{
                margin-right: 5rem;
                display: flex;

                .search_input{
                    width: 15rem;
                    padding-left: 1.5rem;
                    // height: 4rem;
                    // line-height: 3.8rem;
                    // background: #FFFFFF;
                    border: 0.1rem solid #F1F1F1;
                    // font-size: 1.6rem;
					font-size: 1.2rem;
                    font-weight: 400;
					height: 3rem;

                    &::placeholder {
                        color: #C2C2C2;
                    }
                }

                .search_icon_block{
                    width: 5rem;
                    height: 3rem;
                    line-height: 3rem;
                    text-align: center;
                    background: #343579;
                    cursor: pointer;

                    .icon-sousuo{
                        font-size: 2rem;
                        color: #FFFFFF;
                    }
                }
            }

            .icon_close{
                color: rgba(174, 178, 183, 1);
                font-size: 2.4rem;
                cursor: pointer;
            }
        }

        
    }

    .my_material_content{
        // padding: 0 3rem 3.5rem 3rem;
		padding: 0;
        // height: calc(100% - 6.6rem);
		height: auto;
        position: relative;

        .material_content_top{
            padding: 1.3rem 0 2.1rem;
            height: 7rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-sizing: border-box;

            .material_content_top_title{
                font-size: 2rem;
                color: #030303;
            }

            .material_content_top_right{
                display: flex;
                align-items: center;

                .select_block{
                    background: #FFFFFF;
                    color: #1A1A1A !important;
                    margin-right: 2.3rem;

                    .icon-xiala{
                        color: #1A1A1A !important;
                    }
                }

                .check_all_block{
                    display: flex;
                    align-items: center;
                    font-size: 1.6rem;
                    color: #64686D;
                    cursor: pointer;

                    &.check_all{
                        color: #1A1A1A;
                    }

                    .check_block{
                        width: 2.4rem;
                        height: 2.4rem;
                        background: #EBECF4;
                        border: 0.1rem solid #64686D;
                        padding: 0.3rem;
                        margin-right: 0.7rem;

                        .check_block_body{
                            width: 100%;
                            height: 100%;
                            background: #343579;
                        }
                    }
                }
            }

        }

        .material_content_body{
            width: 100%;
            // height: calc(100% - 19rem);
			height: 100%;
            overflow-y: auto;
            position: relative;
			height: 30rem;
			&.material_content_body::-webkit-scrollbar{display: none;}

            .content_img_item{
				margin: 0 2rem 5rem 0;
				display: inline-block;
				width: 10rem;
				height: 10rem;
				border: 1px solid #f5f5f5;
				position: relative;
				cursor: pointer;
				padding: 0;
				img{
					object-fit: cover;
					width: 100%;
					height: 100%;
				}
				&.moodb{
					margin: 0 2rem 2rem 0;
				}
				.pin_block{
					.ant-checkbox-wrapper{
						height: 2.2rem;
						align-items: center;
						span{
							font-size: 1.4rem;
							line-height: 2.3rem;
						}
						.ant-checkbox-inner{
							width: 1.6rem;
							height: 1.6rem;
						}
						.ant-checkbox-inner::after{
							width: .571428571rem;
							height: .914285714rem;
						}
					}
				}
				&.active{
					opacity: 0.5;
				// border: 2px solid;
					border-radius: 1rem;
					transform: scale(0.9);
					img {
					}
					.pin_block{
						pointer-events:none;
						
					
					}
					.operate_file_block{
						pointer-events:none;
					}
				}
                .content_img_name{
                    width: 16.5rem;
                    height: 3.5rem;
                    line-height: 3.5rem;
                    white-space: nowrap;
                    text-align: center;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    font-size: 1.4rem;
                    color: #030303;
                }
            }
			.material_content_list_loding{
				text-align: center;
				img{
					height: 10rem;
				}
			}
            
        }

        .no_data_block{
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 99;

            &.loading_block{
                background: rgba(0,0,0,0.2);
            }
        }

        .material_confirm{
            width: 9.8rem;
            height: 3.6rem;
            line-height: 3.6rem;
            font-size: 1.4rem;
            text-align: center;
            margin: 0 auto;
            color: #FFFFFF;
            background: #343579;
            cursor: pointer;
            margin-top: 2rem;
        }

        .table_pagination{
            position: absolute;
            bottom: 3.5rem;
            left: 0;
            width: 100%;
            text-align: center;
            margin-top: 5rem;
        }
    }
}
</style>
