<!-- 模板区域 -->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
          @click="MarketdialogVisible = true">新增广告</el-button>
      </el-col>
    </el-row>
    <!-- 模板内容 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column align="center" prop="videoTitle" label="广告标题" width="180">
      </el-table-column>
      <el-table-column align="center" prop="videoDesc" label="广告描述" width="180">
      </el-table-column>
      <el-table-column align="center" prop="videoUrl" label="广告" width="180">
      </el-table-column>
      <el-table-column align="center" prop="videoCover" label="广告封面">
        <template slot-scope="scope">
          <img class="table-img" v-show="scope.row.videoCover" :src="baseUrl + scope.row.videoCover" alt="广告封面">
        </template>
      </el-table-column>
      <el-table-column align="center" prop="videoCompany" label="广告归属公司" width="180">
      </el-table-column>
      <el-table-column align="center" prop="videoTimeLength" label="广告时长" width="180">
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="Marketupdate(scope.row)">编辑</el-button>
          <el-button @click="Marketdel(scope.row)" type="text" size="small" style="color: red;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="pagetotal" :page.sync="pageRequest.pageNum" :limit.sync="pageRequest.pageSize"
      @pagination="marketquery" />
    <el-dialog :title="dialogaTitle" :visible.sync="MarketdialogVisible" width="30%">
      <el-form label-width="100px" :model="marketform">
        <el-form-item label="广告标题">
          <el-input v-model="marketform.videoTitle" placeholder="广告标题"></el-input>
        </el-form-item>
        <el-form-item label="广告描述">
          <el-input type="textarea" placeholder="请输入广告描述，最多不超过50字" v-model="marketform.videoDesc" maxlength="50"
            show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="封面上传">
          <ImageUpload :limit="1" :uploadType="'videoCover'" @input="getVideoCoverUrl"></ImageUpload>
        </el-form-item>
        <el-form-item label="广告上传">
          <ImageUpload :limit="1" :uploadType="'videoUrl'" @input="getVideoUrlUrl"></ImageUpload>
        </el-form-item>
        <el-form-item label="广告归属公司">
          <el-select v-model="marketform.videoCompany" placeholder="请选择">
            <el-option v-for="item in companyList" :key="item.value" :label="item.companyName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告奖品">
          <el-select v-model="marketform.awardIds" placeholder="请选择" multiple>
            <el-option v-for="item in awardList" :key="item.value" :label="item.awardName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告时长">
          <el-input v-model="marketform.videoTimeLength" type="number" placeholder="广告时长"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="MarketdialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="marketAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<!-- 行为区域 -->
<script>
import adAgencyApi from "@/api/advertisement/adAgency";
import imageUpload from "@/components/ImageUpload/index";
export default {
  components: {
    imageUpload
  },
  // 数据存放区域
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_URL + process.env.VUE_APP_BASE_API + '/',
      companyList: [],
      loading: false,
      MarketdialogVisible: false,
      tableData: [],
      marketform: {
        videoTitle: "",//标题
        videoCompany: "",//归属公司
        videoDesc: "",//广告描述
        videoJumpUrl: "",
        videoTimeLength: 0,//广告时长
        videoUrl: "",//广告地址
        videoCover: "",//封面地址
        awardIds:[],
      },
      pageRequest: {
        pageNum: 1,
        pageSize: 20,
      },
      awardList:[],
      dialogType: "add",
      dialogaTitle: "新增",
      pagetotal: 0
    }
  },
  // 生命周期函数
  created() {
    this.company();
    this.marketquery();
    this.MarketAward();
  },
  // 方法函数
  methods: {
    MarketAward() {
      adAgencyApi.marketAwardList(this.pageRequest).then((result) => {
        this.awardList = result.data.records;
      })
    },
    getVideoCoverUrl(data) {
      this.marketform.videoCover = data;
    },
    getVideoUrlUrl(data) {
      this.marketform.videoUrl = data;
    },
    Marketupdate(row) {
      this.dialogType = 'edit';
      this.dialogaTitle = ' 编辑'
      let obj = Object.assign({}, row)
      this.marketform = obj;
      this.MarketdialogVisible = true;
    },
    Marketdel(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        adAgencyApi.companyDelete({ id: row.id }).then((result) => {
          this.$message({
            type: 'success',
            message: result.msg,
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    marketAdd() {
      if (this.dialogaType == 'add') {
        adAgencyApi.marketAdd(this.marketform).then((result) => {
          if (result.code == '200') {
            this.marketquery();
            this.MarketdialogVisible = false
            this.$message({
              type: 'success',
              message: result.msg,
            });
          } else {
            this.$message({
              type: 'error',
              message: result.msg,
            });
          }
        })
      } else {
        adAgencyApi.marketUpdate(this.marketform).then((result) => {
          if (result.code == '200') {
            this.marketquery();
            this.MarketdialogVisible = false
            this.$message({
              type: 'success',
              message: result.msg,
            });
          } else {
            this.$message({
              type: 'error',
              message: result.msg,
            });
          }
        })
      }
    },
    marketquery() {
      adAgencyApi.marketList(this.pageRequest).then((result) => {
        this.tableData = result.data.records;
        this.pagetotal = result.data.total;
        this.loading = false;
      })
    },
    company() {
      adAgencyApi.companyList(this.pageRequest).then((result) => {
        this.companyList = result.data.records;
      })
    }
  },
  // 计算属性
  computed: {
  }
}
</script>
<!-- 样式区域 -->
<style lang='scss' scoped></style>

