<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" v-if="hasPerm('companyCaseIndex: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('companyCaseIndex: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.companyId"
    >
      <span slot="companyName" slot-scope="text">
        <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="companyDescribe" slot-scope="text">
        <ellipsis :length="50" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="companyDefaultImg" slot-scope="src">
        <img :src="src | getImgUrl" width="60" height="45" @click="showPreviewImg(src)" style="cursor: pointer;"/>
      </span>
      <span slot="companyExchangeImg" slot-scope="src">
        <img :src="src | getImgUrl" width="60" height="45" @click="showPreviewImg(src)" style="cursor: pointer;"/>
      </span>
      <span slot="companyAssociatedCase" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="companyJumpUrl" slot-scope="text">
        <ellipsis :length="80" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="action" slot-scope="text, record">
        <a-popconfirm placement="top" :title="record.companyIsLine===1? '是否确认下线该条目？':'是否确认上线该条目？'" @confirm="() => editjobStatusStatus(record.companyIsLine,record)">
          <a>{{ statusFilter(record.companyIsLine) }}</a>
        </a-popconfirm>
        <a-divider type="vertical"/>
        <a v-if="hasPerm('companyCaseIndex:edit')" @click="$refs.editForm.edit(record)">编辑</a>
        <a-divider type="vertical" v-if="hasPerm('companyCaseIndex:edit') && hasPerm('companyCaseIndex:delete')"/>
        <a-popconfirm v-if="hasPerm('companyCaseIndex:delete')" placement="topRight" title="是否确认删除该条目？" @confirm="() => companyCaseIndexDelete(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 './addCompanyCaseForm'
  import editForm from './editCompanyCaseForm'

  const _ = require('lodash')

  export default {
    name: 'CompanyCaseIndex',
    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: 'companyName',
            scopedSlots: { customRender: 'companyName' }
          },
          {
            title: '描述',
            dataIndex: 'companyDescribe',
            scopedSlots: { customRender: 'companyDescribe' }
          },
          {
            title: '默认图片',
            dataIndex: 'companyDefaultImg',
            scopedSlots: { customRender: 'companyDefaultImg' },
            align: 'center',
            width: '150px'
          },
          {
            title: '交互图片',
            dataIndex: 'companyExchangeImg',
            scopedSlots: { customRender: 'companyExchangeImg' },
            align: 'center',
            width: '150px'
          },
          {
            title: '关联案例',
            dataIndex: 'companyAssociatedCaseShow',
            scopedSlots: { customRender: 'companyAssociatedCaseShow' }
          },
          {
            title: '跳转链接',
            dataIndex: 'companyJumpUrl',
            scopedSlots: { customRender: 'companyJumpUrl' }
          },
          {
            title: '排序',
            dataIndex: 'companySort',
            align: 'center',
            width: '90px'
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return homeApi.findCompanyCaseById(Object.assign(parameter, this.queryParam)).then((res) => {
            var data = res.data
            _.each(data.rows, (item, index) => {
                item.companyAssociatedCaseShow = JSON.parse(item.companyAssociatedCase).articleContentTitle || JSON.parse(item.companyAssociatedCase).articleContentClassify
            })
            return data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        statusDictTypeDropDown: [{ code: 0, name: '下线' }, { code: 1, name: '上线' }],
        previewVisible: false,
        previewImage: ''
      }
    },
    created () {
      if (this.hasPerm('companyCaseIndex:edit') || this.hasPerm('companyCaseIndex:delete') || hasPerm('companyCaseIndex:start') || hasPerm('companyCaseIndex: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 '上线'
        }
      },
      /**
       * 启动停止
       */
      editjobStatusStatus (code, record) {
        if (code === 1) {
          homeApi.updateIsLineCompanyCase({ id: record.companyId, 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.message)
            }
          })
        } else {
          homeApi.updateIsLineCompanyCase({ id: record.companyId, 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.message)
            }
          })
        }
      },
      companyCaseIndexDelete (record) {
        homeApi.deleteCompanyCase({ id: record.companyId }).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.message)
          }
        }).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>
