<template>
  <div style="background-color:#f7f7f7;padding:10px 10px;">
    <div class="zhuti" v-if="onaction == '1'">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>广告服务</span> <span style="padding:0 5px;">></span> <span
            style="color:#000000e6">广告审核</span></div>
      </div>
      <!-- 线上 -->
      <div>
        <div style="display: flex;font-size: 14px;" >
          <div style="margin-right: 4px;cursor: pointer;" @click="chenge(1)" :class="leixing?'chengeXia greens':''">线上({{xsNum || 0}})</div>
          <div style="margin-left: 20px;margin-right: 4px;cursor: pointer;" @click="chenge(2)" :class="leixing?'':'chengeXia greens'">实体({{xxNum || 0}})</div>
        </div>
        <!-- 搜索 -->
        <div class="formSearch">
          <el-form :inline="true" :model="pagequery" style="margin-top: 15px;">
            <el-form-item label="编号" prop="id">
              <el-input v-model="pagequery.id" placeholder="请输入" style="width: 168px;margin-right: 15px" />
            </el-form-item>
            <el-form-item label="所属端" prop="affiliation" v-if="leixing == 1">
              <el-select v-model="pagequery.affiliation" placeholder="请选择" style="width: 168px;margin-right: 15px">
                <el-option label="游客小程序" value="游客小程序"></el-option>
                <el-option label="商家客户端" value="商家客户端"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="申请人" prop="applicant">
              <el-input v-model="pagequery.applicant" placeholder="请输入" style="width: 168px;margin-right: 15px" />
            </el-form-item>
          </el-form>

          <div>
            <el-button @click="onSubmit" style="background-color: #3F9B6A;color: #fff">查询
            </el-button>
            <el-button @click="resetting" class="buttonHover"
              style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;">重置
            </el-button>
          </div>
        </div>
        <!-- 表格 -->
        <el-table :data="tableData"
          :header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}">
          <el-table-column label="序号" width="50" prop="id">
            <template slot-scope="scope">
              {{scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="编号" prop="id">
          </el-table-column>
          <el-table-column label="所属端"  v-if="leixing == 1">
            <template slot-scope="scope">
              {{scope.row.cereAdvertisingInformation.affiliation}}
            </template>
          </el-table-column>
          <el-table-column label="轮播顺序"  v-if="leixing == 1" >
            <template slot-scope="scope">
              {{scope.row.cereAdvertisingInformation.rotationOrder}}
            </template>
          </el-table-column>


          <!-- <el-table-column label="申请人" prop="applicant" ></el-table-column> -->
          <el-table-column label="投放定价" prop="tenant">
            <template slot-scope="scope">
              {{scope.row.rotationOrde || '-'}}
            </template>
          </el-table-column>
          <el-table-column label="投放时间" prop="scheduleTime" >
          </el-table-column>
          <el-table-column prop="applicationTime" label="提交时间" >
          </el-table-column>
          <el-table-column label="状态" prop="auditStatus" width="80">
            <template slot-scope="scope">
              {{scope.row.auditStatus =='1'?'待审核':scope.row.auditStatus =='2'?'待签约':scope.row.auditStatus =='3'?'已驳回':scope.row.auditStatus =='4'?'已签约':scope.row.auditStatus =='5'?'已终止':scope.row.auditStatus =='6'?'下架':scope.row.auditStatus =='7'?'已投放':'-'}}
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right">
            <template slot-scope="scope">
              <div @click="details(scope.row,'1')" class="tableBtn greens">查看</div>
              <div v-if="scope.row.auditStatus =='1' "  @click="details(scope.row,'2')" class="tableBtn greens">审核</div>
              <div v-if="scope.row.auditStatus == '2'" @click="openhet(scope.row)" class="tableBtn greens">录入合同</div>
              <div @click="removeinfozz(scope.row,'5','终止')" class="tableBtn greens"
                v-if="(scope.row.auditStatus =='2' || scope.row.auditStatus =='4') && leixing">终止
              </div>
              <div @click="opencl(scope.row,'5','终止')" class="tableBtn greens"
                v-if="(scope.row.auditStatus =='2' || scope.row.auditStatus =='4') && !leixing">更改状态
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div style="display: flex;justify-content: space-between;" class="bom">
          <div style="font-size: 14px;">共 <span style="color: #3F9B6A;">{{total}}</span> 项数据</div>
          <el-pagination :current-page="pagequery.pageNumber+1" :page-sizes="[10, 20, 50, 100]" :page-size="10"
            background small layout="prev, pager, next" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange">
          </el-pagination>
        </div>

      </div>

    </div>






    <div class="zhuti" v-if="onaction == '2'">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>广告审核</span> <span style="padding:0 5px;">></span> <span
            style="color:#000000e6">新增</span></div>
      </div>

      <div style="padding: 20px 20px 20px 0;">
        <add :info="formInline" @removeonaction="removeonaction" :isdel="'1'"></add>
      </div>
    </div>
    <div class="zhuti" v-if="onaction == '3'">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>广告审核</span> <span style="padding:0 5px;">></span> <span
            style="color:#000000e6">查看</span></div>
      </div>
      <div>
        <resourceCommodity :issp="issp" :info="detailsinfo" @removeonaction="removeonaction">
        </resourceCommodity>
      </div>
    </div>
    <div class="zhuti" v-if="onaction == '4'">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>广告审核</span> <span style="padding:0 5px;">></span> <span
            style="color:#000000e6">{{contractChangeReason}}</span></div>
      </div>
      <div style="padding: 20px 20px 20px 0;">
        <add :info="detailsinfo" @removeonaction="removeonaction" :leixing="leixing"></add>
      </div>
    </div>
    <el-dialog :visible.sync="addcl" title="更改状态" width="65%" append-to-body center :close-on-click-modal="false"
      :close-on-press-escape="false" :show-close="false">
      <el-form :model="oninfo" v-if="oninfo.cereAdvertisingInformation" ref="detailsinfo" label-width="130px" class="demo-ruleForm" style="padding-right: 130px;margin-top: 20px">
        <el-form-item label="广告位名称" prop="advertisingName">
          <el-input disabled v-model="oninfo.cereAdvertisingInformation.advertisingName" placeholder="请输入"  maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="advertisingSpaceNumber">
          <el-input disabled v-model="oninfo.cereAdvertisingInformation.advertisingSpaceNumber" placeholder="请输入"  maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="投放时间" prop="scheduleTime">
          <el-input disabled v-model="oninfo.scheduleTime" placeholder="请输入"  maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="auditStatus">
          <el-input disabled :placeholder="oninfo.auditStatus =='1'?'待审核':oninfo.auditStatus =='2'?'待签约':oninfo.auditStatus =='3'?'已驳回':oninfo.auditStatus =='4'?'已签约':oninfo.auditStatus =='5'?'已终止':oninfo.auditStatus =='6'?'下架':oninfo.auditStatus =='7'?'已投放':'-'"  maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="状态更改" prop="auditStatus">
          <el-select v-model="auditStatus" placeholder="请选择" style="width: 100%;">
            <el-option label="已投放" value="7"></el-option>
            <el-option label="已终止" value="5"></el-option>
            <el-option label="下架" value="6"></el-option>
          </el-select>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item>
              <div style="margin-top: 20px">
                <el-button @click="changeauditStatus" style="background-color: #3F9B6A;color: #fff;">确定</el-button>
                <el-button @click="addcl = false" class="buttonHover"
                  style="color: #606266;border: 1px solid #DBDBDB;background-color: #fff;">取消</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>


</template>

<script>
  import {
    contractGetAll,
    contractDel,
    addList,
    updateList,
    queryById,
    hetongById,
    hetongadd,
    hetongEdit,
    AdvertiserInfoGetAll,
    AdvertiserInfoEdit,
    AdvertiserInfoDel
  } from '@/api/advertisement.js'
  import add from '@/components/add/addht.vue'
  import resourceCommodity from '@/components/chakan/ggsh'
  import {
    cereResourceStrategy,
    editStatus
  } from '@/api/newly.js'
  import {
    getAlls,changAlls
  } from '@/api/information';
  import {
    likeGet,
  } from '@/api/map1'
   import cl from '@/components/change/cl.vue'
  export default {
    data() {
      return {
        auditStatus:'',
        issp:'1',
        oncetype: '',
        addcl: false,
        oninfo: {},
        multipleSelection: [],

        xsNum: 0,
        xxNum: 0,
        leixing: true,
        contractChangeReason: '',
        detailsinfo: {},
        pagequery: {
          pageNumber: 0,
          pageSize: 10,
          advertisingSpaceType: '线上广告位',
          id:'',
          affiliation:'',
          applicant:'',
        },
        tableData: [],
        total: 0,
        onaction: '1',
        formInline: {},
      }
    },
    components: {
      cl,
      add,
      resourceCommodity,
    },
    created() {
      this.getAll()
    },
    methods: {
      async openhet(row) {
        this.formInline = row
                console.error(row)
                let list = []
        if(row.cereBasicInformationShop && row.cereBasicInformationShop.id) {
          const res = await getAlls({
            pageNumber: 0,
            pageSize: 10,
            publishStatus:'2',
            id: row.cereBasicInformationShop.id
          })
          list = res.data.content
          
        }
        if(row.cereAdvertisingInformation && row.cereAdvertisingInformation.id) {
          const res = await likeGet({
            pageNumber: 0,
            pageSize: 10,
            publishStatus:'2',
            id: row.cereAdvertisingInformation.id
          })
          list = res.data.content
        }
        if(row.cereBasicInformationVenue && row.cereBasicInformationVenue.id) {
          const res = await changAlls({
            pageNumber: 0,
            pageSize: 10,
            publishStatus:'2',
            id: row.cereBasicInformationVenue.id
          })
          list = res.data.content
        }
        console.error(list)
        if(list.length>0) {
          this.onaction = '2'
        } else {
          this.$message({
            message: '该资源已被租赁',
            type: 'error'
          })
        }
        
      },
      changeauditStatus() {
        AdvertiserInfoEdit({
            id: this.oninfo.id,
            auditStatus: this.auditStatus
          }).then(res => {
          
            if (res.code == 200) {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.addcl = false
              this.getAll()
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
            }

          })
      },
      async setmsg(item) {
        let t1 = item.cereAdvertisingInformation.id?item.cereAdvertisingInformation.advertisingName:'资源'
        let t2 = '很抱歉，您未签订合约，'+t1+'租赁申请已被终止。'
        let info = {
          type: '通知消息',
          phone: item.applicant,
          content: {
            type: '1',
            title:'租赁申请通知',
            content: t2
          },
        }
        await this.$setmsg(info);
      },
      removeinfozz(row, e, tit) {
        let that = this
        this.$confirm('确定要' + tit + '吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          AdvertiserInfoEdit({
            id: row.id,
            auditStatus: e
          }).then(res => {
            console.error(res)
            if (res.code == 200) {
              this.$message({
                message: tit + '成功',
                type: 'success'
              })
              this.setmsg(row)
              this.removeonaction('1')
			  
            } else {
              this.$message({
                message: res.msg,
                type: 'error'
              })
            }

          })
        })
      },
      minSev(e) {
        this.multipleSelection = e
        console.error(this.multipleSelection)
        let ids = []
        for (let index = 0; index < this.multipleSelection.length; index++) {
          ids.push(this.multipleSelection[index].id)
        }
        console.error(this.oninfo)
        let c1 = {
          resourcesId: this.oncetype + this.oninfo.id,
          rentalPoliciesIds: ids,
        }
        console.error(c1)
        cereResourceStrategy(c1).then(res => {
          if (res.code == 200) {
            this.addcl = false
            this.$message({
              message: '绑定成功',
              type: 'success'
            })
            editStatus({
              resourcesId: this.oncetype + this.oninfo.id,
              publishStatus: '3'
            }).then(res => {
              this.onSubmit()
            })
          } else {
            this.$message({
              message: '绑定失败',
              type: 'error'
            })
          }
        })
      },
      mingClose() {
        this.multipleSelection = []
        this.addcl = false
      },
      async opencl(row, e) {
        this.oncetype = e
        this.oninfo = row
        this.multipleSelection = []
        this.addcl = true
      },
      chenge(val) {
        if (val == 1) {
          this.leixing = true
          this.pagequery.advertisingSpaceType = '线上广告位'
          this.getAll()
        } else {
          this.leixing = false
          this.pagequery.advertisingSpaceType = '实体广告位'
          this.getAll()
        }
      },
      gettime() {
        // 获取当前时间
        let currentTime = new Date();

        // 获取年份
        let year = currentTime.getFullYear();

        // 获取月份（注意月份是从0开始计数的，所以需要加1）
        let month = currentTime.getMonth() + 1;

        // 获取日期
        let day = currentTime.getDate();

        // 获取小时
        let hours = currentTime.getHours();

        // 获取分钟
        let minutes = currentTime.getMinutes();

        // 获取秒数
        let seconds = currentTime.getSeconds();

        // 获取毫秒数
        let milliseconds = currentTime.getMilliseconds();

        // 格式化时间为 YYYY-MM-DD
        let formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

        // 格式化时间为 HH:MM:SS
        let formattedTime =
          `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;

        // 格式化时间为 YYYY-MM-DD HH:MM:SS
        let formattedDateTime = `${formattedDate} ${formattedTime}`;
        return formattedDateTime

      },
      details(row,type) {
        this.detailsinfo = row
        this.issp = type
        // console.error(this.detailsinfo)
        this.onaction = '3'
      },
      removeinfo(row, e) {
        this.contractChangeReason = e
        let dimensions_parts = row.dimensions.split(" ")
        let dimensions = row.equipmentSize.split(" ")

        dimensions_parts.forEach(part => {
          if (part.startsWith("长")) {
            row.dimensionsH = part.split("：")[1];
          } else if (part.startsWith("宽")) {
            row.dimensionsK = part.split("：")[1];
          }
        })
        dimensions.forEach(part => {
          if (part.startsWith("长")) {
            row.equipmentSizeH = part.split("：")[1];
          } else if (part.startsWith("宽")) {
            row.equipmentSizeK = part.split("：")[1];
          } else if (part.startsWith("高")) {
            row.equipmentSizeG = part.split("：")[1];
          }
        })
        this.detailsinfo = row
        this.onaction = '4'
      },
      async getAll() {
        // console.error({...this.pagequery})
        const res = await AdvertiserInfoGetAll(this.pagequery)
        this.tableData = res.data.content
        this.total = res.data.totalElements || 0
        let obj1 = {
          pageNumber: 0,
          pageSize: 10,
          advertisingSpaceType: '线上广告位'
        }
        AdvertiserInfoGetAll(obj1).then(res1 => {
          this.xsNum = res1.data.totalElements
        })
        let obj2 = {
          pageNumber: 0,
          pageSize: 10,
          advertisingSpaceType: '实体广告位'
        }
        AdvertiserInfoGetAll(obj2).then(res2 => {
          this.xxNum = res2.data.totalElements
        })
      },
      removeonaction(e) {
        console.error(e)
        this.onaction = e
        this.pagequery.pageNumber = 0
        this.getAll()
      },
      handleCurrentChange(val) {
        this.pagequery.pageNumber = val - 1
        this.getAll()
      },
      handleSizeChange(val) {
        this.pagequery.pageSize = val
      },
      // 查询按钮
      async onSubmit() {
        this.pagequery.pageNumber = 0
        this.getAll()
      },
      //重置按钮
      resetting() {
        this.pagequery = {
              pageNumber: 0,
              pageSize: 10,
              advertisingSpaceType: this.pagequery.advertisingSpaceType,
              id:'',
              affiliation:'',
              applicant:'',
          },
          this.getAll()
      },
      //删除
      closemsg(item) {
        const h = this.$createElement;
        this.$msgbox({
          title: '消息',
          message: h('p', null, [
            h('span', null, '是否删除 '),
          ]),
          showCancelButton: true,
          showClose: false,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          customClass: 'oe-dialog-btn',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              delList({
                id: item.id
              }).then(res => {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.getAll()
                done();
              })
            } else {
              done();
            }
          }
        })
      },
    }

  }
</script>

<style scoped>
  .zhuti {
    padding: 0 20px 20px 20px;
    min-height: calc(100vh - 50px - 20px);
    background-color: #Fff;

  }
  .chengeXia {
    border-bottom: 6px solid #3F9B6A;
    padding-bottom: 4px;
    color: #3F9B6A;
  }


  .formSearch {
    display: flex;
    width: 100%;
    font-size: 14px;
    justify-content: space-between;
  }

  .bg-purple-dark {
    background: #99a9bf;
  }

  .bg-purple {
    background: #d3dce6;
  }

  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }


</style>
<style lang="scss" scoped>
  ::v-deep .el-dialog__body {
    padding: 0 0 !important;
  }


  .el-dialog__header{
    padding: 0;
    display: none;
  }
</style>
