<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <!--          <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
          <!--            <a-form-item label="筛选：">-->
          <!--              <a-range-picker-->
          <!--                v-model="queryParamTime.createTimeRange"-->
          <!--                format="YYYY-MM-DD"-->
          <!--                :placeholder="['开始时间', '结束时间']"-->
          <!--                @change="onDateChange"-->
          <!--                @ok="onDateOk"-->
          <!--              />-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :md='6' :sm='8'>
            <a-form-item label='主题'>
              <j-input v-model='queryParam.subject' placeholder='请输入主题'></j-input>
            </a-form-item>
          </a-col>
          <a-col :lg='7' :md='8' :sm='24' :xl='6'>
            <a-form-item label='状态'>
              <j-dict-select-tag v-model='queryParam.status' dictCode='	send_status' placeholder='请输入状态' />
            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <a-col :lg='11' :md='12' :sm='24' :xl='10'>
              <a-form-item label='发布时间'>
                <j-date v-model='queryParam.pubDate_begin' class='query-group-cust' placeholder='请选择开始日期'></j-date>
                <span class='query-group-split-cust'></span>
                <j-date v-model='queryParam.pubDate_end' class='query-group-cust' placeholder='请选择结束日期'></j-date>
              </a-form-item>
            </a-col>
          </template>
          <!--          <a-col :md="6" :sm="8">-->
          <!--            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">-->
          <!--              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>-->
          <!--              &lt;!&ndash;              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>&ndash;&gt;-->
          <!--            </span>-->
          <!--          </a-col>-->
          <a-col :lg='7' :md='8' :sm='24' :xl='6'>
            <span class='table-page-search-submitButtons' style='float: left;overflow: hidden;'>
              <a-button icon='search' type='primary' @click='searchQuery'>查询</a-button>
              <!--              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
              <!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
              <!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
              <!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
              <!--              </a>-->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button icon='plus' type='primary' @click='handleAdd'>新增</a-button>
      <!--      <a-button type="primary" icon="download" @click="handleExportXls('宣传报道')">导出</a-button>-->
      <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
      <!--        <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        :columns='columns'
        :dataSource='dataSource'
        :loading='loading'
        :pagination='ipagination'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        :scroll='{x:true}'
        bordered
        class='j-table-force-nowrap'
        rowKey='id'
        size='middle'
        @change='handleTableChange'>

        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' alt='' height='25px'
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            icon='download'
            size='small'
            type='primary'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>

        <span slot='action' slot-scope='text, record'>
           <a @click="handleDetail(record)">详情</a>
            <a-divider type="vertical"/>
            <a @click="handleEdit(record)" v-if="record.status!=1">编辑</a>
            <a-divider type="vertical" v-if="record.status!=1"/>
           <a @click="isPublish(record)" v-if="record.status!=1">发布</a>
           <a @click="isNotPublish(record)" v-if="record.status==1">撤销</a>
            <a-divider type="vertical"/>
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
        </span>

      </a-table>
    </div>

    <mr-publicity-modal ref='modalForm' @ok='modalFormOk' />

    <j-modal
      :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
      :title='modelDetail.title'
      :visible.sync='modelDetail.visible'
      :width='800'
      switchFullscreen
    >
      <template>
        <a-row>
          <p class='news_title'>{{ info.subject }}</p>
          <p v-if='undefined!=info.source && null!=info.source ' class='news_title'>来源：{{ info.source }}</p>
          <p class='sub_info'>
            <span>{{ info.createBy }}</span>
            <span v-if='null!=info.pubDate'>{{ info.pubDate }}</span>
            <span>{{ info.views }}次浏览</span>
          </p>
          <a-divider />
          <div class='scroll_area'>
<!--            <div v-if='null!=info.imageUrl' class='cover'>-->
<!--              <img :src='info.imageUrl' />-->
<!--            </div>-->
            <div v-html='info.content'></div>
          </div>
        </a-row>
      </template>
    </j-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { formatDate } from '@/utils/util'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import MrPublicityModal from '../modules/mrPublicity/MrPublicityModal'
import { putAction } from '@/api/manage'

export default {
  name: 'MrPublicityList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    MrPublicityModal
  },
  data() {
    var that = this
    let ellipsis = (v, l = 40) => (<j-ellipsis value={that.textReplaceAll(v)} length={l} />)
    return {
      model: {},
      queryParamTime: [],
      description: '宣传报道管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '主题',
          align: 'center',
          dataIndex: 'subject'
        },
        {
          title: '浏览次数',
          align: 'center',
          dataIndex: 'views'
        },
          {
              title: '封面',
              align: 'center',
              dataIndex: 'imageUrl',
              scopedSlots: {customRender: 'imgSlot'}
          },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status_dictText'
        },
        {
          title: '发布时间',
          align: 'center',
          dataIndex: 'pubDate',
          customRender: function(text) {
            console.log(text)
            if (text) {
              return formatDate(new Date(text).getTime(), 'yyyy-MM-dd')
            } else {
              return ''
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/MrPublicity/mrPublicity/list',
        delete: '/MrPublicity/mrPublicity/delete',
        deleteBatch: '/MrPublicity/mrPublicity/deleteBatch',
        exportXlsUrl: '/MrPublicity/mrPublicity/exportXls',
        importExcelUrl: 'MrPublicity/mrPublicity/importExcel',
        ispublish: '/MrPublicity/mrPublicity/ispublish',
        isNotPublish: '/MrPublicity/mrPublicity/isNotPublish'

      },
      dictOptions: {},
      superFieldList: [],
      modelDetail: {
        title: '详情',
        visible: false
      },
      info: {},
      disableSubmit: true
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleDetail: function(record) {
      console.log(record)
      this.modelDetail.visible = true
      this.info = record
    },
    initDictConfig() {
    },
    textReplaceAll: function(text) {
      if (null != text && '' != text && undefined != text) {
        var reg = /<[^<>]+>/g
        var afterText = text.replace(reg, '').replace(/&nbsp;/ig, '').replace(/&ldquo;/ig, '').replace(/&rdquo;/ig, '').replace(/&mdash;/ig, '')
        return afterText
      } else {
        return text
      }
    },
    isPublish: function(record) {
        var that = this
        //宣传报道封面 发布时有时没有
        that.model={}
      let formData = Object.assign(that.model, record)
      putAction(that.url.ispublish, formData).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    isNotPublish: function(record) {
      var that = this
      let formData = Object.assign(this.model, record)
      putAction(that.url.isNotPublish, formData).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'subject', text: '主题', dictCode: '' })
      fieldList.push({ type: 'Text', value: 'content', text: '内容', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '状态(0未发布，1已发布，2撤销)', dictCode: '' })
      fieldList.push({ type: 'string', value: 'pubDate', text: '发布时间', dictCode: '' })
      this.superFieldList = fieldList
    },
    formatRichText: function(html) {
      // if(html.indexOf(/<img[^>]*>/gi != -1)){
      let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
        match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '')
        match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '')
        match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '')
        return match
      })

      newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
        match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;')
        return match
      })
      newContent = newContent.replace(/<br[^>]*\/>/gi, '')
      newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"')
      return newContent
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.news_title {
  margin: 0 auto 20px auto;
  width: 80%;
  font-size: 18px;
  text-align: center;
  line-height: 2;
  font-weight: bold;
}

.sub_info {
  margin: 0;
  display: flex;
  justify-content: space-between;
  color: #9e9e9e;
}

.cover {
  margin: 0 auto 20px auto;
  width: 60%;
  height: 260px;
}

.cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scroll_area {
  max-height: 500px;
  padding-right: 10px;
  overflow-y: auto;
}
</style>