<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="32">
              <a-form-item label="标题">
                <a-input v-model="queryParam.articleContentTitle" allow-clear placeholder="请输入标题搜索" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="32">
              <a-form-item label="类型">
                <a-select v-model="queryParam.systemDictType" placeholder="请选择类型" allow-clear>
                  <a-select-option
                    v-for="(item,index) in typeDictTypeDropDown"
                    :key="index"
                    :value="item.code"
                  >{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="32">
              <a-button type="primary" @click="$refs.table.refresh(true)">搜索</a-button>
              <a-button type="primary" style="margin-left: 8px" icon="plus" @click="$refs.detailsOplog.add()">新增</a-button>
            </a-col>
          </a-row>
        </a-form>
        <!-- 表格 -->
        <s-table
          ref="table"
          size="default"
          :pagination="false"
          :loading="loading"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.articleContentId"
        >
          <span slot="articleContentTitle" slot-scope="text">
            <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="articleContentCover" slot-scope="src">
            <img :src="src | getImgUrl" width="60" height="45" @click="showPreviewImg(src)" style="cursor: pointer;"/>
          </span>
          <span slot="articleContentAbstract" slot-scope="text">
            <ellipsis :length="80" tooltip>{{ text }}</ellipsis>
          </span>
          <span slot="articleContentType" slot-scope="text">
            {{ typeFilter(text) }}
          </span>
          <span slot="action" slot-scope="text, record">
            <a-popconfirm placement="top" :title="record.articleContentIsLine===1? '是否确认下线该条目？':'是否确认上线该条目？'" @confirm="() => editjobStatusStatus(record.articleContentIsLine,record)">
              <a>{{ statusFilter(record.articleContentIsLine) }}</a>
            </a-popconfirm>
            <a-divider type="vertical"/>
            <a v-if="hasPerm('articleContent:edit')" @click="$refs.detailsOplog.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('articleContent:edit') && hasPerm('articleContent:delete')"/>
            <a-popconfirm v-if="hasPerm('articleContent:delete')" placement="topRight" title="是否确认删除该条目？" @confirm="() => articleContentDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </s-table>
      </div>
      <details-oplog ref="detailsOplog" @ok="handleOk"/>
      <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel" title="预览">
        <img alt="example" style="width: 100%" :src="previewImage" />
      </a-modal>
    </a-card>
  </div>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import homeApi from '@/api/homeApi'
import { setImgUrl } from '@/utils/img'
import {
  findArticleContentById,
  updateArticleContent,
  deleteArticleContent
} from '@/api/modular/system/contentControlApi'
import detailsOplog from './details'

export default {
  components: {
    STable,
    detailsOplog,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: { id: '', systemDictType: '', articleContentTitle: '' },
      typeDictTypeDropDown: homeApi.systemTypeList,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'dataIndex',
          customRender: (text, row, index) => {
            return index + 1
          },
          align: 'center',
          width: '90px'
        },
        {
          title: '标题',
          dataIndex: 'articleContentTitle',
          scopedSlots: { customRender: 'articleContentTitle' }
        },
        {
          title: '封面',
          dataIndex: 'articleContentCover',
          scopedSlots: { customRender: 'articleContentCover' },
          align: 'center',
          width: '150px'
        },
        {
          title: '摘要',
          dataIndex: 'articleContentAbstract',
          scopedSlots: { customRender: 'articleContentAbstract' }
        },
        {
          title: '内容类型',
          dataIndex: 'articleContentType',
          scopedSlots: { customRender: 'articleContentType' },
          width: '120px'
        },
        {
          title: '分类',
          dataIndex: 'articleContentClassify'
        },
        {
          title: '子分类',
          dataIndex: 'articleContentSubclassify'
        },
        {
          title: '排序',
          dataIndex: 'articleContentSort',
          align: 'center',
          width: '60px'
        }
      ],
      loading: true,
      loadData: parameter => {
        return findArticleContentById(Object.assign(parameter, this.queryParam)).then((res) => {
          if (this.hasPerm('ArticleContent:list')) {
            return res.data
          }
        })
      },
      selectedRows: [],
      statusDictTypeDropDown: [{ code: 0, name: '下线' }, { code: 1, name: '上线' }],
      previewVisible: false,
      previewImage: ''
    }
  },
  created () {
    if (this.hasPerm('articleContent:edit') || this.hasPerm('articleContent:delete') || hasPerm('articleContent:start') || hasPerm('articleContent:stop')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  filters: {
    getImgUrl (v) {
      return setImgUrl(v)
    }
  },
  methods: {
    statusFilter (status) {
      const values = this.statusDictTypeDropDown.filter(item => item.code === status)
      if (values.length > 0) {
        return values[0].name
      } else {
        return '上线'
      }
    },
    typeFilter (status) {
      const values = this.typeDictTypeDropDown.filter(item => item.code === status)
      if (values.length > 0) {
        return values[0].name
      } else {
        return ''
      }
    },
     /**
     * 启动停止
     */
    editjobStatusStatus (code, record) {
      if (code === 1) {
        updateArticleContent({ id: record.articleContentId, isLine: 0 }).then(res => {
          if (res.status === 200) {
            if (res.data) {
              this.$message.success('下线成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('下线失败')
            }
          } else {
            this.$message.error('下线失败：' + res.msg)
          }
        })
      } else {
        updateArticleContent({ id: record.articleContentId, isLine: 1 }).then(res => {
          if (res.status === 200) {
            if (res.data) {
              this.$message.success('上线成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('上线失败')
            }
          } else {
            this.$message.error('上线失败：' + res.msg)
          }
        })
      }
    },
    articleContentDelete (record) {
      deleteArticleContent({ id: record.articleContentId }).then((res) => {
        if (res.status === 200) {
          if (res.data) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败')
          }
        } else {
          this.$message.error('删除失败：' + res.msg)
        }
      }).catch((err) => {
        this.$message.error('删除错误：' + err.message)
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    showPreviewImg (src) {
      this.previewImage = setImgUrl(src)
      this.previewVisible = true
    },
    handleCancel () {
      this.previewVisible = false
    }
  }
}
</script>

<style lang="less">
.mr5 {
  margin-right: 5px;
}
</style>
