<template>
<section class="dm-wrap">
    <div class="pb22 clearfix">
        <el-button class="fr" type="primary"  @click="$router.push('/coupon/info')">新增优惠券</el-button>
    </div>
  <el-table tooltipEffect="light" :data="tableList" style="width: 100%" v-loading="loading">
    <el-table-column label="商品" align="left" prop="timesStatus" min-width="140">
      <template slot-scope="scope">
        <div class="ellipsis-100" >
          <img class="vertical-middle table__avatar--gift" :src="filterAvatar(scope.row.cardType)" width="40" height="40" />
          <div class="inline-block vertical-middle">
              <p class="table-name--ellipsis">{{scope.row.proTitle || '--'}}</p>
              <p class="fz13 gray">{{scope.row.proSubTitle || '--'}}</p>
          </div>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="积分费用" align="left" prop="integralCost" width="100">
      <template slot-scope="scope">
        <updateCount :model="scope.row" theType="integralCost" typeName="积分" @refresh="getPageCardsList"></updateCount>
      </template>
    </el-table-column>
    <el-table-column label="现金费用" align="left" prop="cashCost" width="100">
      <template slot-scope="scope">
          <updateCount :model="scope.row" theType="cashCost" :precision="2" typeName="元" @refresh="getPageCardsList"></updateCount>
      </template>
    </el-table-column>
    <el-table-column label="库存" align="left" prop="virtualStock" width="100">
      <template slot-scope="scope">
          <updateCount :model="scope.row" theType="virtualStock" @refresh="getPageCardsList"></updateCount>
      </template>
    </el-table-column>
    <el-table-column label="兑换次数" align="left" prop="allExchangeNumber" width="80">
      <template slot-scope="scope">
        <span @click="$router.push('/coupon/exchange/'+scope.row.integralMallProId)" class="blue">{{scope.row.allExchangeNumber}}</span>
      </template>
    </el-table-column>
    <el-table-column label="状态" align="left" prop="status" width="140px">
      <template slot-scope="scope" >
        <span v-html="renderStatus(scope.row).html"></span>
      </template>
    </el-table-column>
    <el-table-column label="显示状态" align="center" prop="proShowStatus" width="120">
      <template slot-scope="scope" >
        {{renderShowStatus(scope.row)}}
      </template>
    </el-table-column>
    <el-table-column label="操作" align="left" width="140px">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push('/coupon/info/'+scope.row.integralMallProId)">编辑</el-button>
          <dm-delete @confirm="delData(scope.row)" tips="是否删除该优惠券?">
            <el-button type="text">删除</el-button>
          </dm-delete>
        </template>
    </el-table-column>
  </el-table>
  <el-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listParams.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="listParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></el-pagination>
