<template>
  <div class="advertising">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template #advertImg="{ row }">
        <img :src="picUrl(row.advertImg)" alt="广告图片" style="max-height: 150px" />
      </template>
       <template #advertBack="{ row }">
      <img :src="picUrl(row.advertBack)" alt="背景图片" style="max-height: 150px;">
    </template>
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
          >新增</el-button
        >
      </template>

      <!-- <template slot-scope="scope"
                slot="status">
        <el-tag v-if="scope.row.status === 0"
                size="small"
                type="danger">未启用</el-tag>
        <el-tag v-else
                size="small">启用</el-tag>
      </template> -->

      <template slot-scope="scope" slot="menu" >
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="operateHandle(scope.row)"
          >{{scope.row.status == 1?'下线':"上线"}}</el-button
        >
        <el-button
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="addOrUpdateHandle(scope.row)"
          >修改</el-button
        >
        <el-button
        type="danger"
        icon="el-icon-delete"
        size="small"
        @click.stop="deleteHandle(scope.row,scope.index)">删除</el-button>
      </template>
    </avue-crud>
 
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import { tableOption } from "@/crud/marketing/advertising";
import AddOrUpdate from './advertising-add-or-update'
export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      searchForm: {},
      dataListLoading: false,
      addOrUpdateVisible: false,
      tableOption: tableOption,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,

    };
  },
  components: {
    AddOrUpdate
  },
  methods: {
    picUrl(url) {
      if(!url) return
      if(url.indexOf('http') === -1) {
        return this.resourcesUrl + url.split(',')[0]
      }else {
        return url.split(',')[0]
      }
    },
    // 获取数据列表
    getDataList(page) {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/advert/search"),
        method: "get",
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page ? page.currentPage : 1,
              size: page ? page.pageSize : 20,
            },
            this.searchForm
          )
        ),
      }).then(({ data }) => {
        this.page.total = data.total;
        this.page.pageSize = data.size;
        this.page.currentPage = data.current;
        this.dataList = data.records;
        this.dataListLoading = false;
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 点击查询
    searchChange(params) {
      this.searchForm = params;
      this.getDataList(this.page);
    },
    operateHandle(row){
        let msg = row.status == 1 ? "下线" : "上线";
        this.$confirm(`确定进行${msg}操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/advert/operate/" + row.id),
            method: "get",
          }).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.refreshChange();
              },
            });
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(row, index) {
      this.$confirm(`确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/advert/del/" + row.id),
            method: "get",
            // data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.refreshChange();
              },
            });
          });
        })
        .catch(() => {});
    },
    // 刷新回调用
    refreshChange() {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage;
      this.getDataList(this.page);
    },
  },
};
</script>
