<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_left" v-if="selectCode != 'Moodboard'">
					<div class="select_block">
						<generalMenu selectWidth="15rem" :dataList="disignTypeList" @setprintModel="menuChange" :item="disignType"></generalMenu>
						<!-- <a-select
							ref="select"
							v-model:value="disignType"
							:options="disignTypeList"
							@change="handleChange"
							:fieldNames="{ label: 'name', value: 'value' }"
						>
							<template #suffixIcon
								><span
									class="icon iconfont icon-xiala"
									style="color: #343579"
								></span
							></template>
						</a-select> -->
					</div>
					
				</div>
                <div class="my_material_header_right">
                    <div class="content_search_block generalModel_state">
						<div class="generalModel_state_item">
							<input class="search_input" :placeholder="$t('Material.inputContent1')" v-model="searchPictureName"  @keydown.enter="getLibraryList('')">
	                        <div class="gallery_btn" @click.stop="getLibraryList('')"><span class="icon iconfont icon-sousuo"></span></div>
						</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?.collectionChecked ? 'active':''  , selectCode == 'Moodboard' ? 'moodb':''  ]" >
						<img :src="item?.imgUrl" @click.stop="selectImgItem(item)">
						<sketchCategory v-if="selectCode == 'Sketchboard'" :disignTypeList="disignTypeList" :generateList="imgList" :item="item" :isSetSketchCategory="true" @upTypeSucced='upTypeSucced'></sketchCategory>
						<!-- <printCategory v-if="selectCode == 'Printboard'" :disignTypeList="disignTypeList" :generateList="imgList" :item="item" :isSetSketchCategory="true"></printCategory> -->
						<!-- <div v-if="selectCode == 'Sketchboard' || selectCode == 'Printboard'" class="pin_block">
							<a-checkbox v-model:collectionChecked="item.pin">{{ $t('Material.PIN') }}</a-checkbox>
						</div> -->
					</div>
					<div v-show="!load.isNoData" class="material_content_list_loding">
						<span class="page_loading" v-show="!isShowLoading" v-observe></span>
						<span v-show="isShowLoading">
							<a-spin size="large" />
						</span>
					</div>
					<!-- <div v-show="total > imgList?.length && total != 0" 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,inject, 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';
import { openGuide,driverObj__ } from "@/tool/guide";
import sketchCategory from "@/component/HomePage/sketchCategory.vue";
import printCategory from "@/component/HomePage/printCategory.vue";
import generalMenu from "@/component/HomePage/generalMenu.vue";

