<template>
	<view
		class="v-cell"
		:class="[customClass]"
		:style="[$u.addStyle(customStyle)]"
		:hover-class="
			!disabled && (clickable || isLink) ? 'v-cell--clickable' : ''
		"
		:hover-stay-time="250"
		@tap="clickHandler"
	>
		<view
			class="v-cell__body"
			:class="[
				center && 'v-cell--center',
				size === 'large' && 'v-cell__body--large',
			]"
		>
			<view class="v-cell__body__content">
				<view class="v-cell__left-icon-wrap" v-if="$slots.icon || icon">
					<slot name="icon" v-if="$slots.icon"> </slot>
					<v-icon
						v-else
						:name="icon"
						:custom-style="iconStyle"
						:size="size === 'large' ? 22 : 18"
					></v-icon>
				</view>
				<view class="v-cell__title">
					<slot name="title" v-if="$slots.title || !title"> </slot>
					<text
						v-else
						class="v-cell__title-text"
						:style="[titleTextStyle]"
						:class="[
							disabled && 'v-cell--disabled',
							size === 'large' && 'v-cell__title-text--large',
						]"
						>{{ title }}</text
					>
					<slot name="label">
						<text
							class="v-cell__label"
							v-if="label"
							:class="[
								disabled && 'v-cell--disabled',
								size === 'large' && 'v-cell__label--large',
							]"
							>{{ label }}</text
						>
					</slot>
				</view>
			</view>
			<slot name="value">
				<text
					class="v-cell__value"
					:class="[
						disabled && 'v-cell--disabled',
						size === 'large' && 'v-cell__value--large',
					]"
					v-if="!$u.test.empty(value)"
					>{{ value }}</text
				>
			</slot>
			<view
				class="v-cell__right-icon-wrap"
				v-if="$slots['right-icon'] || isLink"
				:class="[`v-cell__right-icon-wrap--${arrowDirection}`]"
			>
				<slot name="right-icon">
					<v-icon
						v-if="rightIcon"
						:name="rightIcon"
						:custom-style="rightIconStyle"
						:color="disabled ? '#c8c9cc' : 'info'"
						:size="size === 'large' ? 18 : 16"
					></v-icon>
				</slot>
			</view>
		</view>
		<v-line v-if="border"></v-line>
	</view>
</template>

<script>
import props from "./props.js";
import mpMixin from "../../libs/mixin/mpMixin.js";
import mixin from "../../libs/mixin/mixin.js";
/**
 * cell  单元格
 * @description cell单元格一般用于一组列表的情况，比如个人中心页，设置页等。
 * @property {String | Number}	title			标题
 * @property {String | Number}	label			标题下方的描述信息
 * @property {String | Number}	value			右侧的内容
 * @property {String}			icon			左侧图标名称，或者图片链接(本地文件建议使用绝对地址)
 * @property {Boolean}			disabled		是否禁用cell
 * @property {Boolean}			border			是否显示下边框 (默认 true )
 * @property {Boolean}			center			内容是否垂直居中(主要是针对右侧的value部分) (默认 false )
 * @property {String}			url				点击后跳转的URL地址
 * @property {String}			linkType		链接跳转的方式，内部使用的是vcu-uni-view封装的route方法，可能会进行拦截操作 (默认 'navigateTo' )
 * @property {Boolean}			clickable		是否开启点击反馈(表现为点击时加上灰色背景) （默认 false ）
 * @property {Boolean}			isLink			是否展示右侧箭头并开启点击反馈 （默认 false ）
 * @property {Boolean}			required		是否显示表单状态下的必填星号(此组件可能会内嵌入input组件) （默认 false ）
 * @property {String}			rightIcon		右侧的图标箭头 （默认 'arrow-right'）
 * @property {String}			arrowDirection	右侧箭头的方向，可选值为：left，up，down
 * @property {Object | String}			rightIconStyle	右侧箭头图标的样式
 * @property {Object | String}			titleStyle		标题的样式
 * @property {Object | String}			iconStyle		左侧图标样式
 * @property {String}			size			单位元的大小，可选值为 large，normal，mini
 * @property {Boolean}			stop			点击cell是否阻止事件传播 (默认 true )
 * @property {Object}			customStyle		定义需要用到的外部样式
 *
 * @event {Function}			click			点击cell列表时触发
 * @example 该组件需要搭配cell-group组件使用，见官方文档示例
 */
