<template>
  <div class="mod-marketing-coupon">
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" label-width="auto" size="small">
        <div class="input-row">
          <el-form-item prop="couponName" label="优惠券名" class="search-form-item">
            <el-input type="text" v-model="searchForm.couponName" placeholder="优惠券名"></el-input>
          </el-form-item>
          <el-form-item prop="overdueStatus" label="过期状态" class="search-form-item">
            <el-select v-model="searchForm.overdueStatus" placeholder="过期状态">
              <el-option label="过期" :value="0"></el-option>
              <el-option label="未过期" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="putonStatus" label="投放状态" class="search-form-item">
            <el-select v-model="searchForm.putonStatus" placeholder="投放状态">
              <el-option label="等待投放" :value="0"></el-option>
              <el-option label="已投放" :value="1"></el-option>
              <el-option label="违规下架" :value="2"></el-option>
              <el-option label="等待审核" :value="3"></el-option>
              <el-option label="取消投放" :value="-1"></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="default-btn primary-btn" @click="searchChange">{{$t('product.search')}}</div>
        <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('product.reset')}}</div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div class="default-btn primary-btn"@click.stop="addOrUpdateHandle()">新增</div>
      </div>
      <div class="table-con prod-table">
        <el-table
          ref="prodListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%">
           <el-table-column
            align="center"
            type="index"
            :label="$t('number')"
            width="55">
          </el-table-column>
          <el-table-column
            align="center"
            prop="couponName"
            label="优惠券名"
            width="auto">
          </el-table-column>
          <el-table-column
            align="center"
            prop="couponType"
            label="优惠券类型"
            width="auto">
            <template slot-scope="scope">
              <span class="tag-text" v-if="scope.row.couponType == 1">代金券</span>
              <span class="tag-text" v-else-if="scope.row.couponType == 2">折扣券</span>
              <span class="tag-text" v-else-if="scope.row.couponType == 3">兑换券</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="scorePrice"
            label="所需积分"
            width="auto">
          </el-table-column>
          <el-table-column
            align="center"
            prop="startTime"
            label="开始时间"
            width="auto">
          </el-table-column>
          <el-table-column
            align="center"
            prop="endTime"
            label="结束时间"
            width="auto">
          </el-table-column>
          <el-table-column
            align="center"
            prop="overdueStatus"
            label="过期状态"
            width="auto">
            <template slot-scope="scope">
              <span v-if="scope.row.overdueStatus === 1"
                class="tag-text">未过期</span>
              <span v-else
                class="tag-text">过期</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="putonStatus"
            label="投放状态"
            width="auto">
            <template slot-scope="scope">
             <span v-if="scope.row.putonStatus === 1"
                class="tag-text">已投放</span>
            <span v-else-if="scope.row.putonStatus === 0"
                    class="tag-text">等待投放</span>
            <span v-else-if="scope.row.putonStatus === -1"
                    class="tag-text">取消投放</span>
            <span v-else-if="scope.row.putonStatus ===2"
                    class="tag-text">违规下线</span>
            <span v-else-if="scope.row.putonStatus ===3"
                    class="tag-text">等待审核</span>
            </template>
          </el-table-column>
           <el-table-column
            align="center"
            prop="stocks"
            label="库存"
            width="auto">
          </el-table-column>
          <el-table-column fixed="right" :label="$t('crud.menu')" width="150">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div class="default-btn text-btn"
                   @click="addOrUpdateHandle(scope.row.couponId)">修改</div>
                <div class="default-btn text-btn"
                  v-if="scope.row.putonStatus > 1"
                  @click="auditEventHandle(scope.row.couponId)">下线管理</div>
                <div class="default-btn text-btn"
                  @click="deleteHandle(scope.row.couponId)">积分下架</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>
    <!-- 下线管理弹窗-->
    <offlineEventHandle v-if="offlineEventHandleVisible"
                        selectUrl="/admin/coupon/getOfflineHandleEventByCouponId"
                        applyUrl="/admin/coupon/auditApply"
                        ref="offlineEvent"
                        @refreshDataList="refreshChange"></offlineEventHandle>
  </div>
</template>

