<style lang="scss" scoped>
.preview {
  .el-form-item {
    margin-bottom: 15px;
  }
  label,
  p {
    line-height: 25px !important;
  }
}
// 上传
/deep/.avatar-uploader .el-upload {
    width: 148px;
    height: 148px;
    line-height: 148px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .avatar{
        width: 100%;
        height: auto;
    }
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 68px;
    height: 68px;
    line-height: 68px;
    text-align: center;
}
.avatar {
    width: 68px;
    height: 68px;
    display: block;
}
/deep/.speaker_avatar .el-upload{
    width: 80px;
    height: 80px;
    line-height: 80px;
}
.hotel_imgs {
    /deep/.el-upload--picture-card{
    width:79px;
    height: 79px;
    line-height: 79px;
    }   
}
</style>
<template>
  <section>
    <p><span>系统管理></span>Banner图管理</p>
    <div class="content">
      <div class="filter">
        <el-form
          size="small"
          label-width="70px"
          :inline="true"
          label-position="left"
        >
          <el-form-item label="">
            <el-input
              clearable
              placeholder="请输入Banner图标题"
              v-model="form.name"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-button
              @click="form1={},(dialogVisible = true), (dialogTitle = '新增Banner图')"
              type="primary"
              >新增Banner图</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        class="table"
        :data="list"
        height="57vh"
        border
        v-loading="loading"
        default-expand-all
        row-key="id"
        style="width: 100%"
      >
        <el-table-column prop="name" label="Banner标题" align="center"> </el-table-column>
        <el-table-column label="Banner图" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.img" alt="" width="220" height="80" />
          </template>
        </el-table-column>
        <el-table-column prop="is_active" align="center" label="是否开启">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_active">开启</el-tag>
            <el-tag v-else type="info">关闭</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_active" align="center" label="是否跳转">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_jump">跳转</el-tag>
            <el-tag v-else type="info">不跳转</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="url" align="center" label="跳转链接"> </el-table-column>
        <el-table-column prop="zip" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button class="edit" type="text" @click="edit(scope.row.id)"
              >编辑</el-button
            >
            <el-button class="del" @click="del(scope.row.id)" type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Page
        ref="pageButton"
        :current="form.page"
        :page_size="form.page_size"
        :total="total"
        @pageChange="gopage"
      />
    </div>

    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
    >
      <el-form size="small" class="preview" :inline="false" label-width="120px">
        <el-form-item label="Banner标题">
          <el-input v-model="form1.name"></el-input>
        </el-form-item>
        <el-form-item label="封面图：">
          <el-upload
            class="avatar-uploader"
            action="/api/admin/uploadfile"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form1.img" height="100%" :src="form1.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <span style="font-size: 12px; color: #999999"
            >建议图片尺寸为：210*160</span
          >
        </el-form-item>
        <el-form-item label="是否开启">
          <el-radio v-model="form1.is_active" :label="1">是</el-radio>
          <el-radio v-model="form1.is_active" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="是否跳转">
          <el-radio v-model="form1.is_jump" :label="1">是</el-radio>
          <el-radio v-model="form1.is_jump" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="跳转地址">
          <el-input v-model="form1.url" placeholder="请输入跳转地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      form: { name: "", page: 1, page_size: 20 },
      form1: {is_active:1,is_jump:0,img:""},
      total: 1,
      list: [{ name: "2333" }],
      loading: false,
      input: "",
      dialogVisible: false,
      dialogTitle: "",
      defaultProps: {},
      data: [],
      selected: [],
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.form1.img = file.response.data;
      this.$set(this.form1,"img",file.response.data)
    },
    search() {
      let parm = this.form;
      this.getData();
    },
    getData() {
      var parm = this.form;
      this.loading = true;
      this.$api.getBannersList(parm).then((res) => {
        let list = res.data.data.list;
        this.list = list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    gopage(size) {
      if (size) {
        this.form.page_size = size;
      }
      this.form.page = this.$refs.pageButton.page;
      this.getData();
    },
    edit(id){
      this.$api.getBanners({id:id}).then((res)=>{
        this.form1 = res.data.data
        this.dialogVisible = true
      })
    },
    del(id) {
      this.$confirm("确定删除吗", "提示", {
        type: "warning",
      }).then(() => {
        this.$api.delBanners({ id: id }).then((res) => {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getData();
        });
      });
    },
    save() {
      let parm = this.form1;
      if (parm.id) {
        this.$api.updateBanners(parm).then((res) => {
          if (res.data.code == 0) {
            this.$message({
              type: "success",
              message: "保存成功!",
            });
            this.getData();
          } else {
            this.$message.error("失败!");
          }
        });
      } else {
        this.$api.addBanners(parm).then((res) => {
          if (res.data.code == 0) {
            this.$message({
              type: "success",
              message: "保存成功!",
            });
            this.getData();
          } else {
            this.$message.error("失败!");
          }
        });
      }
      this.dialogVisible = false;
    },
  },
  created() {
    this.getData();
  },
};
</script>
