<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="hasPerm('bannerIndex:page')">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称搜索"/>
            </a-form-item>
          </a-col>
          <a-col :md="16" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button type="primary" style="margin-left: 8px" v-if="hasPerm('bannerIndex:add')" icon="plus" @click="$refs.addForm.add()">新增</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.bannerId"
    >
      <span slot="bannerName" slot-scope="text">
        <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="bannerImage" slot-scope="src">
        <img :src="src | getImgUrl" width="60" height="45" @click="showPreviewImg(src)" style="cursor: pointer;"/>
      </span>
      <span slot="bannerType" slot-scope="text">
        {{ typeFilter(text) }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a-popconfirm placement="top" :title="record.bannerIsLine===1? '是否确认下线该banner？':'是否确认上线该banner？'" @confirm="() => editjobStatusStatus(record.bannerIsLine,record)">
          <a>{{ statusFilter(record.bannerIsLine) }}</a>
        </a-popconfirm>
        <a-divider type="vertical"/>
        <a v-if="hasPerm('bannerIndex:edit')" @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical" v-if="hasPerm('bannerIndex:edit') && hasPerm('bannerIndex:delete')"/>
        <a-popconfirm v-if="hasPerm('bannerIndex:delete')" placement="topRight" title="是否确认删除该banner？" @confirm="() => bannerIndexDelete(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>
    <add-form ref="addForm" @ok="handleOk" />
    <edit-form ref="editForm" @ok="handleOk" />
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel" title="预览">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </a-card>
</template>
<script>
  import { STable, Ellipsis } from '@/components'
  import homeApi from '@/api/homeApi'
  import { setImgUrl } from '@/utils/img'
  import addForm from './addBannerForm'
  import editForm from './editBannerForm'
  const _ = require('lodash')

  // import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  export default {
    name: 'BannerIndex',
    components: {
      STable,
      Ellipsis,
      addForm,
      editForm
    },
    data () {
      return {
        // 查询参数
        queryParam: { id: '' },
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: 'dataIndex',
            customRender: (text, row, index) => {
              return index + 1
            },
            align: 'center',
            width: '90px'
          },
          {
            title: '名称',
            dataIndex: 'bannerName',
            scopedSlots: { customRender: 'bannerName' },
            width: '260px'
          },
          {
            title: '图片',
            dataIndex: 'bannerImage',
            scopedSlots: { customRender: 'bannerImage' },
            align: 'center',
            width: '150px'
          },
          {
            title: '跳转方式',
            dataIndex: 'bannerType',
            scopedSlots: { customRender: 'bannerType' },
            align: 'center',
            width: '150px'
          },
          {
            title: '跳转链接',
            dataIndex: 'bannerTypeUrlShow',
            scopedSlots: { customRender: 'bannerTypeUrlShow' }
          },
          {
            title: '排序',
            dataIndex: 'bannerSort',
            align: 'center',
            width: '90px'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return homeApi.findBannerById(Object.assign(parameter, this.queryParam)).then((res) => {
            var data = res.data
            _.each(data.rows, (item, index) => {
              if (item.bannerType === 1) {
                item.bannerTypeUrlShow = JSON.parse(item.bannerTypeUrl).articleContentTitle || JSON.parse(item.bannerTypeUrl).articleContentClassify
              } else {
                item.bannerTypeUrlShow = item.bannerTypeUrl
              }
            })
            return data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        statusDictTypeDropDown: [{ code: 0, name: '下线' }, { code: 1, name: '上线' }],
        typeDictTypeDropDown: [{ code: 0, name: '无' }, { code: 1, name: '站内文章' }, { code: 2, name: '外部链接' }],
        previewVisible: false,
        previewImage: ''
      }
    },
    created () {
      if (this.hasPerm('bannerIndex:edit') || this.hasPerm('bannerIndex:delete') || hasPerm('bannerIndex:start') || hasPerm('bannerIndex:stop')) {
        this.columns.push({
          title: '操作',
          width: '300px',
          dataIndex: 'action',
          align: 'center',
          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 (type) {
        const values = this.typeDictTypeDropDown.filter(item => item.code === type)
        if (values.length > 0) {
          return values[0].name
        } else {
          return '无'
        }
      },
      /**
       * 启动停止
       */
      editjobStatusStatus (code, record) {
        if (code === 1) {
          homeApi.updateIsLinebanner({ id: record.bannerId, 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 {
          homeApi.updateIsLinebanner({ id: record.bannerId, 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)
            }
          })
        }
      },
      bannerIndexDelete (record) {
        homeApi.deleteBannerById({ id: record.bannerId }).then((res) => {
          if (res.status === 200) {
            if (res.data) {
              this.$message.success('删除成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('删除失败')
            }
          } else {
            this.$message.error('删除失败')
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      showPreviewImg (src) {
        this.previewImage = setImgUrl(src)
        this.previewVisible = true
      },
      handleCancel () {
        this.previewVisible = false
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
