<template>
  <div>
    <el-button type="text" icon="el-icon-setting" @click="drawer = true">设 置</el-button>
    <el-drawer
      title="设置宣传图"
      :visible.sync="drawer"
      direction="rtl"
      size="424px">
      <div style="padding:20px">
        <uploadImg btntype="limage" :folderid="folderid" accept=".JPG,.PNG" :bindData="{ownertable:'sys_ad',ownerid:sys_adspaceid,usetype:'default'}" :hidemediastock="true" @onSuccess="onSuccess"></uploadImg>
        <div v-for="(item,index) in banner" :key="index" class="banner_item">
          <img style="width:100%" :src="item.attinfos[0]?item.attinfos[0].url:''" alt="">
          <!-- <previewImage :image="item.attinfos[0]?item.attinfos[0]:''"></previewImage> -->
          <div class="model">
            <i class="el-icon-edit" style="font-size:36px;" @click="onEdit(item,index)"></i>
            <i class="el-icon-delete" style="font-size:36px" @click="deleteData(item,index)"></i>
          </div>
        </div>
      </div>
    </el-drawer>
    <el-dialog title="图片设置" :visible.sync="dialogFormVisible" width="500px" :before-close="closeDialog">
      <el-form label-width="100px" label-position	="left" size="small" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="链接地址" prop="hyperlink">
          <el-input v-model="form.hyperlink" autocomplete="off" placeholder="输入链接地址" clearable></el-input>
        </el-form-item>
        <el-form-item label="授权应用">
          <el-popover
            placement="top"
            trigger="manual"
            v-model="showPopover">
            <el-table :data="applist">
              <el-table-column property="systemappname" label="应用名称"></el-table-column>
              <el-table-column width="90" property="systemappname" label="操作">
                <template slot-scope="scope">
                  <el-button type="text" @click="selectRowApp(scope.row,()=>{showPopover = false})">选 择</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div style="margin-top:16px;text-align:right">
              <el-pagination
                background
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-size="param.content.pageSize"
                layout="total, prev, pager, next, jumper"
                :total="total">
              </el-pagination>
            </div>
            <el-input slot="reference" v-model="appinfo.systemappname" autocomplete="off" placeholder="选择应用" @focus="showPopover = true" @keyup.native.enter="selectApp()" clearable></el-input>
          </el-popover>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="insertormodify_bannermag('ruleForm')" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import previewImage from '@/components/previewImage/index.vue'

import uploadImg from '@/components/upload/hw_obs_upload.vue'
export default {
  props:['sys_adspaceid','banner'],
  components:{
    uploadImg,
    previewImage
  },
  data () {
    return {
      rules: {
        hyperlink: [
          { required: true, message: '输入链接地址', trigger: 'blur' },
        ]
      },
      drawer:false,
      dialogFormVisible:false,
      showPopover:false,
      form:{},
      date: function () {
        let date = (new Date()).getTime()
        return date
      },
      selectBanner:{},
      param:{
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
            "pageNumber": 1,
            "pageSize": 5,
            "typename": "appselect",
            "parameter": {
            }
        }
      },
      appinfo:{
        systemappname:''
      },
      applist:[],
      total:0,
      currentPage:null,
      folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
    }
  },
  methods:{
    async onSuccess(res){
      const bindata = await this.create()
      this.bindFileData(bindata,res)
    },
    // 创建一条banner记录
    create () {
      return new Promise((reslove,reject)=>{
        this.$api.requested({
         "classname": "webmanage.adspace.adspace",
          "method": "insertormodify_ad",
          "content": {
            "sys_adspaceid": this.sys_adspaceid,
            "sys_adid": 0,
            "hyperlink":"",
            "isused": 1
          }
        }).then(res=>{
          reslove(res.data)
        })
      })
    },
    // 绑定附件信息
    async bindFileData(bindata,fileData) {
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": 'sys_ad',
          "ownerid": bindata.sys_adid,
          "usetype": "default",
          "attachmentids": [
            fileData.data.attachmentids[0]
          ]
        }
      })
      res.code === 1?this.$emit('onSuccess'):""
    },
    deleteData (item,index) {
       this.$api.requested({
        "classname": "webmanage.adspace.adspace",
        "method": "delete_ad",
        "content": {
            "sys_adspaceid": this.sys_adspaceid,
            "sys_adid": item.sys_adid
        }
      }).then(res=>{
        this.tool.showMessage(res,(e=>{
          this.$emit('onSuccess')
          this.banner.splice(index,1)
        }))
      })
    },
    onEdit (item,index) {
      this.form = {
        "sys_adspaceid": item.sys_adspaceid,
        "sys_adid": item.sys_adid,
        "hyperlink":item.hyperlink,
        "isused": item.isused
      }
      this.appinfo = {
        systemappid: item.systemappid,
        systemappname: item.systemappname
      }
      this.selectBanner = item
      this.dialogFormVisible = true
      this.drawer = false
    },
    insertormodify_bannermag (formName) {
      let param = {
        "classname": "webmanage.adspace.adspace",
        "method": "insertormodify_ad",
        "content": {
            "sys_adspaceid": this.form.sys_adspaceid,
            "sys_adid": this.form.sys_adid,
            "hyperlink":this.form.hyperlink,
            "isused": this.form.isused,
            "systemappid":this.appinfo.systemappid
        }
      }
      this.$refs[formName].validate((valid) => {
        if (!valid) return false;
        this.$api.requested(param).then(res=>{
          this.tool.showMessage(res,(e=>{
            this.dialogFormVisible = false
            this.drawer = true
            this.$emit('onSuccess')
          }))
        })
      })
    },
    async selectApp () {
      this.param.content.parameter.condition = `%${this.appinfo.systemappname}%`
      const res = await this.$api.requested(this.param)
      this.applist = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.selectApp()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.selectApp()
    },
    selectRowApp (row,hidePopover) {
      this.appinfo = row
      hidePopover()
    },
    closeDialog () {
      this.dialogFormVisible =false
      this.showPopover = false
    }
  },
  mounted() {
    this.selectApp()
  }
}

</script>
<style>
</style>
<style scoped>
.banner_item{
  position: relative;
  min-height: 200px;
  /* width: 100%; */
  border: 1px solid #999;
  border-radius:5px;
  margin: 15px 0;
  overflow: hidden;
  cursor: pointer;
  
}
.model{
  
  position: absolute;
  display: none;
  align-items: center;
  justify-content: space-around;
  width:100%;
  height: 100%;
  top:0;
  left:0;
  color: #fff;
  background: rgba(0,0,0,0.5);
}
.banner_item:hover .model{
 display: flex;
}
</style>