<template>
	<div class="my-container">
		<!-- <transition-group name="fade" style="position: relative;width: 100%;display: flex;"> -->
		<div class="bill-left" v-show="isShow==='home'" key="item">
			<div class="bill-tab">
				<div class="bill-title">导航</div>
				<side-tree :propConfig="treeConfig" @onChange="onChangeTree" ref="sideTree" />
			</div>
		</div>
		<div class="bill-main" v-show="isShow==='home'" key="item2">
			<module-view :propConfig="config" ref="moduleView" @pagination="getDataList" @search="getDataList"
				@resert="onRefresh" @clickHandle="clickHandle" @detail="openEdit" @onRefresh="onRefresh">
			</module-view>
		</div>
		<!-- 新增/编辑弹窗 -->
		<div class="detil" v-show="isShow==='add'" key="item3">
			<by-tool :propConfig="toolConfig" @clickHandle="insideTools" ref="byTool" />
			<div class="box-shadow">
				<by-form :propConfig="addConfig" ref="addFormId">
					<template v-slot:organizationCode="{value}" class="clearfix">
						<vxe-input v-model="value.organizationCode" :disabled="isOrganizationCode" style="width: 100%;"></vxe-input>
					</template>
					<template v-slot:k3ServerUrl="{value}" class="clearfix">
						<!-- <i class="el-icon-question" title="私有云必须配置金蝶云星空产品地址,K3Cloud/结尾,若为公有云则必须置空"></i> -->
						<!-- <vxe-input v-model="value.k3ServerUrl" style="width:calc(100% - 25px);margin-left: 10px;"></vxe-input> -->						
						<vxe-input v-model="value.k3ServerUrl" style="width: 27.2%;"></vxe-input>
						<div style="display: inline-block;margin-left: 17px;color: #ababab;">
							<span style="color: red;margin-right: 5px;">*</span>私有云必须配置金蝶云星空产品地址,K3Cloud/结尾,若为公有云则必须置空
						</div>
					</template>
					<template v-slot:status class="clearfix">
						<el-radio class="fl" style="margin-top: 8px;" v-model="radio" :label="0">正常</el-radio>
						<el-radio class="fl" style="margin-top: 8px;" v-model="radio" :label="1">禁用</el-radio>
					</template>
				</by-form>
			</div>
		</div>
		<!-- </transition-group> -->
	</div>
