<template>
  <div class="app-container">
    <!-- 顶部操作区域 -->
    <div class="filter-container">
      <div class="filter-control">
        <span>供应商名称:</span>
        <el-input
          v-model="listQuery.shopName"
          placeholder="供应商名称"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span>广告状态:</span>
        <el-select v-model="listQuery.status" style="width: 120px" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="0" label="已上架" />
          <el-option :value="1" label="已下架" />
        </el-select>
      </div>
      <div class="filter-control">
        <span>点击日期:</span>
        <el-date-picker
          v-model="timesValue"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        />
      </div>
      <div class="filter-control">
        <el-button type="primary" @click="getList"> 查询 </el-button>
        <el-button type="primary" @click="handleoOerate('add')"> 添加 </el-button>
        <el-button type="primary" @click="getList"> 一键排序 </el-button>
      </div>
    </div>
    <!-- 列表 -->
    <el-table v-loading="isLoading" :data="list" border style="width: 100%" height="600">
      <el-table-column label="序号" align="center" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="pcImage" label="PC广告图" align="center" width="120">
        <template slot-scope="{ row }">
          <el-popover placement="top-start" title="" width="120" trigger="hover">
            <img :src="row.pcImage" alt="" style="width: 100px; height: 100px" />
            <img slot="reference" :src="row.pcImage" alt="" style="width: 50px; height: 50px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="appImage" label="小程序广告图" align="center" width="120">
        <template slot-scope="{ row }">
          <el-popover placement="top-start" title="" width="120" trigger="hover">
            <img :src="row.appImage" alt="" style="width: 100px; height: 100px" />
            <img slot="reference" :src="row.appImage" alt="" style="width: 50px; height: 50px" />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center" width="100">
        <template slot-scope="{ row }">
          <el-tag v-if="row.status === 0" type="success" size="small">已上架</el-tag>
          <el-tag v-if="row.status === 1" type="danger" size="small">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="jumpLink" label="跳转链接" align="center">
        <template slot-scope="{ row }">
          <a style="color: #2fa4e7" target="_blank" :href="row.jumpLink">
            {{ row.jumpLink ? row.jumpLink : '---' }}
          </a>
        </template>
      </el-table-column>
      <el-table-column prop="shopName" label="供应商名称" align="center" />
      <el-table-column label="排序" width="60" align="center">
        <template slot-scope="{ row }">
          <el-input v-model="row.sort" maxlength="4" minlength="1" @blur="handleOnInputBlur(row)" />
        </template>
      </el-table-column>
      <el-table-column label="点击量" width="80" align="center">
        <template slot-scope="{ row }">
          {{ row.pv }}
        </template>
      </el-table-column>
      <el-table-column prop="addTime" label="添加时间" align="center">
        <template slot-scope="{ row }">
          {{ row.addTime ? row.addTime : '---' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" style="margin: 2px" @click="handleoOerate('edit', row)">
            编辑
          </el-button>
          <el-button
            v-if="row.status === 1"
            type="success"
            size="mini"
            style="margin: 2px"
            @click="handleOffShelf(row)"
          >
            上架
          </el-button>
          <el-button
            v-if="row.status === 0"
            type="primary"
            size="mini"
            style="margin: 2px"
            @click="handleOffShelf(row)"
          >
            下架
          </el-button>
          <el-button type="danger" size="mini" style="margin: 2px" @click="handleDelete(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getShopAdvertisingImage"
    />
  </div>
</template>

<script>
import {
  getShopAdvertisingImage,
  renewShopAdvertisingImageStatus,
  delShopAdvertisingImage,
  renewShopAdvertisingImageSort
} from '@/api/user/customer/customer'
import pickerOptions from '@/utils/time-picker.js'
const defaultListQuery = {
  shopName: '', // 供应商名称
  status: '', // 上下架状态 0 已上架 1 已下架
  pageNum: 1,
  pageSize: 20,
  startPvCreateTime: '',
  endPvCreateTime: ''
}
export default {
  name: 'AdvertisList',
  filters: {},
  data() {
    return {
      isLoading: true,
      pickerOptions,
      time: '',
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      timesValue: []
    }
  },
  computed: {},
  watch: {
    timesValue: {
      handler() {
        this.getList()
      },
      immediate: true
    }
  },
  created() {
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取行为记录列表
    getList() {
      this.listQuery.pageNum = 1
      this.list = []
      if (this.timesValue && this.timesValue.length > 0) {
        this.listQuery.startPvCreateTime = this.timesValue[0]
        this.listQuery.endPvCreateTime = this.timesValue[1]
      } else {
        this.listQuery.startPvCreateTime = ''
        this.listQuery.endPvCreateTime = ''
      }
      this.getShopAdvertisingImage()
    },

    // 获取关键词列表
    async getShopAdvertisingImage() {
      try {
        this.isLoading = true
        const res = await getShopAdvertisingImage(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.isLoading = false
      } catch (error) {
        console.log(error)
      }
    },
    // 添加广告图
    handleoOerate(type, row) {
      if (type === 'add') {
        this.$router.push({
          path: '/user/customer/advertis-edit',
          query: { type: type }
        })
      } else {
        this.$router.push({
          path: '/user/customer/advertis-edit',
          query: { id: row.id, type: type }
        })
      }
    },
    // 操作下架
    handleOffShelf(row) {
      let confirmTxt = ''
      let status = ''
      if (row.status === 0) {
        confirmTxt = '确定下架该广告图吗？'
        status = 1
      } else {
        confirmTxt = '确定上架该广告图吗？'
        status = 0
      }
      this.$confirm(confirmTxt)
        .then((_) => {
          this.renewShopAdvertisingImageStatus({ id: row.id, status: status })
        })
        .catch(() => {
          this.getList()
        })
    },
    // 上下加供应商广告
    async renewShopAdvertisingImageStatus(id) {
      try {
        await renewShopAdvertisingImageStatus(id)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 操作删除广告图
    async handleDelete(row) {
      try {
        await this.$confirm('确定删除该广告图吗？', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.delShopAdvertisingImage(row)
      } catch (error) {
        this.$message.info('已取消删除操作')
      }
    },
    // 调用删除广告图
    async delShopAdvertisingImage(row) {
      try {
        await delShopAdvertisingImage(row.id)
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 广告图排序
    async handleOnInputBlur(row) {
      try {
        await renewShopAdvertisingImageSort({ id: row.id, sort: row.sort })
        this.getList()
      } catch (error) {
        console.log(error)
      }
    },
    // 机构跳转
    handleClubHrefLink(row) {
      return (
        process.env.VUE_APP_ADMIN_URL +
        `/a/user/jumpLink/toOldAdmin?type=1&clubId=${row.clubId}&clubName=${row.corporateName}`
      )
    }
  }
}
</script>

<style></style>
