<!--
  @description: 广告管理
  @Modifier：dijia
  @routerPath:/personalCenter/advertManage
  @date：  2021/7/20
-->
<template>
  <div>
    <div class="shopmall-page-title">
      广告管理
    </div>
    <!-- 搜索表单区域 -->
    <div class="search-content clearfix">
      <section class="fl">
        <el-form ref="formitInfo" :model="initDataParams" :inline="true">
          <el-form-item label="广告位名称" prop="advertisingName">
            <el-input v-model.trim="initDataParams.advertisingName" 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="listTable"
        v-loading="loading"
        :data="tableData"
        :row-key="getRowKeys"
        size="small"
        :stripe="false"
      >
        <el-table-column align="center" label="序号" width="55" type="index" />
        <el-table-column label="广告位名称" prop="advertisingName" min-width="200" />
        <el-table-column label="尺寸(px)" align="center" min-width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.measurement || '无' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="广告数" prop="advertisementCount" align="center" min-width="80" />
        <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 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="操作"
          width="340"
          align="center"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="viewOrEditAdvert(scope.row.id, 'view')">
              [查看]
            </el-link>
            <el-link type="primary" @click="viewOrEditAdvert(scope.row.id, 'edit')">
              [编辑]
            </el-link>
            <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="deleteAdvert(scope.row.id)">
              [删除]
            </el-link>
            <el-link type="primary" @click="toAdvertisement(scope.row.id)">
              [广告列表]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页组件-->
    <pagination :pagination-data="paginationData" @change="getData" />
    <!-- 查看/新增/编辑 弹框 -->
    <advertManageDialog ref="advertDialog" @repload="repload" />
  </div>
</template>

<script>
/* api */
import {
  getShptAdvertisementList,
  advertisementStatus,
  deleteAdvertisement,
  getAdvertisementDetails
} from '@/api/shoppingmall/products'
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
import advertManageDialog from './advertManage-dialog.vue'// 分页
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 常用方法
export default {
  name: 'AdvertManageList',
  components: {
    pagination,
    advertManageDialog
  },
  data() {
    return {
      loading: false,
      // 获取row的key值
      getRowKeys(row) {
        return row.id
      },
      // table数据
      tableData: [],
      // search参数
      initDataParams: {
        advertisingName: '',
        useStatus: ''
      },
      // 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()// 查询条件
      getShptAdvertisementList(params).then(res => {
        if (res.header.ret === '0') {
          this.loading = false
          this.tableData = res.data.records
          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
      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,
        type: 'add' // 类型 add 新增/view 查看/edit 编辑
      })
    },
    // 修改启用/停用状态
    changeStatus(id, useStatus) {
      this.$confirm('确认要修改广告位状态吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        advertisementStatus(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(() => {
        deleteAdvertisement(id).then(res => {
          if (res.header.ret === '0') {
            this.getData()
            this.$message.success('删除成功！')
          }
        })
      }).catch(() => {})
    },
    // 查看/编辑广告位
    viewOrEditAdvert(id, code) {
      getAdvertisementDetails(id).then(res => {
        if (res.header.ret === '0') {
          this.$refs.advertDialog.open({
            visible: true,
            advertData: res.data,
            type: code // 类型 add 新增/view 查看/edit 编辑
          })
        }
      })
    },
    // 跳转到广告列表页
    toAdvertisement(id) {
      this.$router.push({
        path: '/personalCenter/advertisementList',
        query: {
          id: id
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.shopmall-page-add {
  width: 100%;
  margin-top:  12px;
  padding-left: 12px;
  display: flex;
  align-items: center;
}
</style>
