<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.name" 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.cstatus" allow-clear>
                  <a-select-option
                    v-for="(item, index) in GetClientDataList('campaignstatusenum')"
                    :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('huodong:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('huodong:add')"
            >新增活动</a-button
          >
        </template>
        <span slot="headimg" slot-scope="text, record">
          <a-avatar
            shape="square"
            :size="64"
            @click="$refs.PreImg.Show({ alt: record.title, img: record.imageurl })"
            :alt="record.title"
            :src="record.imageurl"
            :srcSet="record.imageurl"
          />
        </span>
        <span slot="time" slot-scope="text, record">
          <span>{{ record.starttime }}</span>
          至
          <span>{{ record.endtime }}</span>
        </span>
        <span slot="page" slot-scope="text">
          <a @click="copyTextByContentType(text, 4)">复制</a>
        </span>
        <span slot="cstatus" slot-scope="text, record">
          <a-tag color="red" v-if="record.cstatus === 1"> {{ getenumval('campaignstatusenum', text) }}</a-tag>
          <a-tag color="green" v-else-if="record.cstatus === 2"> {{ getenumval('campaignstatusenum', text) }}</a-tag>
          <a-tag color="#b3b3b3" v-else> {{ getenumval('campaignstatusenum', text) }}</a-tag>
        </span>
        <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="moreinfo" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="address" slot-scope="text, record">
          <ellipsis :length="10" tooltip
            >{{ record.province }}{{ record.city }}{{ record.area }}{{ record.address }}</ellipsis
          >
        </span>
        <span slot="istop" slot-scope="text">
          <span v-if="text">是</span>
          <span v-else>否</span>
        </span>
        <div slot="content" slot-scope="text">
          <div>
            <div>{{ textcontent(text, 10) }}</div>
          </div>
        </div>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('huodong:edit')" @click="AddorEdit(record, 1)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('huodong:status') && (record.status === 1 || record.status === 3)" />
          <a-popconfirm
            v-if="hasPerm('huodong: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('huodong:status') && record.status == 2" />
          <a-popconfirm
            v-if="hasPerm('huodong:status') && record.status == 2"
            placement="topRight"
            title="确认下架？"
            @confirm="() => setstatus(record, 1)"
          >
            <a>下架</a>
          </a-popconfirm>
        </span>
      </s-table>
      <editForm ref="editForm" :title="title" @ok="handleOk" />
    </a-card>
    <previewimg ref="PreImg"></previewimg>
  </div>
</template>
<script>
import { STable, XCard, Ellipsis } from '@/components'
import { List, SetStatus } from '@/api/modular/main/huodong'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import previewimg from '@/views/components/previewimg'
import editForm from './editForm'
import { copyTextByContentType, textcontent } from '@/utils/util'
export default {
  components: {
    XCard,
    STable,
    editForm,
    previewimg,
    Ellipsis
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '',
      // 表头
      columns: [
        {
          title: '封面图',
          dataIndex: 'headimg',
          scopedSlots: {
            customRender: 'headimg'
          }
        },
        {
          title: '名称',
          dataIndex: 'name',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '是否置顶',
          dataIndex: 'istop',
          scopedSlots: {
            customRender: 'istop'
          }
        },
        {
          title: '浏览量',
          dataIndex: 'browsecnt'
        },
        {
          title: '点赞量',
          dataIndex: 'likecnt'
        },
        {
          dataIndex: 'startime',
          title: '起止时间',
          scopedSlots: {
            customRender: 'time'
          }
        },
        {
          dataIndex: 'phone',
          title: '联系电话'
        },
        {
          title: '地址',
          dataIndex: 'address',
          scopedSlots: {
            customRender: 'address'
          }
        },
        {
          title: '活动状态',
          dataIndex: 'cstatus',
          scopedSlots: {
            customRender: 'cstatus'
          }
        },
        {
          title: '内容',
          dataIndex: 'content',
          scopedSlots: {
            customRender: 'content'
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {
            customRender: 'status'
          }
        },
        {
          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('huodong:status') || this.hasPerm('huodong: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)
        })
    }
  }
}
</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>
