<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.title" allow-clear placeholder="请输入标题" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-select placeholder="请选择上架状态" v-model="queryParam.status" allow-clear>
                  <a-select-option
                    v-for="(item, index) in GetClientDataList('goodsstatus')"
                    :key="index"
                    :value="item.code"
                    >{{ item.val }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-select placeholder="请选择类型" v-model="queryParam.ptype" allow-clear>
                  <a-select-option
                    v-for="(item, index) in GetClientDataList('conenttypeenum')"
                    :key="index"
                    :value="item.code"
                    >{{ item.val }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 6) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table ref="table" :columns="columns" :data="loadData" :alert="true" :rowKey="record => record.id">
        <template slot="operator" v-if="hasPerm('info:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('info:add')">新增公告</a-button>
        </template>
        <span slot="status" slot-scope="text, record">
          <a-tag color="#f50" v-if="record.status === 1"> {{ getenumval('goodsstatus', text) }}</a-tag>
          <a-tag color="#1dbb1d" v-else-if="record.status === 2"> {{ getenumval('goodsstatus', text) }}</a-tag>
          <a-tag color="#b3b3b3" v-else> {{ getenumval('goodsstatus', text) }}</a-tag>
        </span>
        <span slot="ptype" slot-scope="text">
          {{ getenumval('conenttypeenum', text) }}
        </span>
        <span slot="page" slot-scope="text, record">
          <a v-if="record.ptype === 4" @click="copyTextByContentType(text, 6)">复制</a>
          <span v-else>不支持</span>
        </span>
        <div slot="content" slot-scope="text, record">
          <div v-if="record.ptype === 1">
            <div>AppId:{{ record.appid }}</div>
            <div v-if="text !== ''">
              路径:<ellipsis :length="10" tooltip>{{ text }}</ellipsis>
            </div>
          </div>
          <div v-if="record.ptype === 2 || record.ptype === 3">
            <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
          </div>
          <div v-if="record.ptype === 4">
            <div>{{ textcontent(text, 10) }}</div>
          </div>
        </div>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('notice:edit')" @click="AddorEdit(record, 1)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('notice:status') && (record.status === 1 || record.status === 3)" />
          <a-popconfirm
            v-if="hasPerm('notice:status') && (record.status === 1 || record.status === 3)"
            placement="topRight"
            title="确认上架？"
            @confirm="() => setstatus(record, 2)"
          >
            <a>上架</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('notice:status') && record.status == 2" />
          <a-popconfirm
            v-if="hasPerm('notice:status') && record.status == 2"
            placement="topRight"
            title="确认下架？"
            @confirm="() => setstatus(record, 1)"
          >
            <a>下架</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('notice:delete')" />
          <a-popconfirm
            v-if="hasPerm('notice:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => Delete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <editForm ref="editForm" :title="title" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XCard, Ellipsis } from '@/components'
import { List, SetStatus, Delete } from '@/api/modular/main/notice'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import editForm from './editForm'
import { copyTextByContentType, textcontent } from '@/utils/util'
export default {
  components: {
    XCard,
    STable,
    editForm,
    Ellipsis
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '',
      // 表头
      columns: [
        {
          title: '排序',
          dataIndex: 'sortcode'
        },
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '类型',
          dataIndex: 'ptype',
          scopedSlots: {
            customRender: 'ptype'
          }
        },
        {
          title: '浏览量',
          dataIndex: 'browsecnt'
        },
        {
          title: '点赞量',
          dataIndex: 'likecnt'
        },
        {
          title: '内容',
          dataIndex: 'content',
          scopedSlots: {
            customRender: 'content'
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {
            customRender: 'status'
          }
        },
        {
          dataIndex: 'createdtime',
          title: '创建时间'
        },
        {
          title: '内链',
          dataIndex: 'id',
          scopedSlots: {
            customRender: 'page'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return List(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('info:status') || this.hasPerm('info:edit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataList,
    copyTextByContentType,
    textcontent,
    handleOk() {
      this.$refs.table.refresh()
    },
    // 获取枚举信息
    getenumval(type, item) {
      return GetClientDataItem(type, item)
    },
    // 新增或者编辑
    AddorEdit(record, type) {
      this.title = type === 1 ? '编辑公告' : '新增公告'
      this.$refs.editForm.init(record)
    },
    // 修改状态
    setstatus(record, status) {
      SetStatus({
        id: record.id,
        status: status
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('状态设置成功')
            this.$refs.table.refresh()
          } else {
            this.$myerror(res.message)
          }
        })
        .catch(err => {
          this.$myerror(err.message)
        })
    },
    // 删除
    Delete(record) {
      Delete({
        id: record.id
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$myerror(res.message)
          }
        })
        .catch(err => {
          this.$myerror(err.message)
        })
    }
  }
}
</script>
<style scoped lang="less">
/deep/ .ant-table td {
  white-space: nowrap;
}
/deep/ .ant-table-body {
  overflow-x: auto !important;
}
/deep/ .ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  white-space: nowrap;
}
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
