<template>
	<view class="enterprise-list">
		<view class="list-wrap">
			<u-sticky bgColor="#fff" class="main-sticky">
				<view class="search-wrap">
					<view class="input-wrap">
						<view class="input">
							<u-search bgColor='#fff' :showAction='false'
								:placeholder="$t('base.common.inputPlaceholder')" @search="searchList"
								@clear='searchList' v-model="form.keyword">
							</u-search>
							<view class="search-btn" @click="searchList">{{$t("customer_meter.search")}}</view>
						</view>
					</view>
				</view>

			</u-sticky>
			<template v-if="driverList.length">
				<u-swipe-action>
					<view v-for="(item, index) in driverList" :key="item.id" class="business-card">
						<!-- <u-swipe-action-item :name="item.userName" :options="options" @click="onVehicleChange($event,item)"> -->
							<view class="business-card-info">
								<text class="info">{{item.dealerName}}</text>
								<text class="no">{{item.dealerPhone}}</text>
								<view class="businessStatus-info">
									<u--image width="100rpx" height="88rpx"
										:src="item.statusFlag==1?'/subpages/static/czzn-img/state/start.png':'/subpages/static/czzn-img/state/stop.png'">
									</u--image>
								</view>
							</view>
						<!-- </u-swipe-action-item> -->
					</view>
				</u-swipe-action>
			</template>

			<template v-else>
				<u-empty mode="list" icon="https://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</template>
		</view>
		<u-modal :show="showAdd" @confirm="confirm" ref="addModal" showCancelButton closeOnClickOverlay
			@cancel="showAdd=false" @close="showAdd=false">
			<view class="invite-box">
				<view class="invite-box-title">绑定客商</view>
				<u--input placeholder="请输入客商手机号" border="surround" v-model="phone"></u--input>
			</view>
		</u-modal>

	<!-- 	<uni-fab ref="fab" horizontal="left" vertical="bottom" buttonColor="#3c9cff" @trigger="trigger"
			@fabClick="onClickRight" /> -->
	</view>
</template>

<script>
	import {
		getDealerList,
	} from "../../api/driver";
	import {
		getDriverTenantList,
	} from "../../api/tenant.js";
	export default {
		data() {
			return {
				options: [{
						text: this.$t("base.button.disableEnable"),
						name: "disable",
						style: {
							backgroundColor: '#3c9cff'
						}
					},
					//     {
					// 	text: this.$t("base.button.delete"),
					// 	name: "delete",
					// 	style: {
					// 		backgroundColor: '#f56c6c'
					// 	}
					// }
				],
				current: undefined,
				showAdd: false,
				showEnable: false,
				keyword: "",
				active: 0,
				value: "",
				phone: "",
				driverList: [],
				form: {
					keyword: "",
				},
				tablist: [{
						name: this.$t("customer_meter.all"),
					},
					{
						name: this.$t("customer_meter.enable"),
					},
					{
						name: this.$t("customer_meter.deactivate"),
					},
				],
				page: {
					pages: 1,
					size: 10,
				},
				total: 0,
				hasMore: true,
			};
		},
		components: {},
		onLoad() {
			this.bindDealerList();
		},
		methods: {
			//搜索列表
			searchList() {
				this.page.pages = 1
				this.hasMore = false
				this.bindDealerList()
			},
			//显示启用弹窗
			onVehicleChange(e, item) {
				var that = this;
				if (e.index == 0) {
					//停用客商
					uni.showModal({
						content: (item.statusFlag == 1 ? that.$t("dealer.stopDealer") : that.$t(
							"dealer.startDealer")),
						showCancel: true,
						cancelText: that.$t("settings.cancelText"),
						confirmText: that.$t("settings.confirmText"),
						success: function(res) {
							if (res.confirm) {
								console.log("开始处理客商信息、", item);
								dealerUpdateStatus(item.id, item.statusFlag == 1 ? 0 : 1).then(res => {
									if (res.statusCode == 200) {
										uni.showToast({
											title: (item.statusFlag == 1 ? that.$t(
												"dealer.stopIng") : that.$t(
												"dealer.startIng")),
											icon: "none",
											duration: 2000,
										});
										that.driverList = [];
										that.bindDealerList()
									}
								})
							}
						}
					})

				}
				//  else {
				// 	//删除
				// 	uni.showModal({
				// 		// title: '提示',
				// 		content: that.$t("driver.deleteVehcile"),
				// 		showCancel: true,
				// 		cancelText: that.$t("base.search.cancelText"),
				// 		confirmText: that.$t("base.search.submitText"),
				// 		success: function(r) {
				// 			if (r.confirm) {
				// 				dealerDeleteVehicle(item).then(res => {
				// 					if (res.statusCode == 200) {
				// 						that.bindDealerList()
				// 					}
				// 				})
				// 			}
				// 		}
				// 	})
				// }
			},
			//加载更多
			scrolltolower() {
				if (this.hasMore) {
					this.page.pages += 1;
					this.bindDealerList();
				}
			},

			bindDealerList() {
				this.form.appUserType="4"
				getDealerList(this.form).then((res) => {
					if (this.hasMore) {
						this.driverList = [
							...this.driverList,
							...res.data.data,
						];
					} else {
						this.driverList = res.data.data;
					}
					console.log(this.driverList);
					// debugger
					this.driverList.forEach((item) => {
						item.enableFlag = item.statusFlag == "1" ? true : false;
					});
					this.total = res.data.data.total;
					if (this.driverList.length >= this.total) {
						this.hasMore = false;
					} else {
						this.hasMore = true;
					}
				});
			},
			//点击右侧加号
			onClickRight() {
				this.showAdd = true;
			},
			changeTab(item) {
				this.active = item.index;
			},
			//绑定客商
			confirm() {
				bindDealerByPhone({
					phone: this.phone
				}).then((res) => {
					if (res.statusCode == 200) {
						console.log("绑定的信息为、",res.data);
						if(res.data.code==0 && !res.data.data.tenantList){
							uni.setStorageSync('tenantInfo', res.data.data.tenantList)
						}
						this.dealerPhone = ""
						this.showAdd = false
						this.driverList = [];
						uni.showToast({
							title: "绑定成功",
							icon: "none",
							duration: 2000,
						});
						this.page.pages = 1;
						this.bindDealerList();
					} else {
						this.dealerPhone = ""
						this.showAdd = false
					}
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	@import '../../common/css/public.scss';
</style>
