<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="位置">
        <el-select
          v-model="queryParams.location"
          placeholder="请选择位置"
          clearable
          @change="
            queryParams.pageNum = 1;
            getList();
          "
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="(item, index) in bannerLocationList"
            :key="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getList()"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:ticket:add']"
          >添加banner</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList()"
      ></right-toolbar>
    </el-row>
    <!-- 列表 -->
    <el-table ref="table" v-loading="loading" :data="list">
      <el-table-column label="banner名称" prop="name" />
      <el-table-column label="位置" min-width="100">
        <template slot-scope="{ row }">
          <div>
            {{ row.location.desc }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="banner图片">
        <div slot-scope="{ row }">
          <el-image
            style="width: 250px; height: 100px"
            :src="row.picUrl"
            :preview-src-list="[row.picUrl]"
          />
        </div>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="{ row }">
          <div>{{ row.sort }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <div>
            <el-button
              v-hasPermi="['business:banner:edit']"
              type="text"
              @click="edit(row)"
              >编辑</el-button
            >
            <el-button
              v-hasPermi="['business:banner:remove']"
              type="text"
              class="del"
              @click="del(row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList"
    />

    <banner-create
      v-if="createShow"
      :dialog-show="createShow"
      :banner-id="bannerId"
      @close="close"
    />
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common";
import { goodsBannerList, goodsBannerRemove } from "@/api/business/banner";
import BannerCreate from "./components/BannerCreate";
export default {
  name: "ListBanner",
  components: {
    BannerCreate,
  },
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {},
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      list: [],
      bannerLocationList: [],
      createShow: false,
      bannerId: '',
    };
  },
  created() {
    this.getBannerLocation();
    this.getList();
  },
  methods: {
    getBannerLocation() {
      this.getDicts("banner_location").then((res) => {
        this.bannerLocationList = res && res.data;
      });
    },
    getList() {
      this.loading = true;
      goodsBannerList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            res.rows.forEach((item) => {
              item.picUrl = publicFileGetUrl + item.picUrl.split(",")[0];
              item.location = JSON.parse(item.location);
            });
            this.total = res.total;
            this.list = res.rows;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    edit(item) {
      this.bannerId = item.bannerId;
      this.createShow = true;
    },

    del(item) {
      this.$confirm(`确认删除banner “${item.name}” 吗？`, "删除banner", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        goodsBannerRemove({ bannerId: item.bannerId }).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },

    // 重置
    resetQuery() {
      this.queryParams = {};
      this.pageParams.pageNum = 1
      this.getList();
    },

    handleAdd() {
      this.createShow = true;
    },

    // 关闭弹框
    close() {
      this.createShow = false;
      this.bannerId = "";
      this.getList();
    },
  },
};
</script>
<style>
</style>