<template>
  <div class="container">
    <!-- 头部 -->
    <div class="head">
      <div>banner列表</div>
      <el-button type="primary" @click="addItem">添加banner</el-button>
    </div>
    <!-- 主体 -->
    <div class="app-container calendar-list-container">
      <!-- 条件搜索 -->
      <div class="filter-container">
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="所有状态">
            <el-select v-model="searchForm.status">
              <el-option v-for="(item, index) in bannerStatus" :key="index" :label="item" :value="~~index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="banner标题">
            <el-input v-model="searchForm.title" placeholder="请输入banner标题"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="search">搜索</el-button>
            <el-button type="primary" plain @click="clearFilterData">清除搜索</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 数据列表 -->
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
        <el-table-column align="center" label="序号" width="65">
          <template slot-scope="scope">
            {{scope.row.index}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="banner标题" prop="title"></el-table-column>
        <el-table-column align="center" label="banner图">
          <template slot-scope="scope">
            <img class="cover" :src="scope.row.cover" @click.stop="showPreview(scope.row.cover)"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <!-- 根据状态获取对应中文 -->
            {{ getStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="250">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button :type="scope.row.status === 1 ? 'info': 'warning'" size="mini" @click="changeParams(scope.row, 'status')">{{scope.row.status === 1 ? '下架': '上架'}}</el-button>
            <el-button type="danger" size="mini" @click="changeParams(scope.row, 'isDel')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="page-foot">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="currentChange"
          :current-page.sync="listQuery.page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
        <!-- <el-pagination
          background
          layout="prev, pager, next"
          :page-size="listQuery.limit"
          :total="total" @current-change="currentChange">
        </el-pagination> -->
      </div>

      <!-- 使用图片查看器预览当前缩略图 -->
      <el-image-viewer
        v-show="currentIcon.showPreview"
        :on-close="closePreview"
        :url-list="[currentIcon.url]"
      />

      <!-- 添加、编辑弹窗 -->
       <el-dialog
        top="10%"
        :close-on-click-modal="false"
        :title="dialogTitle"
        :visible.sync="dialogVisible"  @closed="closeDialog"> <!-- @open="onOpen" @closed="closeDialog">-->
        <custom-form
        :formData="formObject"
        @submitFunc="updateFunc"
        @reset="resetForm"
        ></custom-form>
      </el-dialog>

    </div>

  </div>
</template>

<script>
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import { banner } from "@/utils/formDatas.js"//表单数据
import { page, update } from "@/api/website/banner"//接口

import CustomForm from "@/components/CustomForm"//自定义表单组件

export default {
  name: "bannerSetting",
  components: {
    CustomForm,
    ElImageViewer
  },
  data() {
    return {
      currentIcon:{
        showPreview: false,
        url: ""
      },
      data: null,//原数据
      list: null,//赋值数据 | 过滤数据
      total: null,//总个数
      listLoading: true,//请求加载状态
      //请求参数。页码页数等
      listQuery: {
        page: 1,
        limit: 10
      },
      //搜索表单
      searchForm: {
        status: 0,
        title: ""
      },
      formObject: {
        formVal: banner.init(),//提交的表单参数
        rules: banner.rules,//表单规则
        formItem: banner.formInfo,//表单项。
      },
      //是否启用，1、启用，2、禁用
      bannerStatus: {
        0: '全部',
        1: '启用',
        2: '禁用',
      },
      dialogVisible: false,//弹窗显示控制
      dialogTitle: "新增banner"//弹窗标题
    }
  },
  mounted() {
    this.getList()//获取数据
  },
  methods: {
    handleSizeChange(val) {
      this.listQuery.limit = val

      this.getList();
    },
    closePreview() {
      this.currentIcon.showPreview = false;
    },
    showPreview(url){
      this.currentIcon.showPreview = true
      this.currentIcon.url = url
    },
    //获取图片链接
    getSrc(val) {
      this.editForm.cover = val
    },
    //获取自定义组件值
    getCustomInput(val) {
      this.editForm.url = val
    },
    //搜索按钮
    search() {
      this.listQuery = {
        status: this.searchForm.status || null,
        title: this.searchForm.title,
        page: 1,
        limit: 10
      }
      this.getList();
    },
    //清除搜索
    clearFilterData() {
      this.searchForm = {
        status: 0,
        title: ""
      }

      this.listQuery = {
        status: this.searchForm.status || null,
        title: this.searchForm.title,
        page: 1,
        limit: 10
      }
      this.getList()
    },
    //切换页码
    currentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    //编辑
    edit(row) {
      this.dialogTitle = "编辑banner";
      this.formObject.formVal = this.filterParams(row)
      this.dialogVisible = true;
    },
    closeDialog() {
      this.$bus.$emit('upload-clearFiles')
    },
    //更新方法
    updateFunc(params) {
      params.rank = ~~params.rank;
      update(params).then(res => {
        if (res.status ==  200) {
          this.dialogVisible = false;
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      })
    },
    //取消表单
    resetForm() {
      this.dialogVisible = false;
    },
    //新增banner
    addItem() {
      this.dialogTitle = "新增banner";
      this.dialogVisible = true;
      this.formObject.formVal = banner.init();

    },
    filterParams(row) {
      let a = {};
      for (let i in this.formObject.formVal) {
        if (row[i] !== '') {
          a[i] = row[i]
        }
      }
      return a
    },
    //获取状态
    getStatus(val) {
      return this.bannerStatus[val]
    },
    //上下架、删除更新
    changeParams(row, key) {
      this.$confirm('确定继续执行该操作吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.formObject.formVal = row;
          this.formObject.formVal = this.filterParams(row)
          if (key == 'isDel') {
            this.formObject.formVal[key] = row[key] == 0 ? 1 : 0;
          } else {
            this.formObject.formVal[key] = row[key] == 1 ? 2 : 1;
          }

          //更新专题
          this.updateFunc(this.formObject.formVal);
        })

    },
    //获取banner列表
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(res => {
        this.list = res.data.data
        this.list.forEach((element,index) => {
          element.index = ((this.listQuery.page-1)*this.listQuery.limit)+(index+1)
        });
        this.data = res.data.data
        this.total = res.data.totalCount;
        this.listLoading = false;
      }).catch(err => {
        this.listLoading = false;
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px;
  width: 100%;
  border-bottom: 1px solid #e6e6e6;
}
.cover {
  width: 50px;
  height: 50px;
  object-fit: cover;
  cursor: pointer;
}
.page-foot {
  margin-top: 20px;
}
</style>
