<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form label-width="77px" class="ele-form-search">
        <el-row :gutter="15">
          <el-col :md="6" :sm="12">
            <el-form-item label="名称:">
              <el-input
                placeholder="请输入优惠券名称"
                clearable
                v-model="where.title"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
              >
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="productTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar></template>
        <template v-slot:timeBetween="{ row }">
          <div
            >{{ row.timeStart.split(' ')[0] }}~{{
              row.timeEnd.split(' ')[0]
            }}</div
          >
        </template>
        <template v-slot:showBiz="{ row }">
          <template v-if="row.userType === 1">所有门店</template>
          <template v-else>
            {{ objFirstValue(row.selectBiz) }}
            <el-link
              size="mini"
              v-if="Object.keys(row.selectBiz).length > 1"
              type="primary"
              class="ele-btn-icon"
              @click="showAllBizName(row)"
            >
              查看所有>>
            </el-link>
            <!--
            <template v-for="(item, index) in row.selectBiz">
              <el-tag style="margin: 5px" :key="index" type="warning">{{
                item
              }}</el-tag>
            </template>
            -->
          </template>
        </template>
      </ele-pro-table>
      <ele-modal
        width="800px"
        :visible.sync="showBizName"
        :append-to-body="true"
        :close-on-click-modal="true"
        custom-class="ele-dialog-form"
        title="推广范围"
      >
        <el-row style="padding-bottom: 20px">
          <template v-for="(item, index) in bizNames">
            <el-tag :key="index" type="info" style="margin-bottom: 10px">{{
              item
            }}</el-tag>
          </template>
        </el-row>
      </ele-modal>
    </el-card>
  </div>
</template>

<script>
  import { pageProductCoupon } from '@/api/car/coupon';
  import { setPageTabTitle } from '@/utils/page-tab-util';

  const ROUTE_PATH = '/car/product/coupon';

  export default {
    name: 'carProduct',
    components: {},
    data() {
      return {
        title: '',
        where: {
          title: '',
          selectedCar: {
            brandId: '',
            seriesId: '',
            modelId: ''
          }
        },
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center',
            fixed: 'left'
          },
          {
            prop: 'id',
            label: 'ID',
            minWidth: 40,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'title',
            label: '标题',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'price',
            label: '优惠券金额',
            showOverflowTooltip: true,
            minWidth: 50
          },
          {
            prop: 'timeBetween',
            label: '有效期',
            showOverflowTooltip: true,
            slot: 'timeBetween',
            minWidth: 100
          },
          {
            prop: 'typeCn',
            label: '类型',
            align: 'center',
            minWidth: 80,
            resizable: false,
            showOverflowTooltip: true
          },
          {
            prop: 'showBiz',
            label: '适用门店',
            align: 'center',
            minWidth: 200,
            resizable: false,
            // showOverflowTooltip: true,
            slot: 'showBiz'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        productId: 0,
        showBizName: false,
        bizNames: {}
      };
    },
    created() {},
    methods: {
      edit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        const productId = this.productId;
        return pageProductCoupon({
          ...where,
          ...order,
          page,
          limit,
          productId
        });
      },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({ page: 1, where: this.where });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
      },
      handleCarChange(carInfo) {
        this.where.selectedCar = carInfo;
      },
      showAllBizName(row) {
        this.showBizName = true;
        this.bizNames = row.selectBiz;
      },
      objFirstValue(myObject) {
        const keys = Object.keys(myObject);
        return keys.length > 0 ? myObject[keys[0]] : null;
      }
    },
    watch: {
      $route: {
        handler(route) {
          const { path } = route;
          if (path !== ROUTE_PATH) {
            return;
          }
          this.title = this.$route.query.title;
          this.productId = this.$route.query.id;
          setPageTabTitle(this.title + '的优惠券');
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
        },
        immediate: true
      }
    }
  };
</script>
<style scoped></style>