export default defineComponent({
    components: { sketchCategory,printCategory,generalMenu },

    props: ["msg",'disignTypeList'],
    setup(prop) {
        let myMaterialModalShow = ref(false)
        let store:any =useStore()
        let imgList:any = ref([])
        let isShowLoading:any = ref(false)
        let selectCode:any = ref('')
        let currentPage:any = ref(1)
        let searchPictureName = ref('')
        let isOldSearchPictureName = ref('')
        let pageSize = ref(12)
        let total = ref(0)
        let searcMaterialhName:any = ref('') //搜索名字
        // let disignTypeList:any = ref([
		let driver__:any = inject('driver__')
				
        //     ])
		let workspace:any = ref({})

		let {t} = useI18n()
		let usGenerateList = ref([])
	
		
		let disignType = ref({
			name:'',
			value:''
		})
		let load = ref({
			isShowMark:false,
			isNoData:false,
		})
        return{
            myMaterialModalShow,
            imgList,
            isShowLoading,
            selectCode,
            currentPage,
            searchPictureName,
			isOldSearchPictureName,
            pageSize,
            total,
            searcMaterialhName,
            // disignTypeList,
			workspace,
			t,
			driver__,
			usGenerateList,
			disignType,
			load,
        }
    },
	data (prop) {
		return {
			type_ : {
				type1:'material',
				type2:prop.msg
			},
            store: useStore(),
		}
	},
	directives:{
		observe:{
			mounted (el,binding) {
				// console.log(binding.instance);
				let this_:any = binding.instance
				this_.load.isShowMark = false
				this_.load.isNoData = false
				let parentDom = el.parentNode
				new IntersectionObserver(
				(entries, observer) => {
						// 如果不是相交，则直接返回
						// console.log(entries[0]);
						if (!entries[0].intersectionRatio) return;
						this_.currentPage += 1
						this_.getLibraryList('di')
					},
					// { root:worksPage }
				).observe(el);
				// const ob = new IntersectionObserver(callback,{
				// 	// root:null,
				// 	// threshold:[0.1]
				// })
				// ob.observe(el)
				// console.log(el)
				// // this.currentPage = 1
				// // this.pageSize = 12
				// let this_:any = binding.instance
				// function callback(entries:any, observer:any) {
				// 	entries.forEach((entry:any) => {
				// 		if (!entries[0].intersectionRatio) return;
				// 			console.log(123);
				// 		// this_.getLibraryList('di')
				// 	});
				// }
			},
		},
	},
	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.collectionChecked = 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.collectionChecked = 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.collectionChecked = false
							}
						})
					}
				});
				
			}
		},
		
	},
	
    methods:{
        init(code:any){
            this.selectCode = code
            // this.myMaterialModalShow = true
			this.currentPage = 1
			this.pageSize = 12
			this.imgList = []
			if(code != 'Moodboard'){
				this.disignType = this.disignTypeList[0]
			}
			this.load = {
				isShowMark:false,
				isNoData:false,
			}
			this.handleChange()
			
        },
		setUseGenerate(arr:any){
			let parent:any = this.$parent
			this.imgList.forEach((item:any) => {
				item.collectionChecked = false
			});
			if(parent.isUseGenerate){
			}else{
				arr.forEach((item:any) => {
					this.imgList.forEach((imgListItem:any) => {
						if(item.id == imgListItem.id){
							imgListItem.collectionChecked = true
						}
					});
				});
			}
			
		},
        selectImgItem(imgData:any){
			let parent:any = this.$parent
			if(this.selectCode != 'Moodboard'){
				imgData.categoryValue = imgData.level2TypeEnum.name
				imgData.category = imgData.level2TypeEnum.value
			}
			imgData.like = true
			if(parent.isUseGenerate){
				this.setUseGenerate([])
				let obj = {
					imgId : imgData.id,
					imgUr: imgData.imgUrl,
					level2Type:imgData.level2Type,
					categoryValue:imgData.categoryValue,
					category:imgData.category,
					designType:'library'
				}
				// parent.useGenerate.designType = 'collection'
				this.$emit('setLibrary',obj)
			}else{
				imgData.type_ = this.type_
				imgData.resData = JSON.parse(JSON.stringify(imgData))
				this.imgList.forEach((v:any)=>{
					v.categoryShow = false
				})
				let maxImg = 8
				if(this.selectCode == 'Sketchboard'){
					maxImg = 20
				}else if(this.selectCode == 'Printboard'){
					maxImg = 16
				}
				imgData.jsContent1 = this.t('uploadFile.jsContent1',{maxImg:maxImg})
				this.store.commit("addGenerateMaterialFils", imgData);
				let arr =  this.store.state.UploadFilesModule.moodboardMaterialFiles
				if(arr.length >=2 && this.selectCode == 'Moodboard'){
					if(this.driver__.driver){
						driverObj__.moveNext()
					}
				}
			}
			
        },
		menuChange(data:any){
			this.disignType = data
			this.handleChange()
		},
		handleChange(){
			this.currentPage = 1
			this.imgList = []
			this.load = {
				isShowMark:false,
				isNoData:false,
			}
            this.getLibraryList('')
		},
		upTypeSucced(){
			this.handleChange()
		},
        //选择所有的图片
        async getLibraryList(str:any){
			if(this.load.isShowMark && !this.load.isNoData)return
			let probjects = JSON.parse(JSON.stringify(this.store.state.Workspace.probjects))
			if(this.searchPictureName != this.isOldSearchPictureName || this.workspace.sex != probjects.sex){
				this.imgList = []
				this.currentPage = 1
			}else{
			}
			
			// if(str == 'di'){
			// 	this.currentPage = this.imgList?.[0]?.id != undefined?this.imgList?.length / this.pageSize+1:1
			// }
            let data = {
                level1Type:this.selectCode,
                level2Type: this.selectCode == 'Moodboard'?'':this.disignType.value,
				modelSex:probjects?.sex,
                page: this.currentPage,
                // page:this.currentPage,
                pictureName:this.searchPictureName,
                size:this.pageSize,
                // size:this.imgList?.length + this.pageSize,
				
            }
            this.isShowLoading = true
            await Https.axiosPost(Https.httpUrls.queryLibraryPage,data).then(
                (rv: any) => {
                    this.total = rv.total
					let aa:any = []
					if(rv.content.length == 0)this.load.isNoData = true
					rv.content.forEach((item:any,index:any) => {
						let arr
						if(this.type_.type2 == 'Sketchboard'){
							arr = this.store.state.UploadFilesModule.sketchMaterialFiles
							item.categoryValue = item.level2TypeEnum.name;
							item.category = item.level2TypeEnum.value;
							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.collectionChecked = true
									item.pin = v.pin
								}
							})
						}else{
						}
						aa.push(item)
					});
					
					this.isOldSearchPictureName = this.searchPictureName
					if((aa[aa.length-1].id == this.imgList[this.imgList.length-1]?.id) && aa?.[aa.length-1]?.id){
					}else{
						this.imgList.push(...aa)
					}
					this.workspace = probjects
					// this.imgList = aa
                    this.isShowLoading = false
                }
            ).catch((res)=>{
                this.isShowLoading = false
				this.load.isNoData = true
            });
        },
    }
})
</script>
<style lang="less">
.my_material_modal{
	height: calc(30rem*1.2);
    overflow-x: hidden;
	flex: 1;
	border-radius: 0;
	display: flex;
    flex-direction: column;
	&.my_material_modal::-webkit-scrollbar{display: none;}
    .ant-modal-close{
        width: calc(3.6rem*1.2);
        height: calc(3.6rem*1.2);
        position: absolute;
        top: calc(-1.8rem*1.2);
        right: calc(-1.8rem*1.2);
    }

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

    .ant-modal-body{
        background: #F2F3FB;
        height: 80vh;
        min-height: calc(72rem*1.2);
        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: calc(2.5rem*1.2);
		padding-bottom: calc(2rem*1.2);
		// background-color: #fff;
		position: sticky;
		top: 0;
		z-index: 2;
		.my_material_header_left{
			margin-right: auto;
			.select_block{
				border: calc(0.1rem* 1.2) solid #F1F1F1;
				margin-right: calc(2.3rem*1.2);
				height: auto;
				.ant-select-selector{
					height: 90%;
				}
			}
		}
        .my_material_header_right{
            display: flex;
            justify-content: space-between;
            align-items: center;
			.generalModel_state_item{
				margin: 0;
				.gallery_btn{
					margin-left: 2rem;
				}
			}
			
            .icon_close{
                color: rgba(174, 178, 183, 1);
                font-size: calc(2.4rem*1.2);
                cursor: pointer;
            }
        }

        
    }

    .my_material_content{
        // padding: 0 3rem*1.2) 3.5rem*1.2) 3rem*1.2);
		padding: 0;
        // height: calc(100% - 6.6rem*1.2));
		height: auto;
        position: relative;
		flex: 1;
		overflow: hidden;
        .material_content_top{
            padding: calc(1.3rem*1.2) 0 calc(2.1rem*1.2);
            height: calc(7rem*1.2);
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-sizing: border-box;

            .material_content_top_title{
                font-size: calc(2rem*1.2);
                color: #030303;
            }
			
            .material_content_top_right{
                display: flex;
                align-items: center;

                .select_block{
                    background: #FFFFFF;
                    color: #1A1A1A !important;
                    margin-right: calc(2.3rem*1.2);

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

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

                    &.check_all{
                        color: #1A1A1A;
                    }

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

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

        }

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

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

        .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: calc(9.8rem*1.2);
            height: calc(3.6rem*1.2);
            line-height: calc(3.6rem*1.2);
            font-size: calc(1.4rem*1.2);
            text-align: center;
            margin: 0 auto;
            color: #FFFFFF;
            background: #343579;
            cursor: pointer;
            margin-top: calc(2rem*1.2);
        }

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