<!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 src="../../../js/vue/vue.js"></script>
		<script src="../../../js/vue/element-ui/lib/index.js"></script>
		<script src="../../../js/myelement.js?v=20220615"></script>
		<script src="../../../js/vue/page.js?v=20220615"></script>
		
		<link href="../../../js/vue/element-ui/lib/theme-chalk/index.css" rel="stylesheet">
		<link href="../../../css/myelement.css?v=20220425" rel="stylesheet">
		<link href="../../../css/iconfont.css" rel="stylesheet">
		<link href="../../../css/page.css?v=20220425" rel="stylesheet">
		<link href="//at.alicdn.com/t/font_2374495_13ltsxm2eor.css" rel="stylesheet">
	</head>
	
	<body>
		<div id="vbody">
			<div id="page_root">
				<div class="topbar">
					<span>{{title}}</span>
					
					<div style="float: right; margin-right: 24px;">
						<el-button @click="download()">导出</el-button>
						<el-button-group style="margin-left: 3px;">
							<!-- <el-button v-if="buttons.upenddate"  @click="updateAgreementEnddate">设置终止日期</el-button> -->
							<!-- <el-button v-if="buttons.change" @click="updateAgreement">变更申请</el-button> -->
							<!-- <el-button v-if="buttons.create" @click="createAgreement">生成协议</el-button> -->
						</el-button-group>
						<el-button v-if="buttons.signingAgreement" :disabled="!selectedrow.id" @click="signingAgreementOuter">{{!selectedrow.has_sign?"签收协议":"取消签收"}}</el-button>
					</div>
				</div>
				<!-- 查询条件 -->
				<div class="topbar-line">
					<div class="query-icon">
						<i class="iconfont icon-query"></i>
					</div>
					<div class="query-bar">
						<h-form-filter ref="form1" 
							:form-attr="filterAttr" 
							:table-fields="filterFields" 
							:form-data="filterObj" 
							:isbuttonquery="true"
							
							v-on:on-formchange="onQuery" 
							v-on:show-popup="showFilterPopup"
							v-on:on-query="onQuery"
							v-on:on-init-query="onInitFilter"
							v-on:on-edit-query="onEditFilter"
							>
						</h-form-filter>
					</div>
				</div>
			
				<div class="versionNo">
					<h-table
						v-if="isRefresh"
						ref="table1"
						:tableloading="tableloading"
						:table-fields="tableFields" 
						:table-data="tableData" 
						:pagesize="pagesize"
						:pagenum="pagenum"
						:total="total"
						:table-height="tableHeight"
						:is-highlight-row="true"
						v-on:get-data="getData"
						v-on:row-click="rowClick"
						v-on:cell-click="cellClick"
					>
					</h-table>
				</div>
				<el-dialog title="签收协议" :visible.sync="reasonvisible" height="50%">
					<el-form label-width="100px">
					  <el-form-item label="签收协议号">
					    <el-input v-model="textarea2_no" placeholder="输入协议号" style="width: 100%;"></el-input>
					  </el-form-item>
					  <el-form-item label="备注">
					    <el-input
					      type="textarea"
					      :autosize="{ minRows: 2, maxRows: 10}"
					      placeholder="备注"
					      v-model="textarea2" style="width: 100%;">
					    </el-input>
					  </el-form-item>
					</el-form>
					<!-- 
					<el-input
					  type="textarea"
					  :autosize="{ minRows: 2, maxRows: 10}"
					  placeholder="备注"
					  v-model="textarea2">
					</el-input> -->
					<div  class="dialog-footer" style="float: right;margin-top: 30px;">
					  <el-button type="default" @click="reasonvisible = false">取 消</el-button>
					  <el-button type="primary"  @click="signingAgreement">确 定</el-button>
					</div>
				</el-dialog>
			</div>
			
			<div id="page_loading" style="position: absolute; top:0px; width: 100vw; height: 100vh;z-index:9999">
				<div class="el-dialog_header" style="text-align: right;border: none;position: absolute;top: 0px;right: 35vh;">
					<el-button style="border: none;" class="a" type="default"  @click="closePageLoading"><span style="font-size: 18px;" class="el-icon-close"></span></el-button>
				</div>
				<div class="el-dialog_body ">
					<div class="spinner">
						<div class="cube1"></div>
						<div class="cube2"></div>
					</div>
				</div>
			</div>
		</div>	
		
		<script type="text/javascript">
			var initlized = false;
			
			new ListVue({
				el: "#vbody",
				data: {
					pageAttr: {
						heightType: "page"
					},
					title: "政策备案明细",
					dataname: "agreementRecord",
					tabaleFieldsName: "policyReportSummary_",
					filterFieldsName: "Policy_All_S",
					reasonvisible:false,
					tableData: [],
					textarea2:"",
					textarea2_no: "",
					textarea2_time: "",
					isRefresh: true,
					filterAttr: {
						columnnumber: 4,
						labelwidth: "120px",
						labelposition: "right",
						size: "medium",
						border: "3px solid #c6c6c600"
					},
					orderby:"line.update_time desc,line.create_time desc,line.code desc",
					dataRequest: [
						 {
							isClientMode: false,
							code: "code",
							label: "value",
							name: "settle_type",
							dataname: "dictitem",
							filter: " parent_id='settle_type'"
						},
						{
							isClientMode: false,
							code: "code",
							label: "value",
							name: "control_type",
							dataname: "dictitem",
							filter: " parent_id='control_type'"
						},
						{
							isClientMode: false,
							code: "code",
							label: "name",
							name: "delivery_part_code",
							dataname: "deliveryOrganization",
						},
						{
							isClientMode: false,
							code: "code",
							label: "value",
							name: "manager_part_code",
							dataname: "dictitem",
							filter: " parent_id='manager-part'"
						},
						{
							isClientMode: false,
							code: "code",
							label: "value",
							name: "customer_part_code",
							dataname: "dictitem",
							filter: " parent_id='customer_part'"
						},
					],
					dataRequestObj: {},
				},
				created() {
					this.pageCode = "A4-3";
					this.getResoures();
				},
				
				mounted() {
					let me = this;
					if (this.dataRequest.length) {
						var result = {};
						this.loadRequestData(this.dataRequest, result, function(data) {
							me.dataRequestObj = data;
							if (me.filterFields.length) {
								var formFields_part_ = clone(me.filterFields);
								formFields_part_.map(e=>{
									if(e.field == "settle_type") {
										e.options = me.dataRequestObj.settle_type;
										
									}else if(e.field == "control_type") {
										e.options = me.dataRequestObj.control_type;
									}else if(e.field == "delivery_part_code") {
										e.options = me.dataRequestObj.delivery_part_code;
									}else if(e.field == "manager_part_code") {
										e.options = me.dataRequestObj.manager_part_code;
									}else if(e.field == "customer_part_code") {
										e.options = me.dataRequestObj.customer_part_code;
									}
								});
								
								me.filterFields = formFields_part_;
							}
							me.initData();
						});
					}
					let isdistributor = localStorage.isdistributor;
					if(isdistributor && isdistributor == "1"){
						let fields = [
						"invoice_limit_price",
						"delivery_rate",
						"delivery_amt",
						"business_rate",
						"cash_type",
						"rebate_type",
						"current_tick_rate",
						"current_tick_amt",
						"drained_rate",
						"drained_amt",
						"drained_arrest_rate",
						"drained_arrest_amt",
						"discount_common_rate",
						"discount_common_amt",
						"discount_business_rate",
						"discount_business_amt",
						"discount_trans_rate",
						"discount_trans_amt",
						"discount_flow_rate",
						"discount_businee_rate",
						"discount_businee_amt",
						"discount_flow_amt",
						"annual_cnt",
						"discount_total_amt",
						"discount_annual_rate","discount_annual_amt",
						"inventory_rate",
						"inventory_amt",
						"discount_total_rate",
						"remark"
						];
						let tableFields_ = [];
						this.tableFields.map(e=>{
							if(fields.indexOf(e.field) == -1) {
								if(e.field == "create_time" || e.field == "update_time"){
									e.isshow ="T";
								}
								tableFields_.push(e)
							}
						})
						this.filterFields = this.filterFields.filter(e=>{
							if(e.field == "customer_name")
								return false;
							else 
								return true;
						})
						this.tableFields = clone(tableFields_);
					}
					//this.initData();
					this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
						hideLoading();
					});
				},
				
				methods:{
					initData() {
						if (initlized) { return; }
						initlized = true;
						
						this.onQuery();
					},
					doQuery() {
						let me = this;
						let filter_ = " 1=1 ";
						if(me.filterFields.length > 0) {
							for(var i=0; i < me.filterFields.length; i++) {
								let fieldObj_ = me.filterFields[i];
								me.filterFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}
						
						for(var k in this.filterObj) {
							let k_val = this.filterObj[k];

							if(k == "product_name" || k== "product_id"){
								continue;
							}
							if(k != "agreement_status") {
								let fieldObj_ = this.filterFieldsObj[k];
								let type_ = fieldObj_.type;
								let field_ = fieldObj_.field;
								let fieldtype_ = "equal";
								if (fieldObj_.fieldname) {
									field_ = fieldObj_.fieldname
								}
								if (fieldObj_.fieldtype) {
									fieldtype_ = fieldObj_.fieldtype
								}
								if(field_ == 'flows.status' && k_val == "end") {
									filter_ += " and " + field_ + " = 'open' and end_line.b_trans_type = 'end' and (end_flow.status = 'working' or end_flow.status = 'refuse') "
								}else if(field_ == 'flows.status' && k_val == "trans") {
									filter_ += " and " + field_ + " = 'open' and end_line.b_trans_type = 'trans' and (end_flow.status = 'working' or end_flow.status = 'refuse')"
								}else if(field_ == 'flows.status' && k_val == "open") {
									filter_ += " and " + field_ + " = 'open' and (end_flow.status = 'open' or end_flow.status is null)"
								}else if(field_ == 'flows.status' && k_val == "input") {
									filter_ += " and (" + field_ + " = 'input' or " + field_ + " is null)" 
								}
								else {
									if (fieldtype_ == "like") {
										filter_ += " and " + field_ + " like '%" + k_val +"%'";
									}else if (type_ == "daterange" && k_val.length) {//期间筛选
										//
										var k_val_a = k_val[0];
										var k_val_b = k_val[1];
										
										if (fieldtype_ == "inScope") {//期间之内,不包含两端
											filter_ += " and (" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "')";
										}
										else if (fieldtype_ == "outScope") {//期间之外,不包含两端
											filter_ += " and (" + field_ + "<'" + k_val_a + "' or " + field_ + ">'" + k_val_b + "')";
										}
										else if (fieldtype_ == "inScopeInclude") {//期间之内,包含两端
											filter_ += " and ((" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "') or (" + field_ + "='" + k_val_a + "' or " + field_ + "='" + k_val_b + "'))";
										}
									}
									else {
										filter_ += " and " + field_ + " = '" + k_val +"'";
									}
								}
							}else {
								if(k_val == "close") {
									filter_ += " and flows.status = 'close'"
								}else if(k_val == "freeze") {
									filter_ += " and agreement_record.freeze = 1"
								}else if(k_val == "termination") 
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.termination_date < now() and agreement_record.end_date > now()"
								else if(k_val == "end")
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date < now()"
								else if(k_val == "no")
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date > now() and (agreement_record.termination_date is null or agreement_record.termination_date > now()) and (flows.status <> 'open' or flows.status is null) and (flows.status <> 'close')"
								else  
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date > now() and (agreement_record.termination_date is null or agreement_record.termination_date > now() ) and (flows.status = 'open')"
							}	
						}
						let isdistributor = localStorage.isdistributor;
						if(isdistributor){
							filter_ += " and customer_code = '" + localStorage.getItem('emp_code').substring(2) + "'";
						}
						let param_ = {
							isClientMode: false,
							dataname: this.dataname,
							filter: filter_,
							orderby:this.orderby,
							page: {
								no: this.pagenum,
								pagesize: this.pagesize
							},
						}
						me.tableloading = true;
						Server.call("root/data/getEntitySet", param_, function(result) {
							me.total = 0;
							me.tableData = [];
							var aDate = new Date();
							var date_ = dateFormat(aDate,"yyyy-MM-dd");
							if (result && result.data) {
								me.total = result.data.page.recordcount;
								var data_ = result.data.entityset;
							
								for(var i=0; i < result.data.entityset.length; i++) {
									
									if(data_[i].termination_date && data_[i].termination_date != "" &&  new Date(date_)  > new Date(data_[i].termination_date) ){
										data_[i].business_status = 'close'
									}else if(data_[i].status == 'open'){
										data_[i].business_status = 'open';
									}else{
										data_[i].business_status = 'ready';
									}
								}
								me.tableData = data_;
							}
							me.tableloading = false;
							me.tableDataAfter();
						});
					},
					signingAgreementOuter() {
						let me = this;
						if(this.selectedrow.has_sign) {
							Root.confirm('确定取消签收吗?', '提示', {
							  confirmButtonText: '确认',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								let param_ = {
									isClientMode :false,
									dataname:"agreement_record",
									id:this.selectedrow.id,
									has_sign:false,
									sign_remark:" ",
									sign_operate_code:" ",
									sign_operate_name:" "
								}
								Server.call("root/data/updateEntity", param_, function(result) {
									if(result && result.success) {
										Root.message({
											type:"success",
											message:"取消签收成功"
										})
									}
									me.doQuery()
								})
							}).catch(() => {
								Root.message({
									type: 'info',
									message: '已取消操作'
								});          
							});
							
						}else {
							this.reasonvisible = true;
						}
					},
					signingAgreement() {
						let me = this;
						var date_ = dateFormat(new Date(),"yyyy-MM-dd");
						
						let param_ = {
							isClientMode :false,
							dataname:"agreement_record",
							id:this.selectedrow.id,
							has_sign:true,
							sign_remark:this.textarea2,
							sign_no:this.textarea2_no,
							sign_time: date_,
							sign_operate_code:window.top.userinfo.employee.code,
							sign_operate_name:window.top.userinfo.employee.name
						}
						Server.call("root/data/updateEntity", param_, function(result) {
							if(result && result.success) {
								Root.message({
									type:"success",
									message:"签收成功"
								})
								
							}
							me.doQuery()
						})
						
						this.reasonvisible = false;
					},
					download (){
						let me = this;
						Root.message({
							type: 'warning',
							message: '正在下载,请稍后...'
						}); 
						document.getElementById("page_loading").style.display = "block"
						//let filter_ = " ifnull(end_flow.status,'') <> 'close' and ifnull(end_flow.status,'') <> 'open' ";
						let filter_ = " 1=1 ";
						if(me.filterFields.length > 0) {
							for(var i=0; i < me.filterFields.length; i++) {
								let fieldObj_ = me.filterFields[i];
								me.filterFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}
						
						for(var k in this.filterObj) {
							let k_val = this.filterObj[k];
							if(k != "agreement_status") {
								let fieldObj_ = this.filterFieldsObj[k];
								let type_ = fieldObj_.type;
								let field_ = fieldObj_.field;
								let fieldtype_ = "equal";
								if (fieldObj_.fieldname) {
									field_ = fieldObj_.fieldname
								}
								if (fieldObj_.fieldtype) {
									fieldtype_ = fieldObj_.fieldtype
								}
								if(field_ == 'flows.status' && k_val == "end") {
									filter_ += " and " + field_ + " = 'open' and end_line.b_trans_type = 'end' and (end_flow.status = 'working' or end_flow.status = 'refuse') "
								}else if(field_ == 'flows.status' && k_val == "trans") {
									filter_ += " and " + field_ + " = 'open' and end_line.b_trans_type = 'trans' and (end_flow.status = 'working' or end_flow.status = 'refuse')"
								}else if(field_ == 'flows.status' && k_val == "open") {
									filter_ += " and " + field_ + " = 'open' and (end_flow.status = 'open' or end_flow.status is null)"
								}else if(field_ == 'flows.status' && k_val == "input") {
									filter_ += " and (" + field_ + " = 'input' or " + field_ + " is null)" 
								}
								else {
									if (fieldtype_ == "like") {
										filter_ += " and " + field_ + " like '%" + k_val +"%'";
									}else if (type_ == "daterange" && k_val.length) {//期间筛选
										//
										var k_val_a = k_val[0];
										var k_val_b = k_val[1];
										
										if (fieldtype_ == "inScope") {//期间之内,不包含两端
											filter_ += " and (" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "')";
										}
										else if (fieldtype_ == "outScope") {//期间之外,不包含两端
											filter_ += " and (" + field_ + "<'" + k_val_a + "' or " + field_ + ">'" + k_val_b + "')";
										}
										else if (fieldtype_ == "inScopeInclude") {//期间之内,包含两端
											filter_ += " and ((" + field_ + ">'" + k_val_a + "' and " + field_ + "<'" + k_val_b + "') or (" + field_ + "='" + k_val_a + "' or " + field_ + "='" + k_val_b + "'))";
										}
									}
									else {
										filter_ += " and " + field_ + " = '" + k_val +"'";
									}
								}
							}else {
								if(k_val == "close") {
									filter_ += " and flows.status = 'close'"
								}else if(k_val == "freeze") {
									filter_ += " and agreement_record.freeze = 1"
								}else if(k_val == "termination") 
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.termination_date < now() and agreement_record.end_date > now()"
								else if(k_val == "end")
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date < now()"
								else if(k_val == "no")
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date > now() and (agreement_record.termination_date is null or agreement_record.termination_date > now()) and (flows.status <> 'open' or flows.status is null) and (flows.status <> 'close')"
								else  
									filter_ += " and agreement_record.freeze <> 1 and agreement_record.end_date > now() and (agreement_record.termination_date is null or agreement_record.termination_date > now() ) and (flows.status = 'open')"
							}	
						}
						let isdistributor = localStorage.isdistributor;
						if(isdistributor){
							filter_ += " and customer_code = '" + localStorage.getItem('emp_code').substring(2) + "'";
						}
						filter_ = encodeURI(filter_);
						var url = baseUrl + "file/downloadDataWithExcel?userId=" + window.top.userinfo.user.id + "&type=AgreementDlownload&dataName=" + this.dataname + "&name=" + "政策备案明细导出.xlsx" + "&filter=" +filter_;
						dealExportByPath(url, "政策备案明细导出.xlsx");
						window.onresize = function(e) {
							document.getElementById("page_loading").style.display = "none"
						};
						window.onblur = function(e) {
							document.getElementById("page_loading").style.display = "none"
						};
					},
					closePageLoading() {
						document.getElementById('page_loading').style.display = 'none';
					},
					onServerInitData(data) {
						var me = this.data;
						
						me.tableFields = data.tableFields;
						me.filterFields = data.filterFields;
						
						if(me.filterFields.length > 0) {
							for(var i=0; i < me.filterFields.length; i++) {
								let fieldObj_ = me.filterFields[i];
								
								me.filterFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}	
						if(me.tableFields.length > 0) {
							for(var i=0; i < me.tableFields.length; i++) {
								let fieldObj_ = me.tableFields[i];
								
								me.tableFieldsObj[fieldObj_.field] = fieldObj_;
							}
						}
					},
					createAgreement(){
						Root.popupParames = {
							width: "1300px",
							//height:"800px",
							url: "../agreement/policy/create_agreement.html",
						}; 
						Root.showPopup(Root.popupParames);
					},
					
					updateAgreementEnddate() {
						var me = this;
						var text_ = "批量终止日期";
						var id_ = "policy_summary_enddate";
						var url_ = "../agreement/policy/policy_summary_enddate.html?v=1";
							
						var config = {
							totab: true,
							width: "1500px",
							icon: "icon-product",
							text: text_,
							id: id_,
							url: url_,
							data: {},
							delta: "",
							callback: function(obj, callback) {
								//me.uploadFileAfter(obj);
								me.onQuery();
								if (callback) {
									callback();
								}
							}
						};
						this.doPopupByPublic(config);
					},
					
					updateAgreement (){
						var me = this;
						var row = this.selectedrow;
						
						//1. empty row
						if (!row || !row.id) {
							Root.message({
								type: 'warning',
								message: '请选择变更的数据'
							});  
							return false;
						}
						if (row.termination_date) {
							Root.message({
								type: 'warning',
								message: '已变更的数据无法再次变更'
							});  
							return false;
						}
						
						Root.showPopup({
							url: "../agreement/policy/policy_summary_edit.html",
							data:row,
							width: 1300,
							height: 550,
							callback: function(result, callback) {
								me.onQuery();
								if (callback) {
									callback();
								}
							}
						});
					},
					onCellClick(config, obj) {
						let me = this;
						var clickContext = this.selectCellField.clickContext;
						if(this.selectCellField.field == "status"  ){
							clickContext.url = "";
							if(obj.row.flow_id && obj.row.status != "input"){
								clickContext.url = "../approval/ApprovalList.html";
								
								clickContext.delta = {
									flow_id: obj.row.flow_id,
								}
								if(obj.row.end_flow_id) {
									clickContext.delta.flow_id = obj.row.end_flow_id
								}
								clickContext.width = "75vw";
								clickContext.height = "65vh";
								clickContext.hide_close = true;
							}
						}
						if (clickContext && clickContext.type == "popup" && clickContext.url != "") {
							config.combine({
								hide_close: clickContext.hide_close,
								url: clickContext.url,
								sceneCode: clickContext.sceneCode,
								data: obj.row,
								delta: clickContext.delta ? clickContext.delta : {},
								width: clickContext.width,
								height: clickContext.height,
							})
						}
						else {
							return false;
						}
						
						return true;
					},
					onshowFilterPopup(filter) {
						var me = this;
						var fieldObj_ = filter.obj;
						var filterValue = clone(me.filterObj);
						var field_ = fieldObj_.field;
						if ("product_name" == field_) {
							Root.showPopup({
								url: "../md/product/popup/product_list.html",
								width: 800,
								height: 550,
								type: "onlySimple",
								dataname: "factory_product",//"md_product_act",
								callback: function(result, callback) {
									filterValue[field_] = result.row.product_name;
									me.filterFieldsObj["product_code"] = {type:"equal",field:"product_code"}
									filterValue["product_code"] = result.row.product_code;
									me.filterObj = filterValue;
									me.onQuery();
									if (callback) {
										callback();
									}
								}
							});
						}else if ("customer_name" == field_) {
							Root.showPopup({
								url: "../md/customer/popup/customer_list.html",
								
								width: 800,
								height: 550,
								callback: function(result, callback) {
									filterValue[field_] = result.row.md_description;
									me.filterObj = filterValue;
									me.onQuery();
									if (callback) {
										callback();
									}
								}
							});
						}else if ("business_depart_name" == field_) {
							Root.showPopup({
								url: "../org/department/popup/department_list.html",
								width: 800,
								height: 550,
								callback: function(result, callback) {
									filterValue[field_] = result.row.name;
									me.filterObj = filterValue;
									me.onQuery();
									if (callback) {
										callback();
									}
								}
							});
						}
					}
				}
			});
		</script>
		<style type="text/css">
			.a{
				background-color: transparent;
			}
			.a:hover{
				background-color: transparent;
			}
		</style>
	</body>
</html>