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

<script>
import props from './props.js'
/**
 * cell  单元格
 * @description cell单元格一般用于一组列表的情况，比如个人中心页，设置页等。
 * @tutorial https://uviewui.com/components/cell.html
 * @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		链接跳转的方式，内部使用的是uView封装的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: 's-cell',
  data() {
    return {}
  },
  mixins: [uni.$s.mpMixin, uni.$s.mixin, props],
  computed: {
    titleTextStyle() {
      return uni.$s.addStyle(this.titleStyle)
    },
  },
  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';

$s-cell-padding: 32rpx 0px !default;
$s-cell-font-size: 15px !default;
$s-cell-line-height: 24px !default;
$s-cell-color: $s-main-color !default;
$s-cell-icon-size: 16px !default;
$s-cell-title-font-size: 15px !default;
$s-cell-title-line-height: 22px !default;
$s-cell-title-color: $s-main-color !default;
$s-cell-label-font-size: 12px !default;
$s-cell-label-color: $s-tips-color !default;
$s-cell-label-line-height: 18px !default;
$s-cell-value-font-size: 14px !default;
$s-cell-value-color: $s-content-color !default;
// $s-cell-clickable-color: $s-bg-color !default;
// 点击单元格背景颜色
$s-cell-clickable-color: #efeff1 !default;

$s-cell-disabled-color: #c8c9cc !default;
$s-cell-padding-top-large: 13px !default;
$s-cell-padding-bottom-large: 13px !default;
$s-cell-value-font-size-large: 15px !default;
$s-cell-label-font-size-large: 14px !default;
$s-cell-title-font-size-large: 16px !default;
$s-cell-left-icon-wrap-margin-right: 4px !default;
$s-cell-right-icon-wrap-margin-left: 4px !default;
$s-cell-title-flex: 1 !default;
$s-cell-label-margin-top: 5px !default;

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

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

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

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

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

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

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

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

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

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

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

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

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

  &__value {
    text-align: right;
    font-size: $s-cell-value-font-size;
    line-height: $s-cell-line-height;
    color: $s-cell-value-color;

    &--large {
      font-size: $s-cell-value-font-size-large;
    }
  }

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

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

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