export default {
	name: "v-cell",
	data() {
		return {};
	},
	mixins: [mpMixin, mixin, props],
	computed: {
		titleTextStyle() {
			return uni.$u.addStyle(this.titleStyle);
		},
	},
	emits: ["click"],
	methods: {
		// 点击cell
		clickHandler(e) {
			if (this.disabled) return;
			this.$emit("click", {
				name: this.name,
			});
			// 如果配置了url(此props参数通过mixin引入)参数，跳转页面
			this.openPage();
			// 是否阻止事件传播
			this.stop && this.preventEvent(e);
		},
	},
};
</script>

<style lang="scss" scoped>
@import "../../libs/css/components.scss";

$v-cell-padding: 10px 15px !default;
$v-cell-font-size: 15px !default;
$v-cell-line-height: 24px !default;
$v-cell-color: $v-main-color !default;
$v-cell-icon-size: 16px !default;
$v-cell-title-font-size: 15px !default;
$v-cell-title-line-height: 22px !default;
$v-cell-title-color: $v-main-color !default;
$v-cell-label-font-size: 12px !default;
$v-cell-label-color: $v-tips-color !default;
$v-cell-label-line-height: 18px !default;
$v-cell-value-font-size: 14px !default;
$v-cell-value-color: $v-content-color !default;
$v-cell-clickable-color: $v-bg-color !default;
$v-cell-disabled-color: #c8c9cc !default;
$v-cell-padding-top-large: 13px !default;
$v-cell-padding-bottom-large: 13px !default;
$v-cell-value-font-size-large: 15px !default;
$v-cell-label-font-size-large: 14px !default;
$v-cell-title-font-size-large: 16px !default;
$v-cell-left-icon-wrap-margin-right: 4px !default;
$v-cell-right-icon-wrap-margin-left: 4px !default;
$v-cell-title-flex: 1 !default;
$v-cell-label-margin-top: 5px !default;

.v-cell {
	&__body {
		@include flex();
		/* #ifndef APP-NVUE */
		box-sizing: border-box;
		/* #endif */
		padding: $v-cell-padding;
		font-size: $v-cell-font-size;
		color: $v-cell-color;
		// line-height: $v-cell-line-height;
		align-items: center;

		&__content {
			@include flex(row);
			align-items: center;
			flex: 1;
		}

		&--large {
			padding-top: $v-cell-padding-top-large;
			padding-bottom: $v-cell-padding-bottom-large;
		}
	}

	&__left-icon-wrap,
	&__right-icon-wrap {
		@include flex();
		align-items: center;
		// height: $v-cell-line-height;
		font-size: $v-cell-icon-size;
	}

	&__left-icon-wrap {
		margin-right: $v-cell-left-icon-wrap-margin-right;
	}

	&__right-icon-wrap {
		margin-left: $v-cell-right-icon-wrap-margin-left;
		transition: transform 0.3s;

		&--up {
			transform: rotate(-90deg);
		}

		&--down {
			transform: rotate(90deg);
		}
	}

	&__title {
		flex: $v-cell-title-flex;

		&-text {
			font-size: $v-cell-title-font-size;
			line-height: $v-cell-title-line-height;
			color: $v-cell-title-color;

			&--large {
				font-size: $v-cell-title-font-size-large;
			}
		}
	}

	&__label {
		margin-top: $v-cell-label-margin-top;
		font-size: $v-cell-label-font-size;
		color: $v-cell-label-color;
		line-height: $v-cell-label-line-height;

		&--large {
			font-size: $v-cell-label-font-size-large;
		}
	}

	&__value {
		text-align: right;
		margin-left: auto;
		font-size: $v-cell-value-font-size;
		line-height: $v-cell-line-height;
		color: $v-cell-value-color;
		&--large {
			font-size: $v-cell-value-font-size-large;
		}
	}

	&--clickable {
		background-color: $v-cell-clickable-color;
	}

	&--disabled {
		color: $v-cell-disabled-color;
		/* #ifndef APP-NVUE */
		cursor: not-allowed;
		/* #endif */
	}

	&--center {
		align-items: center;
	}
}
</style>