<script>
import AddOrUpdate from './scoreCoupon-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'
export default {
  data () {
    return {
      search: {
        slot: ''
      },
      dataForm: {
        shopId: 0,
        orderNumber: '',
        status: null
      },
      dateRange: [],
      overdueOptions: [{
        value: 0,
        label: '过期'
      },
      {
        value: 1,
        label: '未过期'
      }],
      PutonOptions: [{
        value: -1,
        label: '取消投放'
      },
      {
        value: 0,
        label: '等待投放'
      },
      {
        value: 1,
        label: '已投放'
      }],
      dataList: [],
      dataListLoading: false,
      offlineEventHandleVisible: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {
        putonStatus: '',
        overdueStatus: '',
        couponName: ''
      }
    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  methods: {
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/coupon/scorePage'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              'couponName': this.dataForm.couponName,
              'shopId': this.dataForm.shopId,
              'overdueStatus': this.dataForm.overdueStatus,
              'putonStatus': this.dataForm.putonStatus,
              'startTime': this.dateRange === null ? null : this.dateRange[0], // 开始时间
              'endTime': this.dateRange === null ? null : this.dateRange[1] // 结束时间
            },
            this.searchForm
          ), false
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    orderStatus () {

    },
    // 新增 / 修改
    addOrUpdateHandle (val) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(val)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定进行[${id ? '下架' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/platform/coupon/removeScore'),
            method: 'put',
            data: this.$http.adornData(id, false)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('remindPop.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange () {
      this.getDataList(this.page)
    },
    // 刷新回调用
    refreshChange () {
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    // 清空自定义数据
    searchReset () {
      this.dateRange = []
    },
    // 下线管理
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    },
     /**
     * 重置表单
     * @param {String} formName 表单名称
     */
    resetSearchForm (formName) {
      this.$refs[formName].resetFields()
    },

    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss">
.mod-order-order {
  .tit {
    display: flex;
    height: 45px;
    align-items: center;
  }
  .tit .item {
    padding: 0 10px;
    width: 10%;
    text-align: center;
  }
  .tit .product {
    width: 25%;
  }
  .prod-tit {
    padding: 10px;
    background: #f8f8f9;
    border-left: 1px solid #dddee1;
    border-top: 1px solid #dddee1;
    border-right: 1px solid #dddee1;
  }
  .prod-tit span {
    margin-right: 15px;
  }
  .prod-cont {
    display: flex;
    border-top: 1px solid #dddee1;
    border-bottom: 1px solid #dddee1;
    border-left: 1px solid #dddee1;
    color: #495060;
  }
  .prod-cont .item {
    display: flex;
    display: -webkit-flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
    // width: 10%;
    border-right: 1px solid #dddee1;
    text-align: center;
    height: 100%;
  }
  .prod-cont .item span {
    display: block;
  }
  .prod-cont .prod-item {
    // width: 38%;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #dddee1;
  }
  .prod-name {
    width: 55%;
    text-align: left;
  }
  .prod-price {
    position: absolute;
    right: 40px;
    text-align: center;
  }
  .prod-price span {
    display: block;
    margin-bottom: 10px;
  }
  .prod-name .prod-info {
    display: block;
    color: #80848f;
    margin-top: 30px;
  }
  .prod-cont .items.name {
    display: flex;
    position: relative;
    padding: 20px;
    // height: 100px;
    border-bottom: 1px solid #dddee1;
  }
  .prod-cont .items.name:last-child {
    border-bottom: none;
  }
  .prod-image {
    margin-right: 20px;
    width: 100px;
    height: 100px;
  }
  .prod-image img {
    width: 100px;
    height: 100px;
  }
  .item span {
    display: block;
    margin-bottom: 10px;
  }
  .item .operate {
    color: #2d8cf0;
  }
  .item .totalprice {
    color: #c00;
  }
  .prod .remark {
    width: 100%;
    height: 50px;
    line-height: 50px;
    background-color: #e8f7f6;
    border-left: 1px solid #dddee1;
    border-right: 1px solid #dddee1;
    border-bottom: 1px solid #dddee1;
    margin-bottom: 20px;
  }
  .buyer-remark {
    padding: 0 20px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
</style>
