<template>
  <div>
    <a-card size="small" :bordered="false" class="promotion-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="promotion-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="创建时间">
                <rangeDate id="promotion-createDate" ref="rangeCreateDate" :value="createDate" @change="dateCreateChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="促销名称">
                <a-input id="promotion-promotionName" v-model.trim="queryParam.name" allowClear placeholder="请输入促销名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="促销状态">
                <v-select
                  v-model="queryParam.publishState"
                  ref="publishState"
                  id="promotion-publishState"
                  code="PROMO_STATE"
                  placeholder="请选择促销状态"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" :disabled="disabled" id="promotion-refresh" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" :disabled="disabled" id="promotion-reset" @click="resetSearchForm()">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <!-- 列表 -->
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator" v-if="$hasPermissions('B_promotionManagementAdd')">
          <a-button type="primary" id="promotion-add-btn" @click="openAddModal = true">新增</a-button>
        </div>
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 促销状态 -->
          <template slot="stateVal" slot-scope="text, record">
            <span>{{ record.publishState==='UNPUBLISH'?'未发布':record.publishState==='PUBLISH'?'已发布':record.publishState==='CLOSE'?'已关闭':'已结束' }}</span>
          </template>
          <!-- 促销时间 -->
          <template slot="promotionTime" slot-scope="text, record">
            <span>{{ record.activeDateStart }}至{{ record.activeDateEnd }}</span>
          </template>
          <!-- 参与客户 -->
          <template slot="joinCustomers" slot-scope="text, record">
            <span @click="handleCustomers(record)" :id="'promotion-seeDealerInfo-'+record.id" v-if="record.dealerSnList&&record.dealerSnList.length>0">共有<span class="link-bule">{{ record.dealerSnList.length }}</span>个客户</span>
            <span v-else>共有0个客户</span>
          </template>
          <!-- 促销描述 -->
          <template slot="salesDesc" slot-scope="text, record">
            <a-tooltip placement="rightBottom" v-if="record.description&&record.description.length>14">
              <template slot="title">
                <span>{{ record.description }}</span>
              </template>
              <div @click="promotionDesc(record)" :id="'promotion-description-'+record.id" class="link-bule">{{ record.description }}</div>
            </a-tooltip>
            <div v-else-if="!record.description" class="desc">--</div>
            <div v-else @click="promotionDesc(record)" :id="'promotion-description-'+record.id" class="link-bule">{{ record.description }}</div>
          </template>
          <!-- 促销展示 -->
          <template slot="salesShow" slot-scope="text, record">
            <div @click="handleSaleShow(record)" :id="'promotion-seeImgInfo-'+record.id" v-if="record.contentType!='LINK'&&record.imageSet && record.imageSet.length>0">
              <img :src="record.imageSet[0]" alt="图片走丢啦" width="60" />
            </div>
            <div v-else-if="record.contentType=='LINK'&&record.imageSet && record.imageSet.length>0">
              <img :src="record.imageSet[0]" alt="图片走丢啦" width="60" />
            </div>
            <span v-else>--</span>
          </template>
          <!-- 操作 -->
          <!-- state状态 END已结束    CLOSE已关闭    PUBLISH  已发布    UNPUBLISH  未发布 -->
          <template slot="action" slot-scope="text, record">
            <div>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                :id="'promotion-edit-btn-'+record.id"
                @click="promotionEdit(record)"
                v-if="(record.publishState=='UNPUBLISH' || record.publishState=='CLOSE') && $hasPermissions('B_promotionManagementEdit')">编辑</a-button>
              <a-button
                size="small"
                type="link"
                class="button-warning"
                :id="'promotion-modify-btn-'+record.id"
                @click="handleRelease(record,'edit')"
                v-if="((record.publishState=='PUBLISH'&&record.content!='/pagesB/promoDetail')||(record.publishState=='PUBLISH'&&record.content==='/pagesB/promoDetail'&&record.dealerEditFlag==='0') || record.publishState=='CLOSE')&&$hasPermissions('B_promotionManagementContent')">发布修改</a-button>
              <a-button
                size="small"
                type="link"
                class="button-success"
                @click="handleSee(record)"
                v-if="(record.publishState=='PUBLISH' || record.publishState=='END')&&$hasPermissions('B_promotionManagementDetail')"
                :id="'promotion-seeDetail-btn-'+record.id">查看</a-button>
              <a-button
                size="small"
                type="link"
                class="button-info"
                @click="handleRelease(record,'add')"
                v-if="record.publishState=='UNPUBLISH'&&$hasPermissions('B_promotionManagementContent')"
                :id="'promotion-release-btn-'+record.id">发布</a-button>
              <a-button
                size="small"
                type="link"
                class="button-error"
                v-if="record.publishState=='UNPUBLISH'&&$hasPermissions('B_promotionManagementDel')"
                @click="handleDel(record)"
                :id="'promotion-del-btn-'+record.id">删除</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
      <!-- 参与客户 -->
      <lookUp-customers-modal ref="lookUpCustomers" :openModal="openCustomerModal" @close="openCustomerModal = false"></lookUp-customers-modal>
      <!-- 促销描述 -->
      <promotion-desc-modal :openModal="openDescModal" :con="descCon" @close="openDescModal = false;descCon=''"></promotion-desc-modal>
      <!-- 促销展示 -->
      <promotion-show-modal ref="showModal" :openModal="openShowModal" @close="openShowModal = false"></promotion-show-modal>
      <!-- 新增 -->
      <addModal :openModal="openAddModal" :itemId="itemId" @close="closeAddModal" @ok="$refs.table.refresh()"></addModal>
      <!-- 查看 -->
      <detail-Modal :openModal="openDetailModal" :itemSn="itemId" @close="closeDetailModal" @ok="$refs.table.refresh()" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import rangeDate from '@/views/common/rangeDate.vue'
