<template>
	<el-drawer
		title="任务分配"
		v-model="visible"
		:size="'90%'"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<div>
			<el-button type="primary" @click="handlePrint" style="position: absolute;right: 20px;">打印</el-button>
			<el-card style="width: 100%" header="基本信息" shadow="never">
				<el-descriptions>
					<el-descriptions-item label="名称">{{
						mtaskObj.material_out_name
					}}</el-descriptions-item>
					<el-descriptions-item label="任务编号">{{
						mtaskObj.number
					}}</el-descriptions-item>
					<el-descriptions-item label="任务量">{{
						mtaskObj.count
					}}</el-descriptions-item>
					<el-descriptions-item label="开始时间">{{
						mtaskObj.start_date
					}}</el-descriptions-item>
					<el-descriptions-item label="结束时间">{{
						mtaskObj.end_date
					}}</el-descriptions-item>
				</el-descriptions>
			</el-card>
			<!-- 选择人员 -->
			<el-form
					:model="form"
					:rules="rules"
					label-width="100px"
					ref="form"
					style="margin-top: 10px;margin-bottom:50px"
				>
				<el-row>
					<el-col :span="24">
						<el-form-item label="操作人员">
							<el-checkbox-group
								v-model="choose_user"
								@change="userChange"
							>
								<el-checkbox class="checkboxItem" v-for="item in userList" :key="item.id" :label="item.name" :value="item.id">
								{{ item.name }}
								</el-checkbox>
							</el-checkbox-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider style="margin-top:0"></el-divider>
				<el-row>
					<el-col :md="6" :sm="8" v-for="item in cUserList" :key="item.id">
						<el-form-item :label="item.handle_user_name">
							<el-input-number
								v-model="item.count"
								:precision="0"
								:min="1"
								style="width: 90%"
								controls-position="right"
							/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div class="footer">
				<el-button type="primary" @click="mtaskb_submit">提交</el-button>
			</div>
		</div>
		<el-dialog v-model="printVisible" width="1200px">
			<print :baseData="mtaskObj" type="mtask"  @closePrint="printVisible=false"/>
		</el-dialog>
	</el-drawer>
</template>
<script>
import print from "./../setting/print/A4.vue";
export default {
	components: { print },
	props: {
		mgroup: {
			type: String,
			default: "",
		},
		dept: {
			type: String,
			default: "",
		},
	},
	emits: ["success", "closed"],
	data() {
		return {
			form: {},
			apiObj: null,
			loading: false,
			visible: false,
			isSaveing: false,
			printVisible:false,
			mtaskObj:{},
			userList:[],
			cUserList:[],//
			choose_user:[],//已选人员id
			rules: {
				count_use: [
					{
						required: true,
						message: "请输入领取数量",
						trigger: "blur",
					},
				],
			},
		};
	},
	mounted() {
		this.$nextTick(() => {
		    this.getUsers();
		})
		
	},
	methods: {
		open(data) {
			let that = this;
			this.visible = true;
			this.mtaskObj = data;
			that.cUserList = data.mtaskb;
			that.choose_user = [];
			if(that.cUserList.length>0){
				that.cUserList.forEach(item=>{
					that.choose_user.push(item.handle_user);
				})
			}
		},
		
		getUsers() {
			let that = this;
			that.$API.system.user.list.req({ belong_dept: that.dept, page: 0 }).then(res => {
			// that.$API.system.user.list.req({ page:1,page_size:60}).then(res => {
				that.userList = res;
			})
		},
		
		userChange(){
			let that = this;
			let average = Math.floor( that.mtaskObj.count/that.choose_user.length);
			let remainder = that.mtaskObj.count%that.choose_user.length;//取余
			that.cUserList = [];
			console.log('that.choose_user',that.choose_user);
			that.userList.forEach(item=>{
				if(that.choose_user.indexOf(item.id)>-1){
					let obj = {};
					obj.handle_user_name = item.name;
					obj.handle_user = item.id;
					obj.count = average;
					that.cUserList.push(obj);
				}
			})
			console.log('that.cUserList',that.cUserList);
			for(let i=0;i<remainder;i++){
				that.cUserList[i].count += 1;
			}
		},
		
		mtaskb_submit() {
			let that = this;
			console.log(that.cUserList);
			let sum = 0;
			that.cUserList.forEach(item => {
				sum+=item.count;
			});
			console.log(sum-that.mtaskObj.count)
			if(sum-that.mtaskObj.count==0){
				that.isSaveing = true;
				that.$API.pm.mtaskbAdd.req(that.mtaskObj.id,that.cUserList).then((res) => {
					that.isSaveing = false;
					that.visible = false;
					this.$emit("success");
					that.$message.success("提交成功");
				}).catch((e) => {
					that.isSaveing = false;
				});
			}else{
				that.$message.error("分配数量与任务总数不匹配，请确认后再提交");
			}
		},
		
		handleEditSuccess() {},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
		//打印
		handlePrint(){
			this.printVisible = true;
		},
	},
};
</script>

<style scoped>
.checkboxItem{
	width:80px;
}
.footer{
	position: absolute;
    bottom: 0px;
    width: 100%;
    z-index: 10;
    text-align: center;
    background: #ffffff;
    height: 40px;
}
</style>
