<template>
  <!-- 配方广告 -->
  <el-dialog
    title="分配广告"
    :visible.sync="fpShow"
    width="1200px"
    append-to-body
  >
    <div class="fp-table app-container" ref="appMain">
      <div ref="search">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="广告名称" prop="posterName">
            <el-input
              v-model="queryParams.posterName"
              placeholder="请输入广告名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="posterList" :height="tableHeigth">
        <el-table-column label="广告名称" align="center" prop="posterName" />
        <el-table-column label="有效起始时间" align="center" prop="startTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="有效结束时间" align="center" prop="endTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="点击预览广告"
          align="center"
          prop="posterName"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-link type="primary" :href="baseUrl + scope.row.videoPath">{{
              scope.row.posterName
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              :disabled="shanghuPosterId == scope.row.id"
              type="primary"
              size="small"
              @click="handleFp(scope.row)"
              >分配</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div ref="pagination">
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { listPoster, batchAllocation } from "@/api/netEwm/poster";
export default {
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      loading: false,
      posterList: [],
      total: 0,
      fpShow: false,
      tableHeigth: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        posterName: null,
        startTime: null,
        endTime: null,
      },
      fpData: {},
      shanghuPosterId: null,
    };
  },
  methods: {
    openDialog(data, posterId) {
      this.shanghuPosterId = posterId;
      this.fpData = data;
      this.getList();
    },
    /**适配高度 */
    getHeigth() {
      this.fpShow = true;
      let _this = this;
      this.$nextTick(() => {
        let total = this.$refs.appMain.clientHeight;
        let search = this.$refs.search.clientHeight;
        _this.tableHeigth = total - search - 40;
      });
    },
    /** 查询广告列表 */
    getList() {
      this.getHeigth();
      this.loading = true;
      listPoster(this.queryParams).then((response) => {
        this.posterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleFp(row) {
      let _this = this;
      this.fpData.posterId = row.id;
      this.$modal
        .confirm("您确定要将该广告分配给当前商户吗？")
        .then(function () {
          return batchAllocation(_this.fpData);
        })
        .then(() => {
          _this.shanghuPosterId = row.id;
          _this.getList();
          _this.$modal.msgSuccess("分配成功");
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 0 !important;
}
.fp-table {
  height: 550px;
}
</style>
