<template>
	<div>
		<avue-crud
			:data="tableData"
			v-model:page="page"
			@current-change="currentChange"
			@size-change="sizeChange"
			:table-loading="loading"
			@on-load="onLoad"
			@search-change="searchChange"
			:option="option"
			v-if="(deptId != '3' && deptId != '5')"
			>
			<template #id="{row}">
				<el-radio :label="row.id" v-model="radio" style="margin-right: -10px;">&nbsp;</el-radio>
			</template>
		</avue-crud>
		<avue-crud
			:data="tableData"
			v-model:page="page"
			@current-change="currentChange"
			@size-change="sizeChange"
			:table-loading="loading"
			@on-load="onLoad"
			@search-change="searchChange"
			:option="option2"
			@selection-change="selectionChange"
			v-else
			>
		</avue-crud>
		<footer class="el-dialog__footer">
			<el-button icon="el-icon-circle-plus" type="primary" @click="submitDispatchForm">确 定</el-button>
		  <el-button icon="el-icon-circle-close" @click="cancel">取 消</el-button>
		</footer>
	</div>
</template>

<script>
	import {getAllList as getServiceList} from "@/api/systemSet/service";
	import {getAllList as getEngList} from "@/api/systemSet/serverEng";
	import {dispatch, siteDispatch} from "@/api/order/order";
	import {mapGetters} from "vuex";
	export default {
		props:{
			selectionList: {
				type: Array,
				default: []
			}
		},
		data () {
			return {
				loading:false,
				query:{},
				tableData:[],
				page: {
				  pageSize: 10,
				  currentPage: 1,
					total:0,
					layout:'prev, pager, next, ->',
				},
				radio:'',
				checkList:[],
				option:{
					menu:false,
					border:true,
					searchSpan:19,
					searchMenuSpan:5,
					searchMenuPosition:'right',
					searchGutter:0,
					searchLabelWidth:88,
					emptyBtn:false,
					header:false,
					column:[
						{
							label: '服务商名称',
							prop: 'name',
							search:true
						},
						{
							label: '选择',
							prop: 'id',
							width: 65,
							slot:true,
							fixed:'right',
							align:'center'
						},
					],
				},
				option2:{
					menu:false,
					border:true,
					searchSpan:19,
					searchMenuSpan:5,
					searchMenuPosition:'right',
					searchGutter:0,
					searchLabelWidth:88,
					emptyBtn:false,
					header:false,
					selection: true,
					selectionFixed:'right',
					column:[
						{
							label: '工程师名称',
							prop: 'name',
							search:true
						},
					],
				}
			}
		},
		computed:{
			...mapGetters(["userInfo"]),
			deptId(){
				return this.userInfo.dept_id;//3.服务商 4.平台员工 5.服务商人员
			},
			ids() {
			  let ids = [];
			  this.selectionList.forEach(ele => {
			    ids.push(ele.id);
			  });
			  return ids.join(",");
			},
			checkIds(){
				let ids = [];
				this.checkList.forEach(ele => {
				  ids.push(ele.id);
				});
				return ids.join(",");
			}
		},
		methods:{
			selectionChange(list){
				this.checkList = list;
			},
			cancel(){
				this.$emit('childSubmit','cancel');
			},
			onLoad() {
				if((this.deptId == '3' || this.deptId == '5')){
					this.loading = true;
					this.query.role = '1';////1工程师
					getEngList(this.query,this.page).then(res=>{
						this.tableData = res.data.data.records
						this.page.total = res.data.data.total;
						this.loading = false;
					});
				}else{
					let row = this.selectionList[0];
					this.loading = true;
					this.query.province=row.province || row.provinceCode;
					this.query.city=row.city || row.cityCode;
					getServiceList(this.query).then(res=>{
						this.tableData = res.data.data
						// this.page.total = res.data.data.total;
						this.loading = false;
					});
				}
			},
			searchChange(params, done) {
			  this.query = params;
			  this.page.currentPage = 1;
			  this.onLoad();
			  done();
			},
			currentChange(currentPage){
			  this.page.currentPage = currentPage;
			},
			sizeChange(pageSize){
			  this.page.pageSize = pageSize;
			},
			submitDispatchForm(){
				// return
				if((this.deptId == '3' || this.deptId == '5')){
					if(this.checkList.length === 0){
						this.$message.warning("请选择至少一条数据");
						return
					}
					siteDispatch({
						orderIds:this.ids,
						empIds:this.checkIds
					}).then(res => {
						this.$message({
							type: 'success',
							message: '操作成功!',
						});
						this.$emit('childSubmit');
					}, error => {
						window.console.log(error);
					});
				}else{
					if(!this.radio){
						this.$message.warning("请选择至少一条数据");
						return
					}
					dispatch({
						orderId:this.selectionList[0].id,
						siteId:this.radio
					}).then(res => {
						this.$message({
							type: 'success',
							message: '操作成功!',
						});
						this.$emit('childSubmit');
					}, error => {
						window.console.log(error);
					});
				}
			},
		}
	}
</script>

<style>
</style>