<template>
  <a-card size="small" :bordered="false" class="jg-page-wrap promotionInfoList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="5" :sm="24">
              <a-form-item label="发布时间">
                <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="促销名称">
                <a-input id="promotionInfoList-promoName" v-model.trim="queryParam.promoName" allowClear placeholder="请输入促销名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="促销类型">
                <v-select
                  v-model="queryParam.promoType"
                  ref="sourceType"
                  id="promotionInfoList-promoType"
                  code="SHOP_PROMO_PROMO_TYPE"
                  placeholder="请选择促销类型"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="24">
              <a-form-item label="促销状态">
                <v-select
                  v-model="queryParam.promoState"
                  ref="sourceType"
                  id="promotionInfoList-promoState"
                  code="SHOP_PROMO_PROMO_STATE"
                  placeholder="请选择促销状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="promotionInfoList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="promotionInfoList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 促销时间 -->
        <template slot="promotionTime" slot-scope="text, record">
          <span>{{ record.promoStartDate }}至{{ record.promoEndDate }}</span>
        </template>
        <!-- 促销展示 -->
        <template slot="salesShow" slot-scope="text, record">
          <div @click="handleDescModal(record)" v-if="record.imageUrl">
            <img :src="record.imageUrl" alt="图片走丢啦" width="60" />
          </div>
          <span v-else>--</span>
        </template>
        <!-- 操作 -->
        <!-- state  HAVE_RELEASE 已开启 IS_OVER已结束 IS_CLOSE 已关闭 NOT_RELEASE 未开启 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-primary"
            @click="handleOpenOrClose(record,'1')"
            v-if="record.dealerOpenFlag==='1'&&record.dealerOpenState=='0'&&(record.promoState=='IS_CLOSE'||record.promoState=='NOT_RELEASE')&&$hasPermissions('B_publishActiveBtn')"
            id="productOnlineInfoList-detail-btn">开启</a-button>
          <a-button
            size="small"
            type="link"
            class="button-primary"
            v-if="record.dealerOpenFlag==='1'&&record.dealerOpenState=='1'&&record.promoState=='HAVE_RELEASE'&&$hasPermissions('B_closeActiveBtn')"
            @click="handleOpenOrClose(record,'0')"
            id="productOnlineInfoList-detail-btn">关闭</a-button>
          <a-button
            size="small"
            type="link"
            class="button-primary"
            v-if="record.dealerEditFlag==='1'&&record.dealerPublishState!='END'&&$hasPermissions('B_promoActiveEditBtn')"
            @click="handleEdit(record,'edit')">编辑</a-button>
          <a-button
            size="small"
            type="link"
            class="button-success"
            v-if="$hasPermissions('B_promoActiveSeeBtn')"
            @click="handleSee(record)">查看</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 促销展示 -->
    <promotion-desc-modal :con="descInfo" :openModal="openDescModal" @close="openDescModal = false"/>
    <!-- 查看 -->
    <detail-Modal :openModal="openDetailModal" :itemSn="itemId" @close="closeDetailModal" @ok="$refs.table.refresh()" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import promotionDescModal from './promotionDescModal'
import promotionShowModal from './promotionShowModal'
import detailModal from './detailModal'
import { shopPromoList, openOrCloseActive } from '@/api/shopPromo'
import { closeActive } from '@/api/promoActive'

export default {
  name: 'UrgentOffsetList',
  components: { STable, VSelect, rangeDate, promotionDescModal, promotionShowModal, detailModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      openDescModal: false, // 促销描述弹窗
      openShowModal: false, // 促销展示弹窗
      time: [],
      queryParam: { //  查询条件
        beginDate: undefined,
        endDate: undefined,
        promoName: '', //  促销名称
        promoType: '', // 促销类型
        promoState: undefined// 促销状态
      },
      descInfo: '', // 促销描述
      itemId: null, // 促销当前行sn
      disabled: false, //  查询、重置按钮是否可操作
      openDetailModal: false,
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '发布时间', dataIndex: 'createDate', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销名称', dataIndex: 'promoName', width: '20%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '促销时间', scopedSlots: { customRender: 'promotionTime' }, width: '20%', align: 'center' },
        { title: '促销类型', dataIndex: 'promoTypeDictValue', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销展示', scopedSlots: { customRender: 'salesShow' }, width: '10%', align: 'center' },
        { title: '状态', dataIndex: 'promoStateDictValue', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shopPromoList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
              data.list[i].images = data.list[i].images ? data.list[i].images.split(',') : []
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 打开详情
    handleSee (row) {
      this.openDetailModal = true
      this.itemId = row.promoSn
    },
    // 关闭详情
    closeDetailModal () {
      this.openDetailModal = false
      this.itemId = ''
    },
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 查看促销描述
    handleDescModal (row) {
      this.descInfo = row.imageUrl
      const _this = this
      this.$nextTick(() => {
        _this.openDescModal = true
      })
    },
    // 促销展示
    handleSaleShow (row) {
      const _this = this
      _this.openShowModal = true
      this.$nextTick(() => {
        _this.$refs.promotionShow.getDetail({ id: row.promoActiveSn, showType: row.contentType })
      })
    },
    // 开启
    handleOpenOrClose (row, val) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '促销活动一旦' + (val == '1' ? '开启' : '关闭') + '，客户' + (val == '1' ? '可' : '无法') + '参与活动。确认' + (val == '1' ? '开启' : '关闭') + '吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          openOrCloseActive({ promoSn: row.promoSn, dealerOpenState: val }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 关闭
    handleClose (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '促销活动一旦，客户无法参与活动。确认关闭吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          closeActive({ sn: row.promoActiveSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 编辑
    handleEdit (row, type) {
      const _this = this
      if (row.dealerPublishState === 'PUBLISH' && type === 'edit') {
        _this.$warning({
          title: '提示',
          content: '请先关闭活动，活动关闭后可编辑活动'
        })
      } else {
        const nameInfo = type === 'edit' ? 'promotionInfoEdit' : 'promotionInfoDetail'
        _this.$router.push({ name: nameInfo, params: { sn: row.promoActiveSn, pageType: type } })
      }
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.promoName = ''
      this.queryParam.promoType = undefined
      this.queryParam.promoState = undefined
      this.$refs.table.refresh(true)
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 203
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 只刷新列表
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .promotionInfoList-wrap{
    .red{
      color: #ed1c24;
    }
    .green{
      color: #19be6b;
    }
    .font1{
     color:#39f;
    }
    .desc{
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
    }
  }
</style>
