<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>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="edit()"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
          >
            删除
          </el-button>
        </template>
        <template v-slot:timeBetween="{ row }">
          <div
            >{{ row.timeStart.split(' ')[0] }}~{{
              row.timeEnd.split(' ')[0]
            }}</div
          >
        </template>
        <template v-slot:status="{ row }">
          <el-switch
            :active-value="1"
            :inactive-value="0"
            v-model="row.status"
            @change="editStatus(row)"
          />
        </template>
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="edit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit-coupon
      :data="current"
      :visible.sync="showEdit"
      :product-id="productId"
      @done="reload"
    />
  </div>
</template>

<script>
  import {
    pageProductCoupon,
    removeProductCoupon,
    updateProductCouponStatus
  } from '@/api/car/coupon';
  import editCoupon from './components/editCoupon.vue';
  import { setPageTabTitle } from '@/utils/page-tab-util';

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

  export default {
    name: 'carProduct',
    components: { editCoupon },
    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: 'status',
            label: '上架状态',
            align: 'center',
            width: 80,
            resizable: false,
            slot: 'status',
            showOverflowTooltip: true
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            slot: 'action'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        productId: 0
      };
    },
    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 });
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeProductCoupon(row.id)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中数据吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            removeProductCoupon(this.selection.map((d) => d.id))
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
      },
      handleCarChange(carInfo) {
        this.where.selectedCar = carInfo;
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        updateProductCouponStatus(row.id, row.status)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      }
    },
    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>
  .cell-content {
    display: flex;
    align-items: center;
  }

  .table-image {
    width: 80px;
    height: 80px;
    margin-right: 5px;
  }

  .table-text {
    /* 文本样式 */
    overflow: hidden;
  }
</style>
