<template>
	<view class="content">
		<view class="content_item_box">
			<view class="content_item">
				<view class="">
					<text style="color: red;">*</text>任务名称
				</view>
				<view class="">
					<radio-group v-if="!isSupervisor" class="radio-group" name="" @change="task($event)">
						<label class="radio" style="margin-right: 20rpx" v-for="(item,key) in data1" :key="item.name">
							<radio color="#317AFF" :value="item.name" />
							<text>{{item.name}}</text>
						</label>
					</radio-group>
					<radio-group v-else class="radio-group" name="" @change="task($event)">
						<label class="radio" style="margin-right: 20rpx" v-for="(item,key) in data2" :key="item.name">
							<radio color="#317AFF" :value="item.name" />
							<text>{{item.name}}</text>
						</label>
					</radio-group>
				</view>
			</view>
			<view class="content_item">
				<view class="">
					<text style="color: red;">*</text>单位名称
				</view>
				<superwei-combox :isJSON="true" :candidates="companies" keyName="companyName" placeholder="请选择或输入"
					v-model="companyName" @select="bbb($event)" @input="getCompanies($event)"></superwei-combox>

			</view>
			<view class="content_item">
				<view class="">
					<text style="color: red;">*</text>协助机构
				</view>

				<checkbox-group v-if="!isSupervisor" class="checkbox-group" @change="aaa($event)">
					<label v-for="(item,key) in supervisorList " :key="key" class="checkbox">
						<checkbox color="#317AFF" style="transform: scale(0.7);" :value="item.id" />
						<text>{{item.name}}</text>
					</label>
				</checkbox-group>
				<checkbox-group v-else class="checkbox-group" @change="aaa($event)">

					<label v-for="(item,key) in og " :key="key" class="checkbox">

						<checkbox color="#317AFF" style="transform: scale(0.7);" :value="item.id" />

						<text>{{item.name}}</text>

					</label>

				</checkbox-group>
			</view>
		</view>
		<view class="button">
			<button @tap="apply()">确定</button>
			<!-- <button type="default">取消</button> -->
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			rightAction: {
				type: Function,
				default () {
					return () => {}
				}
			},
		},
		data() {
			return {
				dataSource: [{
						label: '选项1',
						id: '1'
					},
					{
						label: '选项2',
						id: '2'
					},
				],
				data1: [{
					name: "熟悉演练",
					value: 1
				}],
				data2: [{
					name: "双随机",
					value: 1,
				}, {
					name: "联络指导",
					value: 0
				}],
				users: [],
				supervisorList: [],
				og: [],
				companies: [],
				curry: 0,
				prams: {
					task: "",
					company: "",
					supervisor: []
				},
				companyName: "",
				selectedYear: 2022,
				selectedMonth: 0,
				OrganizationId: "",
				isSupervisor: false
			}
		},
		created() {
			let roles = uni.getStorageSync("user").roles
			console.log(uni.getStorageSync("user"));
			let isSupervisor = roles.find(item => {
				return item.name.indexOf('检查') != -1
			})
			isSupervisor ? this.isSupervisor = true : this.isSupervisor = false

			this.OrganizationId = uni.getStorageSync("user").organizationId
			this.getSupervisor()
			this.getOg()
			this.getUsers()
			this.getCompanies()
			this.selectedMonth = new Date().getMonth() + 1
			this.selectedYear = new Date().getFullYear()
		},
		methods: {
			getSupervisor() {
				this.$request.get('/api/Users/Inspectors', {}, (data, res) => {

					this.supervisorList = res
					console.log(this.supervisorList);
				})
			},
			getOg() {
				let p = {
					PageNumber: 1,
					PageSize: 9999
				}
				this.$request.get('/api/Organizations', p, (data, res) => {
					this.og=[]
					for(let i=0;i<res.items.length;i++){
						if(res.items[i].level=="squadron"){
							this.og.push(res.items[i])
						}
					}
					// this.og = res.items
				})
			},
			aaa(e) {
				console.log(e);
				this.prams.supervisor = e.detail.value
			},
			task(e) {
				console.log(e);
				this.prams.task = e.detail.value
			},
			getCompanies(e) {
				let params = {
					companyName: e,
					OrganizationId: this.OrganizationId,
					PageNumber: 1,
					PageSize: 50
				}
				this.$request.get('/api/Companies', params, (data, res) => {
					console.log(res);
					this.companies = res.items
				})
			},
			getUsers() {

				let p = {
					OrganizationId: this.OrganizationId,
					OrganizationLevel: 'squadron',
					ContainsChildren: true,
					PageNumber: 1,
					PageSize: 9999
				}
				this.$request.get('/api/Users', p, (data, res) => {
					console.log(res);
					this.users = res.items
				})
			},
			companyname(e) {
				console.log(e);
				this.companyName = this.companies[e.detail.value].companyName
			},
			bbb(e) {
				console.log(e,7800);
				this.prams.company=e.id
			},
			apply() {
				if (!this.prams.task || !this.prams.company || !this.prams.supervisor.length) {
					return this.$alert.showError("请填写完整")
				}
				let selectedMonth = this.selectedMonth < 10 ? "0" + this.selectedMonth : this.selectedMonth;
				let selectedTime = this.selectedYear + '-' + selectedMonth + '-' + '01'
				console.log(this.prams,444);
				let body = {
					month: selectedTime,
					taskName: this.prams.task,
					taskType: this.prams.task,
					companyId: this.prams.company,
					organizationId: this.OrganizationId,
					supervisorIds: this.isSupervisor ? [] : this.prams.supervisor,
					assitantOrganizationIds: this.isSupervisor ? this.prams.supervisor : [],
					creationType: '申领任务',
					approvalStatus: '待处理'
				}
				// return
				this.$request.post('/api/PlanTasks', body, (data, res) => {
					console.log(res);
					if (res.status == 500) {
						this.$alert.showError("该单位已创建任务")
					} else {
						this.$alert.showError("创建成功")
					}
					this.$parent.apply();
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		height: 100%;
		display: flex;
		flex-direction: column;
		padding: 0 20rpx;

		.content_item_box {
			flex: 1;
			overflow: auto;
		}

		.content_item {
			display: flex;
			align-items: center;
			padding: 20rpx 0;

			view {
				margin: 0 20rpx;
			}

			.input {
				flex: 1;
				height: 60rpx;
				border: 2rpx solid #E4E7EC;
			}
		}

		.button {
			display: flex;
			height: 150rpx;
		}

		.checkbox-group,
		.radio-group {
			flex: 1;

			overflow: auto;

			.checkbox,
			.radio {
				margin-bottom: 10rpx;
				margin-right: 10rpx;

				radio {
					transform: scale(0.7);
				}
			}
		}
	}
</style>
