<!doctype html>
<html>
	<head>
		<meta charset="utf-8"><meta http-equiv="Expires" content="0"><meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cache-control" content="no-cache"><meta http-equiv="Cache" content="no-cache">
		<title>收货地址列表</title>
		
		<script type="text/javascript">
			var pageVue = null;
			function loadJsCss(callback) {
				var jscss_urls = [];
				window.top.initJsCss(document, jscss_urls, callback);
			};
			
			function initVue() {
				new ListVue({
					el: "#vbody",
					data: {
						dataname: "md_org_shipto",
						title: "收货地址列表",
						orderby: "update_time desc",
						filterfieldClick: {},
						tablefieldClick: {},
						formfieldClick: {},
						tablebuttonclick: [],
						cbuttons_r: {},
						page_id: "B-011",
						iscustomer: false, // 是否经销商账号
					},
					created() {
						this.iscustomer = window.top.vue.isCustomer
					},
					
					mounted() {
						var me = this;
						var page_id = clone(this.page_id);
						if (window.top.tab.selected.option.page_id) {
							this.title = window.top.tab.selected.option.menutitle
							page_id = window.top.tab.selected.option.page_id;
						}
						getPageByPageId(page_id, function(result){
							me.cbuttons_r = result.buttons_r;
							//获取数据
							me.initData();
							
							me.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
								hideLoading();
							});
						})
					},
					
					methods:{
						initData() {
							var me = this;
							
							this.onQuery(function(result, callback) {//查询后的回调,用于获取字段的
								if (result.meta && result.meta[me.dataname] && result.meta[me.dataname].fields) {
									var metas = clone(result.meta[me.dataname].fields);
									var filterFields_ = [];
									var tableFields_ = [];
									
									metas.map(f=>{
										f.isshow = "T";
										
										if (me.iscustomer && (f.field == "md_org__code" || f.field == "md_org__name")) {
											f.isshow = "F";
											f.isfilter = false;
										}
										
										if (f.isfilter) {
											filterFields_.push(clone(f));
										}
										else {
											var filter_f = clone(f);
											filter_f.isshow = "F";
											filterFields_.push(filter_f);
										}
										
										tableFields_.push(clone(f));
									})
									if (!me.filterFields || (me.filterFields && me.filterFields.length == 0)) {
										me.filterFields = clone(filterFields_);
										me.tableFields = clone(tableFields_);
										
										//字段数组转字段obj,目的为了筛选时获取字段属性
										me.fieldsToFieldsObj();
										
										//设置字段事件
										me.tableFieldClick();
									}
									me.tableButtonClick();
								}
								
								if (callback) {
									callback();
								}
							});
						},
						
						tableButtonClick() {
                            var me = this;
                            var tablebuttonClick = [];
                            tablebuttonClick = [
								{
									isbuttonshow: me.cbuttons_r.add,type:"primary",
									code: "add",isselected: false,classname: "",
									onclick: function(obj) {
										me.onEditData('add', {})
									}
								},
								{
									isbuttonshow: me.cbuttons_r.edit,type:"primary",
									code: "edit", isselected: true, classname:"",
									disabled: function(selectRowList) {
                                        var bo_ = true ;
                                        if (selectRowList[0]) {
                                            bo_ = false
                                        }
                                        return bo_; 
                                    },
									onclick: function(obj) {
										me.onEditData('edit', obj.selectedList[0]);
									}
								},
								{
									isbuttonshow: me.cbuttons_r.open, type:"success",
									code: "open", isselected: true, classname:"",
									disabled: function(selectRowList) {
								        var bo_ = true ;
								        if (selectRowList[0] && selectRowList[0].state_code != "Open") {
								            bo_ = false
								        }
								        return bo_; 
								    },
									onclick: function(obj) {
										var row_ = clone(obj.selectedList[0]);
										Root.confirm('确定生效吗?', '生效提示', {
										  confirmButtonText: '生效',
										  cancelButtonText: '取消',
										  type: 'warning'
										}).then(() => {
											var row = {
												id: row_.id,
												state_code: "Open",
												state_name: "生效",
											}
											me.onSaveData("open", row);
										}).catch(() => {
											Root.message({
												type: 'info',
												message: '已取消'
											});          
										});
										
										
									}
								},
								{
									isbuttonshow: me.cbuttons_r.repeal, type:"danger",
									code: "repeal", isselected: true, classname:"",
									disabled: function(selectRowList) {
								        var bo_ = true ;
								        if (selectRowList[0] && selectRowList[0].state_code == "Open") {
								            bo_ = false
								        }
								        return bo_; 
								    },
									onclick: function(obj) {
										var row_ = clone(obj.selectedList[0]);
										Root.confirm('确定作废BU' + row_.name + '吗?', '作废提示', {
										  confirmButtonText: '作废',
										  cancelButtonText: '取消',
										  type: 'warning'
										}).then(() => {
											var row = {
												id: row_.id,
												state_code: "Close",
												state_name: "作废",
											}
											me.onSaveData("repeal", row);
										}).catch(() => {
											Root.message({
												type: 'info',
												message: '已取消'
											});          
										});
										
										
									}
								},
								{
									isbuttonshow: me.cbuttons_r.delete, type: "danger",
									code: "delete", isselected: true, classname:"",
								    disabled: function(selectRowList) {
								        var bo_ = true ;
								        if (selectRowList[0] && selectRowList[0].state_code == "Input") {
								            bo_ = false
								        }
								        return bo_; 
								    },
									onclick: function(obj) {
										me.onDeleteData(obj.selectedList[0]);
									}
								}
								
							]
							this.tablebuttonclick = tablebuttonClick;
						},
						
						tableFieldClick() {
							
						},
						
						onEditData(sceneCode, row) {
							var me = this;
							if (sceneCode == "edit" && !row.id) {
								Root.message({
									type: 'warning',
									message: '请选择要编辑的数据'
								});  
								return;
							}
								
							//3. popup
							var config = {
								totab: false, //true: 以Tab导航的方式打开
								width: "1100px",
								height: "550px",
								icon: "icon-product",
								text: "收货地址编辑",
								id: "customer_shipto_edit" + sceneCode + row.id,//totab: true时需设置,用于判断是否已打开此页面
								url: "module/md/page/customer/page/customer_shipto_edit.html",
								data: row,
								delta: null,
								sceneCode: sceneCode,//"refuseedit",//"approval", //"add"//"browse",
								callback: function(obj, callback) {
									me.onQuery();
									if (callback) {
										callback();
									}
								}
							};
							me.doPopupByPublic(config);
						},
						
						onSaveData(type, row) {
							var me = this;
							var param = {
								dataname: me.dataname,
								data: {},
							}
							param.data[me.dataname] = row;
							Server.call("root/data/saveEntity", param, function(result) {
								console.log(result);
								if (result.success) {
									var message = "已生效"
									if (type == "repeal") {
										message = "已作废"
									}
									Root.message({
										type: 'success',
										message: message
									}); 
									
									me.onQuery();
								}
							});
						},
						
						onDeleteData(row) {
							let me = this;
							if (!row.id) {
								Root.message({
									type: 'warning',
									message: '请先选择要删除的数据'
								});  
								return
							}
							
							Root.confirm('确定删除' + row.name + '吗?', '删除提示', {
							  confirmButtonText: '删除',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								let param = {
									id: row.id,
									dataname: me.dataname,
								}
								Server.call("root/data/deleteEntity", param, function(result) {
									Root.message({
										type: 'success',
										message: '删除成功!'
									});
									me.onQuery();
								});
							}).catch(() => {
								Root.message({
									type: 'info',
									message: '已取消删除'
								});          
							});
						},
						
						
					}
				});
			};
			
			loadJsCss(function () {
				initVue();
			});
		</script>
		<style>
			/* 在vue.js中 v-cloak 这个指令是防止页面加载时出现 vuejs 的变量名而设计的 */
			[v-cloak] {
				display: none !important;
			}
		</style>
		
	</head>
	
	<body style="margin: 0px; ">
		<div v-cloak id="vbody">
			<div id="page_root">
				<div class="topbar">
					<span>{{title}}</span>
				</div>
				<div class="h_dialog__body">
					<h-table
						v-if="isRefresh && tableFields.length"
						ref="table1"
						:table-fields="tableFields" 
						:table-data="tableData" 
						:is-edit-table-data="isEditTableData"
						:pagesize="pagesize"
						:pagenum="pagenum"
						:total="total"
						:table-height="tableHeight"
						:table-field-click="tablefieldClick"
						:table-buttons-click="tablebuttonclick"
						:tableloading="tableloading"
						:isdraggableorder="true"
						:is-highlight-row="true"

						v-on:get-data="getData"
						v-on:edit-data="editData"
						v-on:del-data="delData"
						v-on:row-click="rowClick"
						v-on:order-fields="orderTableFields"
					>
					</h-table>
				</div>
			</div>
		</div>
		
		<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 50vh;">
			<div class="spinner">
			  <div class="cube1"></div>
			  <div class="cube2"></div>
			</div>
		</div>
		
	</body>
</html>