<template>
	<div class="app-container">

		<el-form :model="queryParamsYetj" ref="queryForm1" size="small" :inline="true" label-width="68px"
			v-if="this.$auth.hasPermi('system:shopName:list')">
			<el-form-item label="店铺名称" prop="shopName">
				<el-select filterable v-model="queryParamsYetj.shopName" clearable placeholder="请选择店铺名称">
					<el-option v-for="(item,index) in options9" :label="item.name" :value="item.name">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="店铺类型" prop="mainIndustry">
				<el-select v-model="queryParamsYetj.mainIndustry" placeholder="请选择店铺类型" clearable>
					<el-option v-for="dict in dict.type.account_source" :key="dict.value" :label="dict.label"
						:value="dict.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
        <el-button type="primary" @click="calldata" v-show="!isDataSyn">
          同步入账数据
        </el-button>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery3">搜索
				</el-button>
				<el-button style="margin-left: 20px;" icon="el-icon-refresh" size="mini" @click="resetQuery">重置
				</el-button>
			</el-form-item>
		</el-form>
		<div v-show="txan">
			<p>账户余额：<span style="color: green;">{{balance}}</span>
				<el-button style="margin-left: 30px;" type="primary" size="small" @click="handleQuery2">提现</el-button>
			</p>
		</div>
		<div v-show="!txan">
			<p>总余额：<span style="color: green;">{{balanceAll}}</span>
			</p>
		</div>
		<el-table :data="tableDataOd" style="width: 100%">
			<el-table-column prop="shopName" label="店铺" width="200">
			</el-table-column>
			<el-table-column prop="balance" label="余额" width="200">
			</el-table-column>
			<el-table-column label="操作" width="200">
				<template slot-scope="scope">
					<el-button v-if="scope.row.shopName=='包赔账号'" size="small" type="primary"
						v-hasPermi="['system:goodsPaymentEntr:list']" @click="goBprz()">入账明细
					</el-button>
					<el-button v-else size="small" type="primary" v-hasPermi="['system:goodsPaymentEntr:list']"
						@click="handleDetailsYetj(scope.row.shopName)">入账明细
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="total1>0" layout="prev, pager, next" :total="total1" :page.sync="queryParamsYetj.pageNum"
			:limit.sync="queryParamsYetj.pageSize" @pagination="handleStatistics" />

		<el-dialog v-if="open1" title="申请提现" :visible.sync="open1" append-to-body width="1000px">
			<div style="padding: 10px;overflow: auto;">
				<center>
					<el-form ref="form1" :model="form1" :rules="rules" label-width="200px">
						<el-row>
							<el-col :span="12">
								<el-form-item label="账户类型" prop="payeeAccountName">
									<el-input v-model="form1.payeeAccountName" placeholder="如微信、支付宝、某某银行" />
								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<el-col :span="12">
								<el-form-item label="姓名" prop="payeeName">
									<el-input v-model="form1.payeeName" placeholder="请输入账号名称" />
								</el-form-item>
							</el-col>


						</el-row>
						<el-row>

							<el-col :span="12">
								<el-form-item label="提现账号" prop="payeeAccount">
									<el-input v-model="form1.payeeAccount" placeholder="请输入提现账号" />
								</el-form-item>
							</el-col>


						</el-row>
						<el-row>

							<el-col :span="12">
								<el-form-item label="余额(元)" prop="withdrawalAmountK">
									<el-input disabled v-model="form1.withdrawalAmountK" placeholder="余额" />
								</el-form-item>
							</el-col>



						</el-row>
						<el-row>

							<el-col :span="12">
								<el-form-item label="提现金额(元)" prop="txMoney">
									<el-input v-model="form1.txMoney" placeholder="请输入提现金额" />
								</el-form-item>
							</el-col>

						</el-row>
					</el-form>
				</center>

			</div>

			<div slot="footer" class="dialog-footer">
				<el-button type="danger" @click="updata()">修改收款账户
				</el-button>
				<el-button v-if="tixian==true" type="primary" @click="submitForm1">确 定
				</el-button>
				<el-button type="primary" v-else :loading="true">等待中</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
		<el-dialog
		  title="提示"
		  :visible.sync="txType"
		  width="25%"
		  :before-close="handleClose">
		  <div class="">
		    <p>请确认您的信息是否正确！</p>
		    <p><span style="color: red;">账户类型：</span> {{form1.payeeAccountName}}</p>
		    <p><span style="color: red;">姓名：</span>{{form1.payeeName}}</p>
		    <p><span style="color: red;">提现账号：</span>{{form1.payeeAccount}}</p>
		    <p><span style="color: red;">提现金额：</span>{{form1.txMoney}}</p>
		  </div>
		  <span slot="footer" class="dialog-footer">
		    <el-button @click="txType = false">取 消</el-button>
		    <el-button :loading='loadingTx' type="primary" @click="submitForm2()">确 定</el-button>
		  </span>
		</el-dialog>

		<el-dialog v-if="open3" title="入账明细" :show-close='false' :visible.sync="open3" append-to-body width="1200px">
			<div>
				<el-form :inline="true">
					<el-form-item label="开始日期" prop="beginTime">
						<div class="block">
							<el-date-picker v-model="queryParamsYetjmx.beginTime" value-format="yyyy-MM-dd" type="date"
								placeholder="选择日期" style="width: 200px;">
							</el-date-picker>
						</div>
					</el-form-item>
					<el-form-item label="结束日期" prop="endTime">
						<div class="block">
							<el-date-picker v-model="queryParamsYetjmx.endTime" type="date" value-format="yyyy-MM-dd"
								placeholder="选择日期" style="width: 200px;">
							</el-date-picker>
						</div>
					</el-form-item>
					<el-form-item label="类型" prop="type">
						<el-select v-model="queryParamsYetjmx.type" placeholder="请选择店铺类型" clearable>
							<el-option v-for="dict in typeOptions" :key="dict.value" :label="dict.label"
								:value="dict.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="商品编号" prop="goodsId">
						<el-input v-model="queryParamsYetjmx.goodsId" style="width: 200px;" placeholder="请输入商品编号"
							clearable @keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item label="分销编号" prop="channelId" v-if="this.$auth.hasPermi('system:channelId:list')">
						<el-input v-model="queryParamsYetjmx.channelId" style="width: 200px;" placeholder="请输入分销编号"
							clearable @keyup.enter.native="handleQuery" />
					</el-form-item>
					<el-form-item>
						<el-button type="primary" icon="el-icon-search" size="mini" @click="handleStatistics1">搜索
						</el-button>
					</el-form-item>
				</el-form>
				<el-table :data="tableDataOd1" :row-class-name="tableRowClassName" border style="width: 100%">
					<el-table-column prop="createTime" width="200px" label="时间">
						<template slot-scope="scope">
							<span>{{scope.row.createTime.slice(0,16)}}</span>
						</template>
					</el-table-column>
					<el-table-column prop="shopName" label="店铺">
					</el-table-column>
					<el-table-column prop="goodsId" label="商品编号">
					</el-table-column>
					<el-table-column prop="channelId" v-if="this.$auth.hasPermi('system:channelId:list')" label="分销编号">
					</el-table-column>
					<el-table-column prop="type" label="类型">
						<template slot-scope="scope">
							<span v-if="scope.row.type=='S'">商品收入</span>
							<span v-if="scope.row.type=='TS'">退款</span>
							<span v-if="scope.row.type=='TX'">提现</span>
							<span v-if="scope.row.type=='BPZC'">包赔支出</span>
							<span v-if="scope.row.type=='BPTK'">包赔退款</span>
							<span v-if="scope.row.type=='BPPF'">包赔赔付</span>
							<span v-if="scope.row.type=='ZHZC'">找回赔付</span>
							<span v-if="scope.row.type=='SJPF'">商家退款</span>
							<span v-if="scope.row.type=='PFKH'">赔付客户</span>
							<span v-if="scope.row.type=='SHCZ'">售后充值</span>
							<el-tooltip v-if="scope.row.type=='BH'" class="item" effect="dark" :content="scope.row.remark" placement="top-start">
								<span style="color: blue;">驳回</span>
							</el-tooltip>
						</template>
					</el-table-column>
					<el-table-column prop="amountMoney" label="金额变动">
						<template slot-scope="scope">
							<span style="color: blue;"
								v-if="scope.row.type=='S'||scope.row.type=='TS'||scope.row.type=='BPTK'||scope.row.type=='BPPF'||scope.row.type=='SJPF'||scope.row.type=='SHCZ'||scope.row.type=='BH'">+{{scope.row.amountMoney}}</span>
							<span style="color: red;"
								v-if="scope.row.type=='TX'||scope.row.type=='BPZC'||scope.row.type=='ZHZC'||scope.row.type=='PFKH'">-{{scope.row.amountMoney}}</span>
						</template>
					</el-table-column>
					<el-table-column prop="balance" label="余额">
						<template slot-scope="scope">
							<span style="color: green;">{{scope.row.balance}}</span>
						</template>
					</el-table-column>
					<el-table-column prop="status" label="状态">
						<template slot-scope="scope">
							<span v-if="scope.row.status=='S'">已打款</span>
							<span v-if="scope.row.status=='T'">提现中</span>
						</template>
					</el-table-column>
					<el-table-column prop="remark" width="250px" label="说明">
						<template slot-scope="scope">
							<div
								v-if="scope.row.type=='BPZC'||scope.row.type=='BPTK'||scope.row.type=='BPPF'||scope.row.type=='ZHZC'||scope.row.type=='SJPF'||scope.row.type=='PFKH'||scope.row.type=='SHCZ'">
								<p v-for="item in scope.row.remark.split(',')">{{item}}</p>
							</div>
							<div v-else>{{scope.row.remark}}</div>
						</template>
					</el-table-column>
				</el-table>
				<pagination v-show="total2>0" layout="prev, pager, next" :total="total2"
					:page.sync="queryParamsYetjmx.pageNum" :limit.sync="queryParamsYetjmx.pageSize"
					@pagination="handleStatistics1" />
			</div>

			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel1">关 闭</el-button>
			</div>
		</el-dialog>
		<el-dialog v-if="openbp" title="入账明细" :show-close='false' :visible.sync="openbp" append-to-body width="1200px">
			<accountDetails ref="accountDetails" />
		</el-dialog>
	</div>
