<template>
	<view class="column flex-1 hb">
		<view class="align-center justify-between p20">
			<view class="justify-start fs28 align-center">
				<u-icon name="bookmark-fill" color="#1677ff" size="28"></u-icon>
				<view>自定义标签</view>
			</view>
			<view class="fs28" @click="isShowSelectAll=!isShowSelectAll">
			{{isShowSelectAll?'取消多选':'多选'}}</view>
		</view>
		<view class="column flex-1 scrolly plr20" >
			<view class="justify-start" v-for="(item,index) in companylabel"
				:key="index">
				<u-checkbox-group @change="selectlist(index)">
					<u-checkbox :checked="item.checked" shape="circle" activeColor="#FF6C47" :name="true"
						label="" labelColor="#333" v-if="isShowSelectAll"/>
				</u-checkbox-group>
				<view class="justify-start align-center ptb16 flex-1" 
				style="border-bottom: 2rpx solid #e7e7e7;" >
					<u-icon name="list" color="#666" size="24"></u-icon>
					<view class="fs28 ml12">{{item.tag}}</view>
				</view>
			</view>
		</view>
		<view class="footbtn">
			<view  v-if="isShowSelectAll" class="justify-between p20">
				<view class="align-center justify-between" v-if="isShowSelectAll">
					<u-checkbox-group @change="selectAll">
						<u-checkbox :checked="isSelectAll" shape="circle" activeColor="#FF6C47" 
						:name="true" label="全选" labelColor="#333" />
						<text class="fs24 base-color-9 ml12">已选 {{selectedCount}}
							个</text>
					</u-checkbox-group>
				</view>
				<view class="justify-center ">
					<button class="base-bg-red radius100 colorf h62 fs28 lh62 mlr10 plr60"
						@click="changeProhibit">删除</button>
				</view>
			</view>
			<view class='base-bg radius50 m20 p20 center colorf'
			 @click="addLable" v-else>新增标签</view>
		</view>
		<u-modal :show="showlist" title="新增标签" @confirm="confirmchange"
		 :showCancelButton="true" @cancel="cancel" style="flex: 0;">
			<view class="slot-content">
				<u-input placeholder="请输入新增标签" v-model="addlable"></u-input>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		getcompanyTaglist,
		addCompanyLabel,
		deleteCompanyLabel
	} from "@/api/courseManage.js"
	export default {
		data() {
			return {
				isShowSelectAll:false,
				showlist:false,
				addlable:'',
				companylabel:[],
				isSelectAll:false,
				isSelected:false,
				selectedCount:0,
				selectidAll:[]
			}
		},
		mounted() {
			this.getcompanylabel()
		},
		methods: {
			changeProhibit(){
				//删除标签
				const params={
					tagIds:this.selectidAll
				}
				console.log(this.selectidAll)
				deleteCompanyLabel(params).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon: 'none',
							title: '标签删除成功'
						})
						this.getcompanylabel()
					}else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			getcompanylabel(){
				getcompanyTaglist().then(res=>{
					if(res.code==200){
						this.companylabel = res.data.map(item => {
							return {
								...item,
								checked: false,
							}
						})
					}else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			addcompanySingle(){
				const params={
					tag:this.addlable
				}
				addCompanyLabel(params).then(res=>{
					if(res.code==200){
						uni.showToast({
							icon: 'none',
							title: '标签添加成功'
						})
						this.getcompanylabel()
					}else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
			},
			addLable(){
				this.showlist=!this.showlist
			},
			selectlist(i){
				// 单选 /反选
				console.log(i)
				let arr = {
					...this.companylabel[i],
					checked: !this.companylabel[i].checked
				}
				this.$set(this.companylabel, i, arr)
				this.selectidAll = this.companylabel.filter(item => item.checked).map(item => item.tagId)
				console.log(this.selectidAll)
				this.isSelectAll = this.companylabel.every(item => item.checked)
				if (this.isSelectAll) {
					this.companylabel = this.companylabel.map(item => {
						return {
							...item,
							checked: this.isSelectAll
						}
					})
				}
			},
			selectAll(){
				// 全选
				this.isSelectAll = !this.isSelectAll
				console.log(this.isSelectAll)
				this.companylabel = this.companylabel.map(item => {
					// 每一项的 checked为 全选的状态
					return {
						...item,
						checked: this.isSelectAll
					}
				})
				console.log(this.companylabel)
			},
			confirmchange(){
				if(this.addlable==''){
					uni.showToast({
						icon: 'none',
						title: "请输入标签"
					})
				}else{
					this.addcompanySingle()
					this.showlist=!this.showlist
				}
			},
			cancel(){
				this.showlist=!this.showlist
			}
		}
	}
</script>

<style lang="scss">

</style>