</section>
</template>
<script>
import { getPageCardsList, deleteProService } from '@/service/api/mallApi.js';
import updateCount from '../common/update-count';
import {formateDateTimeByType} from '@/utils/index.js';
export default {
    name: 'coupon-list',
    components: {
      updateCount
    },
    data () {
      return {
        uuid:'',
        defaultAvatar:require('../../../assets/img/head_default.png'),
        daijinAvatar:require('../../../assets/img/credit_daijin_icon.png'),
        zhekouAvatar:require('../../../assets/img/credit_zhekou_icon.png'),
        duihuanAvatar:require('../../../assets/img/credit_duihuan_icon.png'),
        loading:false,
        tableList:[],
        listParams:{
            currentPage:1,
            pageSize:20
        },
        total:0
      }
    },
    created() {
      this.$store.commit('mutations_breadcrumb',[{name:'积分商城'},{name:'优惠券',path:''}]);
      this.getPageCardsList();
      setTimeout(_ => {
        this.uuid = 'asdaqwerasdaqwerasdaqwerasdaqwer'
      },300)
    },
    methods: {
      filterAvatar(val){
        return (val === 0 ? this.daijinAvatar : (val === 1 ? this.zhekouAvatar: this.duihuanAvatar));
      },
      renderStatus(item) {
        var _releaseType = item.releaseType ;
				//上架日期
				var _limit_time_begin = item.limitTimeBegin ;
				//库存
				var _virtual_stock = item.virtualStock ;
				//兑换日期类型1:全部 2:固定日期 3:每月 4:每周
				// 显示状态的优先级：已过期＞无库存＞定时发布＞未在兑换时间
        var _exchange_date_type = item.exchangeDateType ;
        var dhzt_show_html ='<span class="dm-status--primary">正常</span>'
				var dhzt_show =  '正常';
				//判断是否在兑换日期内 - 未在兑换时间
				if(_exchange_date_type==2) {
					if(!(new Date().getTime() >= item.exchangeFixDateBegin && new Date().getTime() <= (item.exchangeFixDateEnd + (1000*60*60*24) ))) {
            dhzt_show_html =  '<span class="dm-status--warning">未在兑换时间</span>';
				    dhzt_show =  '未在兑换时间';
					}
				}
				if(_exchange_date_type==3) {
          dhzt_show_html =  '<span class="dm-status--warning">未在兑换时间</span>';
          dhzt_show =  '未在兑换时间';
					var _exchange_date_day = item.exchangeDateDay ;
					var day = new Date().getDate() ;
					var _arr = _exchange_date_day.split(",") ;
					_arr.map(v => {
						if(day == parseInt(v)) {
              dhzt_show_html = '<span class="dm-status--primary">正常</span>' ;
				      dhzt_show =  '正常';
						}
					})
				}

				if(_exchange_date_type==4) {
					var _exchange_date_week = item.exchangeDateWeek ;
					var week = new Date().getDay() ;
					if(week == 0) {
						week =7 ;
					}
					if(_exchange_date_week.indexOf(week) == -1) {
            dhzt_show_html =  '<span class="dm-status--warning">未在兑换时间</span>';
            dhzt_show =  '未在兑换时间';
					}
				}

				if(item.exchangeTimeType == 2 && item.exchangeTimeList && dhzt_show == '正常') {
					dhzt_show_html =  '<span class="dm-status--warning">未在兑换时间</span>';
          dhzt_show =  '未在兑换时间';
					for(var k=0;k<item.exchangeTimeList.length;k++) {
						var _o = item.exchangeTimeList[k] ;
						var start = _o.exchangeTimeBeginNumber ;
						var end  = _o.exchangeTimeEndNumber ;
						var _now = Date.now() ;
						if(_now >= start && _now <= end) {
              dhzt_show_html =  '<span class="dm-status--primary">正常</span>';
              dhzt_show =  '正常';
							break ;
 						}
					}
				}

				//定时发布
				if(_releaseType == 2 && item.status==2 && new Date().getTime() < _limit_time_begin) {
          dhzt_show_html =  `<span class="dm-status--success">定时发布<br/>${formateDateTimeByType(new Date(_limit_time_begin),"yyyy-MM-dd-hh-mm-ss")}</span>`;
          dhzt_show =  '定时发布';
				}

				if(_virtual_stock<=0) {
          dhzt_show_html =  '<span class="dm-status--error">无库存</span>';
					dhzt_show = "无库存" ;
				}

				if(item.status==1) {
          dhzt_show_html = '<span class="dm-status--info">已下架</span>';
					dhzt_show = "已下架" ;
				}
        return {
          text:dhzt_show,
          html:dhzt_show_html
        };
      },
      renderShowStatus(item) {
        //商品显示状态 1:上架状态就显示 2:兑换状态下显示
        var dhzt_show =  this.renderStatus(item).text;
        var pro_show_status = item.proShowStatus ;
        var proShowStatus = "不显示" ;
        // var mall_show_status = "mall_show_no" ;
        if(item.status==2) {
          if((item.releaseType==1 || (item.releaseType == 2 && new Date().getTime() >= (item.limitTimeBegin || 0)) ) && ( pro_show_status==1 || (pro_show_status==2 &&  (dhzt_show=="正常" || dhzt_show=='无库存'))) ) {
            proShowStatus = "显示" ;
            // mall_show_status = "mall_show_ok" ;
          }
        }
        return proShowStatus;
      },
      search() {
        this.listParams.currentPage = 1;
        this.getPageCardsList();
      },
      handleSizeChange(val) {
          this.listParams.pageSize = val;
          this.getPageCardsList();
      },
      handleCurrentChange(val) {
          this.listParams.currentPage = val;
          this.getPageCardsList();
      },
      async getPageCardsList() {
        this.loading = true;
        let res = await getPageCardsList(this.listParams);
        this.tableList = [];
        let result =  res.result.rows || [];
        result.map(v => {
          v.integralCostFlag = false;
          v.cashCostFlag = false;
          v.virtualStockFlag = false;
          v.allExchangeNumberFlag = false;
          this.tableList.push(v);
        })
        this.total = res.result.total;
        this.loading = false;
      },
      // 删除
      delData(row) {
        deleteProService({proId:row.integralMallProId}).then(res => {
          this.$tips({type: 'success',message: '删除成功!'});
          this.getPageCardsList();
        }).catch(err => {
          this.$tips({type: 'error',message: '删除失败!'});
        })
      }
    }
}
</script>


