<template>
  <div class="app-container">
    <div class="card-change" >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="" prop="name">
          <el-input
            v-model="queryParams.bannerName"
            placeholder="请输入banner图名称"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

<!--        <el-form-item style="float: right">-->
          <el-form-item label="" prop="bannerName">
            <el-select v-model="queryParams.systemPosition" placeholder="请选择系统位置">
              <el-option
                v-for="item in systemPositionList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

            <el-form-item label="" prop="bannerName">
              <el-select v-model="queryParams.bannerStatus" placeholder="请选择banner图状态">
                <el-option label="停用" value="0" ></el-option>
                <el-option label="启用" value="1" ></el-option>
<!--                <el-option-->
<!--                  v-for="item in systemPositionList"-->
<!--                  :key="item.value"-->
<!--                  :label="item.label"-->
<!--                  :value="item.value">-->
<!--                </el-option>-->
              </el-select>
            </el-form-item>

          <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="handleAdd"
              v-hasPermi="['setting:banner:add']"
            >新增banner图</el-button>
        </el-form-item>
      </el-form>
<!--    </el-card>-->

<!--    &lt;!&ndash;    列表信息&ndash;&gt;-->
<!--    <el-card style="margin-top: 20px" >-->


      <div style="height: 70vh;overflow: auto">
    <el-table :data="tableData" style="width: 100%;" border>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="bannerName" label="banner名称"></el-table-column>
      <el-table-column prop="productImage" label="图片">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="imagePath+scope.row.productImage"
            :preview-src-list="[imagePath + scope.row.productImage]"
            fit="cover">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="systemPosition" label="系统位置"></el-table-column>
      <el-table-column prop="sortOrder" label="排序号"></el-table-column>
      <el-table-column prop="bannerLink" label="banner图链接"></el-table-column>
      <el-table-column prop="note" label="备注"></el-table-column>
      <el-table-column prop="bannerStatus" label="banner图状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.bannerStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="changeStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createByName" label="创建人"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="edit(scope.row)"
            v-hasPermi="['setting:banner:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="del(scope.row)"
            v-hasPermi="['setting:banner:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
      </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    </div>

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="55%" append-to-body>
      <el-form ref="form" :model="deviceInfo" label-width="80px" :rules="rules">
        <el-form :model="deviceInfo" ref="deviceForm" label-width="120px" :rules="rules">
          <el-form-item label="banner名称" prop="bannerName">
            <el-input v-model="deviceInfo.bannerName" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="图片" prop="uploadAction">
            <el-upload
              :action="uploadAction"
              list-type="picture-card"
              :class="{hide:hideUpload}"
              :file-list="uploadFiles"
              :auto-upload="true"
              :show-file-list="false"
              :headers="uploadHeader"
              :on-success="handleUploadSuccessCover">
              <img
                v-if="this.deviceInfo.productImage"
                :src="imagePath + this.deviceInfo.productImage"
                class="list-img"
                style="width: 750px; height: 100%"
              />
              <i v-if="!this.deviceInfo.productImage" class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="系统位置" prop="systemPosition">
            <el-select v-model="deviceInfo.systemPosition" placeholder="系统位置" style="width: 300px">
              <el-option
                v-for="item in systemPositionList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序号" prop="sortOrder">
            <el-input v-model="deviceInfo.sortOrder" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="Banner链接" prop="bannerLink">
            <el-input v-model="deviceInfo.bannerLink" style="width: 300px"></el-input>
          </el-form-item>
          <el-form-item label="Banner状态" prop="bannerStatus">
            <el-switch
              v-model="deviceInfo.bannerStatus"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item label="备注" prop="note">
            <el-input v-model="deviceInfo.note" style="width: 300px" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('deviceForm')">提交</el-button>
          </el-form-item>
        </el-form>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>

import {deleteBannerApi, getBannerListApi, saveBannerApi, updateBannerApi} from "@/api/sys/banner";
import { getToken } from '@/utils/auth'