</template>
<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import api from "@/api/currency";
	import Assembly from "@/components/Assembly/material.vue";
	@Component
	export default class Supplier extends Vue {
		isShow = 'home'
		timeNum = 0;
		timer : any = null
		popTitle : any = ''
		radio : any = 0
		dialogFormVisible : boolean = false
		categoryIds : any = '';
		isOrganizationCode : boolean = true
		toolConfig = {
			tools: {
				return: true,
				save: true,
			}
		}
		treeConfig = {
			attr: {
				retConfig: {
					id: 'id',
				},
				defaultExpandAll: true,
				label: 'name',
				// resType: 'data'
			},
			// request: {
			// 	url: '/maindata/maindataOrganizationCategory/treeUseList',
			// 	method: 'GET'
			// }
		}
		config : any = {
			attr: {
				calculateH: true
			},
			search: {
				attr: {
					size: 'mini'
				},
				columns: [
					[{
						span: 6,
						label: '名称',
						prop: 'name',
						component: 'by-input',
						labelWidth: '70px',
						compConfig: {
							attr: {
								placeholder: '请输入名称',
								clearable: true
							},
						},
					}, {
						span: 6,
						label: '负责人',
						prop: 'contacts',
						component: 'by-input',
						labelWidth: '70px',
						compConfig: {
							attr: {
								placeholder: '请输入负责人',
								clearable: true
							}
						}
					},
					{
						span: 6,
						label: '状态',
						prop: 'status',
						labelWidth: '70px',
						component: 'by-select',
						compConfig: {
							attr: {
								placeholder: '请选择状态',
								clearable: true,
								size: 'small',
								data: [{
									value: 0,
									label: '正常'
								}, {
									value: 1,
									label: '禁用'
								}]
							}
						}
					},
					]
				]
			},
			tool: {
				tools: {
					add: true,
					search: true,
					refresh: true
				}
			},
			table: {
				attr: {
					size: 'mini',
					align: 'left',
				},
				columns: [{
					title: '名称',
					field: 'name',
					isDetail: true,
					width: 150,
				},
				{
					title: '负责人',
					field: 'contacts',
					width: 120,
				},
				{
					title: '联系电话',
					field: 'phonenum',
					width: 150,
				},
				{
					title: '手机号码',
					field: 'telephone',
					width: 150,
				},
				{
					width: 300,
					title: '财务系统类型',
					field: 'financeType2',
				},
				{
					width: 80,
					title: '状态',
					field: 'status',
					component: Assembly,
				}, {
					width: 120,
					title: '操作',
					action: true,
					plugins: [{
						icon: 'el-icon-edit',
						name: '编辑',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).openEdit(item)
							}
						}
					}, {
						name: '删除',
						event: {
							click: (item : any) => (this as any).doDelete(item)
						}
					}]
				}]
			},
		}
		addConfig = {
			attr: {
				size: 'medium',
				rules: {
					categoryName: [{
						required: true, message: '请选择机构分类', trigger: 'change'
					}],
					name: [{
						required: true, message: '请输入名称', trigger: 'blur'
					}],
					organizationCode: [{
						required: true, message: '请输入机构代码', trigger: 'blur'
					}],
					contacts: [{
						required: true, message: '请输入负责人', trigger: 'blur'
					}],
					telephone: [{
						required: true, message: '请输入手机号码', trigger: 'blur'
					}],
					// phonenum: [{
					// 	required: true, message: '请输入联系电话', trigger: 'blur'
					// }],
					financeType: [{
						required: true, message: '请输入财务系统类型', trigger: 'change'
					}],
					province: [{
						required: true, message: '请选择地址', trigger: 'change'
					}],
					address: [{
						required: true, message: '请输入详细地址', trigger: 'blur'
					}],
				}
			},
			columns: [
				[
					{
						span: 6,
						label: '机构分类',
						prop: 'categoryName',
						component: 'select-tree',
						compConfig: {
							attr: {
								label: 'name',
								clearable: true,
								retConfig: {
									categoryId: 'id',
									categoryName: 'name',
								},
								defaultExpandAll: true
							},
							request: {
								url: '/maindata/maindataOrganizationCategory/treeUseList'
							}
						}
					},
					{
						span: 6,
						label: '名称',
						prop: 'name',
						component: 'by-input',
					},
					{
						span: 6,
						label: '机构代码',
						prop: 'organizationCode',
						component: 'by-input',
						compConfig:{
							attr:{
								onceEdit:true,
								placeholder:'保存后不能修改'
							}
						}
						// slot: true,
					},
					{
						span: 6,
						label: '负责人',
						prop: 'contacts',
						component: 'by-input',
					},
				],
				[
					{
						span: 6,
						label: '手机号码',
						prop: 'telephone',
						component: 'by-input',
					},
					{
						span: 6,
						label: '联系电话',
						prop: 'phonenum',
						component: 'by-input',
					},
					{
						span: 6,
						label: '电子邮箱',
						prop: 'email',
						component: 'by-input',
					},
				],
				[
					{
						span: 6,
						labelWidth: '110px',
						label: '财务系统类型',
						prop: 'financeType',
						component: 'by-select',
						compConfig: {
							attr: {
								placeholder: '请选择财务系统类型',
								clearable: true,
								size: 'small',
								data: [
									{
										value: 'kingdee',
										label: '金蝶K3'
									},
									// {
									// 	value: 'yonbip',
									// 	label: '用友U8'
									// },
								]
							}
						}
					},
				],
				[
					{
						span: 24,
						label: 'URL地址',
						slot: true,
						prop: 'k3ServerUrl',
					},
				],
				[
					{
						span: 6,
						label: '登录授权ID',
						slot: true,
						prop: 'k3AppId',
						component: 'by-input',
					},
					{
						span: 6,
						label: '用户名称',
						slot: true,
						prop: 'k3UserName',
						component: 'by-input',
					},
					{
						span: 6,
						label: '数据中心ID',
						slot: true,
						prop: 'k3AcctId',
						component: 'by-input',
					},
					{
						span: 6,
						label: '应用秘钥',
						slot: true,
						prop: 'k3AppSec',
						component: 'by-input',
					},
				],
				[
					{
						span: 6,
						label: '纳税人识别号',
						slot: true,
						prop: 'taxnum',
						component: 'by-input',
					},
				],
				
				[{
					span: 18,
					label: '地址',
					prop: 'province',
					component: 'by-area',
					labelWidth: '100px',
					compConfig: {
						attr: {
							province: 'province', //省
							provinceCode: 'provinceCode', //省编码
							city: 'city',   //市
							cityCode: 'cityCode',  //市编码
							county: 'region',  //县/区
							countyCode: 'regionCode',   //县/区编码
							town: 'street', //街道
							townCode: 'streetCode', //街道编码
						}
					}
				}],
				[
					{
						span: 24,
						label: '详细地址',
						slot: true,
						prop: 'address',
						component: 'by-input',
					},
				],
				[
					{
						span: 6,
						label: '状态',
						slot: true,
						prop: 'status',
					},
				]
			]
		}
		mounted() {
			this.timer = setInterval(() => {
				this.getDataList()
			}, 300)
		}
		//点击树获取数据
		onChangeTree(e : any) {
			this.categoryIds = e.id;
			var ids:any = e.id;
			if(e.id[0]==0) ids = 0;
			let loading = this.$loading({ target: '.main-container' });
			api.byCategoryPage({ categoryId: ids, type: 3 }, 'maindataOrganizationCategory').then((res : any) => {
				if (res.code === 200) {
					(this.$refs.moduleView as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView as any).setPage(page);
					loading.close();
				} else loading.close();
			}).catch(() => loading.close());
		}
		// 内页工具栏方法
		insideTools(e : any) {
			// console.log(e);
			if (e === 'onReturn') this.isShow = 'home';
			if (e === 'onSave') this.confirm();
		}
		// 保存新增/编辑
		confirm() {
			(this as any).$refs.addFormId.validate().then(() => {
				let query = (this as any).$refs.addFormId.getValue();
				query.status = this.radio;
				query.accountId = this.$store.getters.userInfo.userId;
				// 手机验证
				let reg = /^1[3456789]\d{9}$/;
				// let reg2 = /^0\d{2,3}-\d{7,8}$/;
				if (query.telephone && !reg.test(query.telephone)) return this.$message.warning('请输入正确手机号码');
				// if (query.phonenum && !reg2.test(query.phonenum)) return this.$message.warning('请输入正确联系电话');
				if(query.phonenum){
					if (query.phonenum.length>20 || query.phonenum[0]==' ') {
						return this.$message.warning('请输入正确联系电话');
					}
				}
				if (!query.telephone) query.telephone = null;
				if (!query.phonenum) query.phonenum = null;
				this.dialogFormVisible = false;
				// console.log(query);
				let loading = this.$loading({ target: '.main-container' });
				if (this.popTitle === '新增') {
					api.saveList(query, 'maindataMaterialManufacturer').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('保存成功');
							// (this as any).$refs.sideTree.request();
							this.isShow = 'home'; // 展示类型
							this.getDataList();
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				} else if (this.popTitle === '编辑') {
					api.updateList(query, 'maindataMaterialManufacturer').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							this.$message.success('编辑成功');
							// (this as any).$refs.sideTree.request();
							this.isShow = 'home'; // 展示类型
							this.getDataList();
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				}

			}).catch(() => {});
		}
		// 获取列表数据
		getDataList() {
			if (!this.$refs.moduleView) {
				if (this.timeNum > 5) {
					clearInterval(this.timer)
				}
				this.timeNum++;
				return
			}
			clearInterval(this.timer)
			let query = (this.$refs.moduleView as any).getQuery();
			if (this.categoryIds) query.categoryIds = this.categoryIds;
			// console.log('表单字段 ==> ', query);
			this.getPageList(query)
		}
		getPageList(query : any) {
			let loading = this.$loading({ target: '.main-container' });
			api.treeList('maindataOrganizationCategory').then((treeRes : any) => {
				if(treeRes.code==200){
					api.pageList(query, 'maindataMaterialManufacturer').then((res : any) => {
						loading.close();
						if (res.code === 200) {
							
							var data:any = [{
								id: 0,
								name: '机构分类',
								children: treeRes.data
							}];
							(this as any).$refs.sideTree.setData(data);
							
							res.data.records.map((v : any) => {
								if (v.financeType === 'kingdee') v.financeType2 = '金蝶K3';
								if (v.financeType === 'yonbip') v.financeType2 = '用友U8';
							});
							(this.$refs.moduleView as any).setTableValue(res.data.records);
							let page = {
								pageNo: res.data.current, //当前页
								pageSize: res.data.size, //每页条数
								total: res.data.total //总条数
							};
							(this.$refs.moduleView as any).setPage(page);
						} else this.$message.error(res.msg);
					}).catch(() => loading.close());
				}
			}).catch(() => loading.close());
		}
		// 工具栏方法
		clickHandle(e : any) {
			if (e === 'onAdd') this.onAdd();
		}
		// 打开新增
		onAdd() {
			this.isShow = 'add'; // 展示类型
			this.popTitle = '新增'
			this.dialogFormVisible = true;
			this.radio = 0
			this.isOrganizationCode = false
			this.$nextTick(() => (this as any).$refs.addFormId.setValue());
		}
		// 打开编辑
		openEdit(e : any) {
			// console.log('打开编辑 ==> ', e);
			this.isShow = 'add'; // 展示类型
			this.popTitle = '编辑'
			let loading = this.$loading({ target: '.main-container' });
			api.single({ id: e.id }, 'maindataMaterialManufacturer').then((res : any) => {
				if (res.code === 200) {
					let data = res.data;
					this.radio = data.status;
					this.dialogFormVisible = true;
					if(res.data.organizationCode){
						this.isOrganizationCode = true
					}else{
						this.isOrganizationCode = false
					}
					this.$nextTick(() => (this as any).$refs.addFormId.setValue(data));
					loading.close();
				} else loading.close();
			}).catch(() => loading.close());
		}
		// 操作删除
		doDelete(item : any) {
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let loading = this.$loading({ target: '.main-container' });
				api.deleteList({ ids: item.id }, 'maindataMaterialManufacturer').then((res : any) => {
					loading.close();
					if (res.code === 200) {
						// (this as any).$refs.sideTree.request();
						this.getDataList();
						this.$message.success('删除成功');
					} else this.$message.error(res.msg);
				}).catch(() => loading.close());
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 刷新/重置
		onRefresh() {
			(this as any).$refs.moduleView.clearSearch();
			// (this as any).$refs.sideTree.request();
			this.categoryIds = '';
			this.getDataList();
		}
	}
</script>
<style lang="scss" scoped>
	.my-container {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		padding: 16px;
		height: 100%;

		.bill-left {
			position: relative;
			border-right: solid #EEE 1px;
			padding-right: 16px;
			flex-shrink: 0;

			.bill-tab {
				width: 210px;
				height: 100%;
				transition: all .5s;
				overflow: auto;
			}

			.tab-title {
				font-size: 16px;
				padding-bottom: 16px;
				width: 200px;
			}
		}

		.bill-main {
			width: calc(100% - 16px);
			box-sizing: border-box;
			margin-left: 16px;
			position: relative;
			height: 100%;
			overflow-y: hidden;
		}

		.detil {
			width: 100%;

			.box-shadow {
				box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
				padding: 20px;
				border-radius: 4px;
				border: 1px solid #e6ebf5;
				background-color: #fff;
				overflow: hidden;
				color: #303133;
				-webkit-transition: .3s;
				transition: .3s;
				margin-top: 20px;
			}
		}
	}
</style>