</template>

<script>
	import {
		goodsPayment,
		getBalanceList,
		queryTBusShop,
		getTBusShopInfo,
		getBalance,
		tixian,
    callUpdateProcedure
	} from "@/api/data/orderDetails";
	import accountDetails from "../../data/accountDetails/index.vue";
	import {
		getBalanceYe,
	} from "@/api/data/accountDetails";
	import Cookies from 'js-cookie';
	export default {
		components: {
			accountDetails,
		},
		dicts: ['account_source'],
		data() {
			return {
				txType:false,
				loadingTx:false,
				tixian: true,
				// 余额
				balance: 0,
				tsje: 0,
				//店铺名称
				restaurants: [],
				srcList: [],
				input: '',
				open1: false,
				//提现按钮
				txan: false,
				//账户余额
				accountBalance: 0,
				options8: [],
				options9: [],
				options10: [],
				tableData: [],
				// 遮罩层
				loading: true,
				openbp: false,

				// 总条数
				total: 0,
				// 弹出层标题
				title: "",
				// 显示搜索条件
				showSearch: true,
				// 是否显示弹出层
				open: false,
				// 表单校验
				rules: {
					payeeAccountName: [{
						required: true,
						message: "账户类型不能为空",
						trigger: "blur"
					}],
					payeeName: [{
						required: true,
						message: "姓名不能为空",
						trigger: "blur"
					}],
					payeeAccount: [{
						required: true,
						message: "提现账号不能为空",
						trigger: "blur"
					}],
					txMoney: [{
						required: true,
						message: "提现金额不能为空",
						trigger: "blur"
					}],
				},
				typeOptions: [{
					label: '商品收入',
					value: 'S'
				}, {
					label: '退款',
					value: 'TS'
				}, {
					label: '提现',
					value: 'TX'
				}, {
					label: '包赔支出',
					value: 'BPZC'
				}, {
					label: '包赔退款',
					value: 'BPTK'
				}, {
					label: '包赔赔付',
					value: 'BPPF'
				}, {
					label: '找回赔付',
					value: 'ZHZC'
				}],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					publishStatus: null,
					deleteStatus: null,
					updateStatus: null,
					goodsName: null,
					gameName: null,
					partnerName: null,
					beginTime: this.$moment().subtract(7, 'days').format("YYYY-MM-DD"), // 当前时间往前推 7 天
					endTime: this.$moment().format("YYYY-MM-DD"), // 获取当前日期,
					paymentStatus: null,
					deptId: null,
					shopId: null,
					type: "SH",
					originalPrice: null,
				},
				tableDataOd: [],
				tableDataOd1: [],
				open2: false,
				open3: false,
				total1: 0,
				total2: 0,
				queryParamsYetj: {
					pageNum: 1,
					pageSize: 10,
				},
				queryParamsYetjmx: {
					pageNum: 1,
					pageSize: 10,
				},


				// 表单参数
				// form: {},
				form1: {},
				queryForm: {
					publishStatus: null,
					deleteStatus: null,
					updateStatus: null,
					goodsName: null,
					gameName: null,
					partnerName: null,
				},
				queryForm1: {
					pageNum: 1,
					pageSize: 10,
					shopName: null,
				},
				balanceAll: '', //总余额,

        isDataSyn : false,
			};
		},
		created() {
			this.user();
			this.getList();
		},
		mounted() {
			this.restaurants = this.loadAll();
		},
		methods: {

      calldata(){
        this.isDataSyn = true
        callUpdateProcedure()

      },


			updata() {
				this.form1.payeeAccountName = '';
				this.form1.payeeName = '';
				this.form1.payeeAccount = '';
			},
			submitForm1() {
				this.$refs["form1"].validate(valid => {
					if (valid) {
						if (Number(this.form1.txMoney) > Number(this.form1.withdrawalAmountK)) {
							this.$message.error('提现金额不能大于余额');
						} else if(Number(this.form1.txMoney)<100){
							this.$message.error('提现金额不能小于100');
						} else{
							if (this.form1.payeeAccountName != null && this.form1.payeeAccountName != '' && this
								.form1.payeeName !=
								null && this.form1.payeeName != '' && this.form1.payeeAccount != null && this.form1
								.payeeAccount !=
								'') {
								this.txType=true;
							} else {
								this.$message.error('请核对提现账户信息!');
							}
						}

					}
				})


			},
			submitForm2(){
				this.txType=false;
				this.tixian = false;
				this.loadingTx=true;
				let id = Cookies.get('id');
				let query = {
					shopId: id,
					txMoney: this.form1.txMoney,
					payeeAccountName: this.form1.payeeAccountName,
					payeeName: this.form1.payeeName,
					payeeAccount: this.form1.payeeAccount,
				}
				tixian(query).then(response => {
					this.loadingTx=false;
					window.location.reload()
				});
			},
			//店铺名称
			querySearch(queryString, cb) {
				var restaurants = this.restaurants;
				var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
				// 调用 callback 返回建议列表的数据
				cb(results);
			},
			createFilter(queryString) {
				return (restaurant) => {
					return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) !== -1);
				};
			},
			loadAll() {
				queryTBusShop().then(response => {
					// console.log(response)
					response.data.forEach((item) => {
						// console.log(item)
						this.options8.push({
							label: item.id,
							value: item.name,
						});
					});
				});
				return this.options8;
			},
			handleSelect(item) {

				// this.queryParams.shopId=item.address
			},
			user() {
				queryTBusShop().then(response => {
					this.options9 = response.data
				});
				getTBusShopInfo().then(res => {
					if (res.msg == "" || res.msg == null || res.msg == undefined) {
						this.txan = false
					} else {
						this.txan = true;
						this.queryParamsYetj.shopName = res.data.name;
						Cookies.set('id', res.data.id)
						this.form1 = res.data;
						let query = {
							shopId: res.data.id
						}
						getBalance(query).then(response => {
							this.form1.withdrawalAmountK = response.data.balance;
							this.balance = response.data.balance;
						})
					}
					getBalanceYe().then(res => {
						Cookies.set('balance', res.data.balance)
					})
					this.handleQuery3();
				})

			},

			/** 查询黑号信息列表 */
			getList() {
				this.loading = true;
				goodsPayment(this.queryParams).then(response => {
					this.tableData = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			/** 提现按钮操作 */
			handleQuery2() {
				if (this.balance == 0 || this.balance == '0' || this.balance == "") {
					// this.$message({
					// 	type: 'success',
					// 	message: '退款成功!'
					// })
					this.open1 = false;
				} else {
					this.open1 = true;
				}
			},
			// 入账明细
			handleQuery3() {
				getBalanceList(this.queryParamsYetj).then(response => {
					this.balanceAll = (Number(response.data.sum)).toFixed(2)
					this.tableDataOd = response.data.table.rows;
					this.total1 = response.data.table.total;
				});
			},
			handleStatistics() {
				this.handleQuery3()
			},
			goBprz() {
				this.openbp = true
				// this.$router.push({
				// 	path: "/guaranteedCompensation/accountDetails"
				// }).catch(() => {});
			},
			handleDetailsYetj(name) {
				Cookies.set('name', name)
				this.queryParamsYetjmx.shopName = name;
				goodsPayment(this.queryParamsYetjmx).then(response => {
					this.tableDataOd1 = response.rows;
					this.total2 = response.total;
					this.open3 = true;
				});
			},
			handleStatistics1() {
				let name = Cookies.get('name')
				this.handleDetailsYetj(name)
			},
			// 合计 指定某一列添加合计

			getSummaries(param) {
				const {
					columns,
					data
				} = param;
				const sums = [];
				columns.forEach((column, index) => {
					if (index === 0) {
						sums[index] = "合计";
						return;
					} else if (column.property == "balance") {
						//如果是经费（正常的加减法）
						const values = data.map((item) => Number(item[column.property]));
						if (!values.every((value) => isNaN(value))) {
							sums[index] = values.reduce((prev, curr) => {
								const value = Number(curr);
								var sum = 0;
								if (!isNaN(value)) {
									sum = Number(Number(prev) + Number(curr)).toFixed(2);
									return sum;
								} else {
									return prev;
								}
							}, 0);
							sums[index] += " ";
						}
					}
				});
				return sums;
			},
			tableRowClassName({
				row,
				rowIndex
			}) {
				if (rowIndex === 1 || rowIndex === 5 || rowIndex === 9) {
					return 'warning-row';
				} else if (rowIndex === 3 || rowIndex === 7) {
					return 'success-row';
				}
				return '';
			},

			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm1");
				this.handleQuery3();
			},
			cancel() {
				this.open1 = false;
				this.open2 = false;
				this.queryParamsYetj.shopName = '';
				this.resetForm("form");
			},
			cancel1() {
				this.open3 = false;
				this.queryParamsYetjmx.pageNum = 1;
				this.queryParamsYetjmx.beginTime = '';
				this.queryParamsYetjmx.endTime = '';
			},
      handleClose(){

      }


		}
	};
</script>
<style>
	.el-input__inner {
		background-color: #fff !important;
		border: 1px solid #dcdfe6 !important;
		color: #606266 !important;
	}

	.el-input.is-disabled .el-input__inner {
		background-color: #f5f7fa !important;
		border-color: #e4e7ed !important;
		color: #c0c4cc !important;
		cursor: not-allowed !important;
	}

	.el-table .warning-row {
		background: oldlace;
	}

	.el-table .success-row {
		background: #f0f9eb;
	}

	.father {
		height: 80px;
		line-height: 80px;
		font-size: 25px;
		display: flex;
		justify-content: space-around;
	}

	.el-icon-loading {
		color: green;
		margin-right: 5px;
	}

	.el-icon-message-solid {
		color: green;
		margin-right: 5px;
	}

	.el-icon-check {
		color: green;
		margin-right: 5px;
	}

	.el-icon-coin {
		color: green;
		margin-right: 5px;
	}
</style>