export default {
  name: "printIndex",

  data() {
    return {
      tableData: [],
      deviceInfo: {
        id: null,            // 自增id
        bannerName: '',     // Banner名称
        productImage: '',           // 商品图片
        systemPosition: '',      // 系统位置
        sortOrder: null,        // 排序号
        bannerLink: '',          // Banner链接
        bannerStatus: false,       // Banner状态
        note: '',    // 备注
        createByName: '', //

      },
      systemPositionList:[{
        value: '首页',
        label: '首页'
      }, {
        value: '系统首页',
        label: '系统首页'
      },],
      // 是否显示弹出层
      open: false,
      title:"",
      total:0,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        bannerName:'',
        systemPosition:'',
        bannerStatus:'',
      },
      rules: {
        bannerName:[
            {required: true, message: '请填写banner名称', trigger: 'blur'},
        ],
        systemPosition:[
            {required: true, message: '请填写系统位置', trigger: ''},
        ],
        sortOrder:[
            {required: true, message: '请填写排序号', trigger: 'blur'},
        ],
        bannerLink:[
            {required: true, message: '请填写链接', trigger: 'blur'},
        ],
        note:[
            {required: true, message: '请选择备注', trigger: 'blur'},
        ],
      },

      // 上传地址
      uploadAction: process.env.VUE_APP_BASE_API + 'backendApi/file/upload',
      uploadHeader: { 'Access-Token' : getToken() },  // 上传文件列表
      uploadFiles: [
        {name:"nihao",
          url:'http://localhost:8008/static/uploadImages/20231103/ffbbe7d3ee1441fdaf706802fa0f176a.png'}
      ],
      // 图片根目录
      imagePath: process.env.VUE_APP_BASE_API,
      // 隐藏上传
      hideUpload: false,
    };
  },
  created() {
    this.getList()
  },
  methods: {
    getList(){
      getBannerListApi(this.queryParams).then(res=>{
        this.tableData = res.data.records;
        this.total = res.data.total
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 表单验证通过，可以提交数据
          this.submitDeviceInfo();
        } else {
          // 表单验证失败，不执行任何操作
          return false;
        }
      });
    },
    submitDeviceInfo() {
      // 在这里调用接口提交设备信息数据
      console.log('提交设备信息数据:', this.deviceInfo);
      if (this.deviceInfo.id) {
        updateBannerApi(this.deviceInfo).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }

        })
      }else {
        saveBannerApi(this.deviceInfo).then(res=>{
          if(res.code === 200) {
            this.getList()
            this.open = false
          }
        })
      }
    },
    changeStatus(data){
      updateBannerApi(data).then(res=>{
        if(res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改状态成功!'
          });
          this.getList()
        }

      })
    },
    // 上传封面
    handleUploadSuccessCover(file) {
      this.deviceInfo.productImage = file.data.fileName;
      this.deviceInfo.productImage = file.data.fileName;
    },
    handleQuery(){
      this.getList()
    },
    resetQuery(){
      this.queryParams = {
        page: 1,
        pageSize: 10,
        deviceName:'',
      },
      this.getList()
    },
    handleAdd(e) {
      this.clean()
      this.open = true;
      this.title = "新增banner图"
    },
    edit(e) {
      this.clean()
      this.open = true;
      this.title = "修改banner图"
      this.deviceInfo = e
    },
    del(e) {
      this.$confirm('此操作将永久删除该banner图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBannerApi({id:e.id}).then(res=>{
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList()
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    clean() {
      this.deviceInfo= {
        id: null,            // 自增id
        bannerName: '',     // Banner名称
        productImage: '',           // 商品图片
        systemPosition: '',      // 系统位置
        sortOrder: null,        // 排序号
        bannerLink: '',          // Banner链接
        bannerStatus: false,       // Banner状态
        note: '',    // 备注
        createByName: '', //
      }
    },
  }
};
</script>
<style lang="scss" scoped>
.common-dialog >>> .el-upload--picture-card {
  width: 60px;
  height: 50px;
  line-height: 60px;
}
.d-dialog >>> .el-upload--picture-card {
  width: 100px;
  height: 100px;
  line-height: 100px;
}

.d-dialog >>> .avatar-uploader .el-upload {
  width: 100px;
  height: 100px;
}
.d-dialog >>> .el-upload-list--picture-card .el-upload-list__item {
  width: 100px;
  height: 100px;
}
.list-img{
  width: 98px;
  height: 98px;
}
</style>

