<template>
    <div class="main-content__div">
      <div class="table-box__div" v-cloak v-loAdPlaceing="loAdPlaceingFlag">
        <el-form  class="demo-form-inline">
          <el-form-item label="广告位名称">
            <el-input
              placeholder="广告位名称"
              v-model="keyword"
              style="width:150px;"
              size="small"
              >
            </el-input>
          </el-form-item>
          
          <!-- 搜索按钮 -->
          <el-form-item>
            <el-button type="primary" size="small" @click="searchFrom()">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 列表 -->
      <div class="table-box__div" v-cloak v-loAdPlaceing="loAdPlaceingFlag">
        <!-- 查询表单 -->
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item>
            <el-button type="primary" size="small" @click="AdPlacedClick">添加广告位</el-button>
          </el-form-item>
        
        </el-form>
        <el-table
          size="mini"
          :data="tableData"
          :empty-text="tableDataLoAdPlaceingText"
          style="width: 100%;font-size: 15px;"
          :heAdPlaceer-cell-style="{background:'#FAFAFA'}"
        >
          <el-table-column label="编号" width="150" prop="id"></el-table-column>
          <el-table-column label="广告位类型" prop="type_name" width="150"></el-table-column>
          <el-table-column label="广告位名称" prop="name" width="250"></el-table-column>
          <el-table-column label="宽度(PX)" prop="width" width="100"></el-table-column>
          <el-table-column label="高度(PX)" prop="height" width="100"></el-table-column>
          <el-table-column label="状态" prop="status_name" width="100"></el-table-column>
          <el-table-column label="价格" prop="price" width="150">
            <template v-slot="scope">
              <div v-if="scope.row.price==0">面议</div>   
              <div v-else>{{scope.row.price}}</div>        
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="created_at" width="200"></el-table-column>
          <el-table-column label="更新时间" prop="updated_at" width="200"></el-table-column>
          <el-table-column   prop="" label="操作" align="center"  fixed="right" min-width="285">
                  <template v-slot="scope">
                    <div style="display: flex;flex-direction: row;justify-content: center;align-items: center;">
                      <span class="btn"  @click="adList(scope.row, scope.$index)">广告列表</span>
                      <span class="btn"  @click="AdClick(scope.row, scope.$index)">添加广告</span>
                      <span class="btn"  @click="updateClick(scope.row, scope.$index)">编辑</span>
                      <span class="btn"  @click="deleteAuthority(scope.row,scope.$index)">删除</span>
                    </div>
                  </template>
                </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="block" v-show="pagination" style="margin-top: 10px;float: right;">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="parseInt(tableCount)">
          </el-pagination>
        </div>
      </div>
    </div>
  <Dialog title="温馨提示" content="您确认要删除该广告位吗？"
          @close="DialogShow = false"
          @submitSureClick="submitSureClickDel"
          @DialogFalse="DialogShow = false"
          :innerVisible="DialogShow">
  </Dialog>
  <DialogSlot
      title="添加/编辑"
      @close="AdPlaceDialogShow = false"
      @DialogClose="AdPlaceDialogShow = false"
      :innerVisible="AdPlaceDialogShow"
      @colesClick="AdPlaceDialogShow  = false"
  >
    <editFrom
        @colesClick="AdPlaceDialogShow = false"
        :AdPlaceDataForm="AdPlaceDataForm"
         :isloAdPlaceing="isloAdPlaceing"
        @AdPlaceSubmitClick="AdPlaceSubmitClick"
        @closeDlg="closeDlg"
        >
    </editFrom>
  </DialogSlot>



  <DialogSlot
      title="添加/编辑"
      @close="AdDialogShow = false"
      @DialogClose="AdDialogShow = false"
      :innerVisible="AdDialogShow"
      @colesClick="AdDialogShow  = false"
  >
    <Ad
        @colesClick="AdDialogShow = false"
        :AdDataForm="AdDataForm"
         :isloAding="isloAdPlaceing"
        @AdSubmitClick="AdSubmitClick"
        @closeDlg="closeDlg"
        >
    </Ad>
  </DialogSlot>
  </template>
  <script>
  import Dialog from "../../components/dialog";
  import editFrom from "./components/edit";
  import Ad from "./components/ad";
  import DialogSlot from "../../components/DialogSlot";
  export default {
    components: {
      Dialog,
      editFrom,
      DialogSlot,
      Ad,
    },
    data() {
      return {
        value:"1",
        //  分类列表数据
        tableData: [],
        //  加载中数据提示
        tableDataLoAdPlaceingText:"拼命加载中······",
        //  是否显示分页
        pagination:false,
        //  每页显示的条数
        pageSize:10,
        //  当前页
        currentPage:1,
        //  分类列表数据总条数
        tableCount:0,
  
        loAdPlaceingFlag: false,
        isShowSource: false, // 是否显示来源列
        id:0,
        DialogShow: false, //控制删除弹窗
        AdPlaceId:'',
        AdPlaceRowIndex:'',
        AdPlaceDataForm: {},
        AdPlaceTableData: [],
        isloAdPlaceing:false, //提交按钮状态
        AdPlaceDialogShow: false, // 控制保存弹窗
        pid:0,
        keyword:'',

        loAdingFlag: false,
        AdId:'',
        AdRowIndex:'',
        AdDataForm: {},
        AdTableData: [],
        isloAding:false, //提交按钮状态
        AdDialogShow: false, // 控制保存弹窗
      }
    },
    mounted() {
     
    },
    methods: {
      // 获取分类信息列表
      getList:function(){
        let _t = this;
        _t.loAdPlaceingFlag = true;
        let parames = {
            'page':this.currentPage,
            'pageSize':this.pageSize,
            'keyWord':this.keyword
        }
        console.log("参数：",parames)
        this.$api.ad.getAdPlaceList(parames).then(res=>{
                _t.loAdPlaceingFlag = false;
                if(res.code==200){
                  if(res.data.rows.length==0){
                    this.$message.error("还没有数据")
                    return
                  }
                    _t.tableData = res.data.rows;
                    _t.tableCount = res.data.count;
                     //总条数大于每页显示的条数时显示分页
                    if( _t.tableCount > _t.pageSize ){
                     _t.pagination = true;
                    }
                    if( _t.tableData.length==0 ){
                     _t.tableDataLoAdPlaceingText = "暂无数据";
                    }
                }else{
                    this.$message.error(res.message)
                }
            })
      },
      // 点击翻页
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getList();
      },
      // 查看下一级
      nextClick( id ){
        let _t = this;
        _t.id = id
        _t.currentPage = 1
        _t.pageSize = 10
       this.getList()
      },
      AdPlacedClick(){
        this.AdPlaceDataForm = {}
        this.AdPlaceDialogShow = true
      },

      AdClick(row){
        this.AdDataForm = row
        this.AdDialogShow = true
      },

      //更新分类
      updateClick(row,index){
        console.log(row)
        this.AdPlaceDataForm = row
        this.rowIndex = index
        this.AdPlaceDialogShow = true
      },
      closeDlg(val){
          this.AdPlaceDialogShow = false
          this.isloAdPlaceing = false
      },
      AdPlaceSubmitClick(data){
        let _t = this
        console.log("提交数据",data)
        let parames = data
        _t.isloAdPlaceing = true
        //有id更新，没有新增
        if(parames.id){
          this.$api.ad.updateAdPlace(parames).then(res=>{
                _t.loAdPlaceingFlag = false;
                if(res.code==200){
                  _t.tableData[_t.rowIndex] = parames
                     console.log("修改成功")
                     this.getList()
                }else{
                    this.$message.error("修改失败")
                }
                this.AdPlaceDialogShow = false
                this.isloAdPlaceing = false
            })
        }else{
          this.$api.ad.createAdPlace(parames).then(res=>{
                _t.loAdPlaceingFlag = false;
                if(res.code==200){
                     console.log("创建成功")
                    this.getList()
                }else{
                    this.$message.error(res.message)
                }
                this.AdPlaceDialogShow = false
                this.isloAdPlaceing = false
            })
        }
       
      },
      AdSubmitClick(data){
        let _t = this
        console.log("提交数据",data)
        let parames = data
        _t.isloAding = true
        //有id更新，没有新增
        this.$api.ad.createAd(parames).then(res=>{
              _t.loAdingFlag = false;
              if(res.code==200){
                    console.log("创建成功")
                  this.getList()
              }else{
                  this.$message.error(res.message)
              }
              this.AdDialogShow = false
              this.isloAding = false
          })
      },
      //删除分类
      deleteAuthority(row, index) {
        this.AdPlaceId = row.id
        this.AdPlaceRowIndex = index
        this.DialogShow = true
      },
      searchFrom(){
        this.currentPage = 1
        this.pageSize = 10
        this.getList()
      },
      submitSureClickDel(row,index){
        let _t = this
        let parames = {
          'id':this.AdPlaceId
        }
        this.$api.ad.delAdPlace(parames).then(res=>{
          if(res.code==200){
            this.DialogShow = false
            _t.tableData.splice(index, 1);
            this.getList()
          }else{
            this.$message.error(res.message)
          }
        })
      },
      adList(row){
        console.log(row)
          this.$router.push("/left/adList/"+row.id)
      }
    },
    created() {
        this.pid = this.$route.params.Id;
        this.getList();
    }
  }
  </script>
  
  <style >
  .demo-form-inline{
      display: flex;
  }
  .demo-form-inline .el-form-item{
    margin: 0 20px;
  }
  </style>