import lookUpCustomersModal from './lookUpCustomersModal'
import promotionDescModal from './promotionDescModal'
import promotionShowModal from './promotionShowModal'
import addModal from './addModal'
import detailModal from './detailModal'
// 接口
import { promoActiveList, promoActiveDel } from '@/api/promoActive'
export default {
  name: 'PromotionList',
  mixins: [commonMixin],
  components: { STable, VSelect, rangeDate, lookUpCustomersModal, promotionShowModal, promotionDescModal, addModal, detailModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      openCustomerModal: false, // 打开参与客户弹窗
      openDescModal: false, // 打开促销描述弹窗
      openShowModal: false, // 打开促销展示弹窗
      openAddModal: false, // 打开新增弹窗
      openDetailModal: false, // 打开详情弹窗
      createDate: [], //  创建时间
      itemId: '', // 当前活动sn
      descCon: '', // 描述内容
      pageType: '', // 发布类型  add  促销发布  edit 发布修改
      dealerSn: [], // 经销商sn
      // 查询参数
      queryParam: {
        beginDate: undefined, // 促销开始时间
        endDate: undefined, // 促销结束时间
        name: '', // 促销名称
        publishState: undefined// 是否发布
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        const params = Object.assign(parameter, this.queryParam)
        return promoActiveList(params).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
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      columns: [// 表头
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '促销名称', dataIndex: 'name', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '标题', dataIndex: 'title', width: '12%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '促销时间', scopedSlots: { customRender: 'promotionTime' }, width: '8%', align: 'center' },
        { title: '参与客户', scopedSlots: { customRender: 'joinCustomers' }, width: '12%', align: 'center' },
        { title: '促销描述', scopedSlots: { customRender: 'salesDesc' }, width: '12%', align: 'center' },
        { title: '促销展示', scopedSlots: { customRender: 'salesShow' }, width: '6%', align: 'center' },
        // { slots: { title: 'arrowFalgTitle' }, scopedSlots: { customRender: 'arrowFalg' }, width: '6%', align: 'center' },
        { title: '促销状态', scopedSlots: { customRender: 'stateVal' }, width: '6%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
    }
  },
  methods: {
    // 关闭添加促销活动弹窗
    closeAddModal () {
      this.openAddModal = false; this.itemId = ''
    },
    // 参与经销商
    handleCustomers (row) {
      this.openCustomerModal = true
      this.$nextTick(() => {
        this.$refs.lookUpCustomers.pageInit({ dealerSnList: row.dealerSnList ? row.dealerSnList : undefined, promoActiveSn: row.promoActiveSn })
      })
    },
    //  创建时间  change
    dateCreateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 促销展示
    handleSaleShow (record) {
      this.openShowModal = true
      this.$nextTick(() => {
        this.$refs.showModal.pageInit({ type: record.contentType, con: record.content })
      })
    },
    // 促销编辑
    promotionEdit (item) {
      this.itemId = item.promoActiveSn
      this.$nextTick(() => {
        this.openAddModal = true
      })
    },
    // 促销描述
    promotionDesc (row) {
      this.descCon = row.description
      this.$nextTick(() => {
        this.openDescModal = true
      })
    },
    // 删除促销活动
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '点击确定，该内容将会被删除，不可再恢复!',
        centered: true,
        onOk () {
          _this.spinning = true
          promoActiveDel({ 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
            }
          })
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.$refs.rangeCreateDate.resetDate(this.createDate)
      this.queryParam.beginDate = undefined
      this.queryParam.endDate = undefined
      this.queryParam.name = ''
      this.queryParam.publishState = undefined
      this.$refs.table.refresh(true)
    },
    // 促销发布
    handleRelease (row, type) {
      const newType = row.imageSet && row.imageSet.length > 0 ? 'edit' : type
      this.$router.push({ name: 'promotionManagementAdd', params: { sn: row.promoActiveSn, pageType: newType } })
    },
    // 打开详情
    handleSee (row) {
      this.openDetailModal = true
      this.itemId = row.promoActiveSn
    },
    // 关闭详情
    closeDetailModal () {
      this.openDetailModal = false
      this.itemId = ''
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  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" scoped>
  .font1{
   color:#39f;
  }
  .desc{
    width: 100%;
    padding:0 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    box-sizing: border-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
  }
</style>
