<!--
  @description: 广告列表
  @Modifier：dijia
  @routerPath:/personalCenter/advertisementList
  @date：  2021/7/27
-->
<template>
  <div>
    <!--当前位置-面包屑-->
    <div class="crumbs_nav_main">
      <span class="pos_title">
        <i class="iconfont icon-dizhi" />当前位置：
      </span>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="'/personalCenter/advertManage'">
          广告管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span>广告列表</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 搜索表单区域 -->
    <div class="search-content clearfix">
      <section class="fl">
        <el-form ref="formitInfo" :model="initDataParams" :inline="true">
          <el-form-item label="图片名称" prop="pictureName">
            <el-input v-model.trim="initDataParams.pictureName" placeholder="" />
          </el-form-item>
          <el-form-item label="是否使用" prop="useStatus">
            <el-select v-model="initDataParams.useStatus" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </section>
      <div class="fr">
        <el-button type="purple" @click.native="onSearch">
          搜索
        </el-button>
        <el-button type="gray" @click.native="resetList">
          重置
        </el-button>
      </div>
    </div>
    <!-- 按钮区域 -->
    <div class="shopmall-page-add">
      <el-button type="purple" @click="addInstitutions">
        新增广告
      </el-button>
    </div>
    <!-- 表格区域 -->
    <div class="shopmall-view">
      <el-table
        ref="advertisementTable"
        v-loading="loading"
        :data="tableData"
        :row-key="getRowKeys"
        size="small"
        :stripe="false"
      >
        <el-table-column label="序号" align="center" width="55" type="index" />
        <el-table-column label="图片" align="center" min-width="200">
          <template slot-scope="scope">
            <img :src="scope.row.picture" alt="图片" class="ad_picture">
          </template>
        </el-table-column>
        <el-table-column label="图片名称" align="center" prop="pictureName" min-width="80" />
        <el-table-column label="排序" prop="sort" align="center" min-width="80" />
        <el-table-column align="center" label="状态" min-width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.useStatus==='01'" style="color:#00AACA">启用</span>
            <span v-if="scope.row.useStatus==='02'" style="color:#F77C46">停用</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" min-width="120">
          <template slot-scope="scope">
            {{ $Moment(scope.row.createTime).format(momentDate) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="340"
          align="center"
        >
          <template slot-scope="scope">
            <el-link type="primary">
              <span v-if="scope.row.useStatus==='01'" style="color:#F77C46" @click="changeStatus(scope.row.id, '02')">[停用]</span>
              <span v-if="scope.row.useStatus==='02'" @click="changeStatus(scope.row.id, '01')">[启用]</span>
            </el-link>
            <el-link type="primary" @click="viewOrEditAdvert(scope.row.id, 'edit')">
              [修改]
            </el-link>
            <el-link type="primary" @click="deleteAdvert(scope.row.id)">
              [删除]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页组件-->
    <pagination :pagination-data="paginationData" @change="getData" />
    <!-- 查看/新增/编辑 弹框 -->
    <advertisementManageDialog ref="advertDialog" @repload="repload" />
  </div>
</template>

<script>
/* api */
import {
  getAdvertisementPictureList,
  advertisementPictureStatus,
  deleteAdvertisementPicture,
  getAdvertisementPictureDetails
} from '@/api/shoppingmall/products'
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
import advertisementManageDialog from './advertisement-dialog.vue'// 分页
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 常用方法
import { getLoginRegistImage } from '@/api/common/tools.js'// 地址加密获取图片
export default {
  name: 'AdvertisementList',
  components: {
    pagination,
    advertisementManageDialog
  },
  data() {
    return {
      loading: false,
      // 获取row的key值
      getRowKeys(row) {
        return row.id
      },
      // table数据
      tableData: [],
      // search参数
      initDataParams: {
        adId: '', // 广告位ID
        pictureName: '', // 图片名称
        useStatus: '' // 是否使用 01启用,02禁用
      },
      // select使用状态数组 01 启用 02 停用
      options: [
        {
          value: '00',
          label: '请选择',
          disabled: true
        },
        {
          value: '01',
          label: '启用'
        },
        {
          value: '02',
          label: '停用'
        }
      ],
      // 分页数据
      paginationData: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  watch: {
  },
  created() {
    this.getData()
  },
  methods: {
    // 加载数据
    getData() {
      this.loading = true
      const params = this.getQueryParams()// 查询条件
      getAdvertisementPictureList(params).then(res => {
        if (res.header.ret === '0') {
          this.loading = false
          this.tableData = res.data.records
          this.tableData.forEach(item => {
            item.picture = getLoginRegistImage(item.picture)
          })
          this.paginationData.total = res.data.total
        }
      }).catch((err) => {
        this.loading = false
        throw new Error('获取广告列表失败' + err)
      })
    },
    // 查询条件
    getQueryParams() {
      const param = Object.assign({}, this.initDataParams)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      // todo 根据广告位ID查询,查不到
      // param.adId = this.$route.query.id
      return filterObj(param)
    },
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      // 获取列表信息
      this.getData()
    },
    // 重置
    resetList() {
      this.$refs.formitInfo.resetFields()
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      // 获取列表信息
      this.getData()
    },
    // 新增/编辑后 刷新列表
    repload() {
      this.paginationData.pageNo = 1
      this.getData()
    },
    // 新增广告位
    addInstitutions() {
      this.$refs.advertDialog.open({
        visible: true,
        id: '',
        type: 'add' // 类型 add 新增/view 查看/edit 编辑
      })
    },
    // 修改启用/停用状态
    changeStatus(id, useStatus) {
      this.$confirm('确认要修改广告状态吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        advertisementPictureStatus(id, useStatus).then(res => {
          if (res.header.ret === '0') {
            this.getData()
            this.$message.success('修改状态成功！')
          }
        })
      }).catch(() => {})
    },
    // 删除广告位
    deleteAdvert(id) {
      this.$confirm('确认要删除广告吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deleteAdvertisementPicture(id).then(res => {
          if (res.header.ret === '0') {
            this.getData()
            this.$message.success('删除成功！')
          }
        })
      }).catch(() => {})
    },
    // 查看/编辑广告位
    viewOrEditAdvert(id, code) {
      getAdvertisementPictureDetails(id).then(res => {
        if (res.header.ret === '0') {
          this.$refs.advertDialog.open({
            visible: true,
            advertData: res.data,
            id: id,
            type: code // 类型 add 新增/view 查看/edit 编辑
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.shopmall-page-add {
  width: 100%;
  margin-top:  12px;
  padding-left: 12px;
  display: flex;
  align-items: center;
}
.crumbs_nav_main {
  padding: 10px 12px;
  font-size: 14px;
}
.crumbs_nav_main .pos_title .iconfont {
  margin-right: 8px;
  color: #00AACA;
}
.ad_picture {
  width:90%;
  height:90px;
  padding: 5px 0;
  object-fit: fill;
}
</style>
