<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="68px"
        >
          <el-form-item label="商户名称" prop="posName">
            <el-input
              v-model="queryParams.posName"
              placeholder="请输入商户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="联系人" prop="linkMan">
            <el-input
              v-model="queryParams.linkMan"
              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-button
              type="warning"
              icon="el-icon-document-copy"
              size="mini"
              @click="handleMultiplefp"
              :disabled="multiple"
              >批量分配</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="shanghuList"
        @selection-change="handleSelectionChange"
        :height="tableHeigth"
        ref="multipleTable"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商户名称" align="center" prop="posName" />
        <el-table-column label="联系人" align="center" prop="linkMan" />
        <el-table-column label="手机号" align="center" prop="phoneNumber" />
        <el-table-column label="城市" align="center" prop="city" />
      </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 { listShanghu } from "@/api/netEwm/shanghu";
import { batchAllocation } from "@/api/netEwm/poster";
export default {
  data() {
    return {
      //分配弹出层
      fpShow: false,
      loading: false,
      shanghuList: [],
      total: 0,
      tableHeigth: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        posName: null,
        linkMan: null,
        city: null,
      },
      // 非多个禁用
      multiple: true,
      ids: [],
      rowData: {},
    };
  },
  methods: {
    openDialog(row) {
      this.ids = row.shanghuIdList;
      this.rowData = row;
      console.log(this.rowData);
      this.getHeigth();
      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;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    //批量分配广告
    handleMultiplefp() {
      let data = {
        posterId: this.rowData.id,
        shanghuIds: this.ids,
      };

      batchAllocation(data).then((res) => {
        this.$modal.msgSuccess("分配成功");
      });
    },
    /** 查询商户列表 */
    getList() {
      this.loading = true;
      listShanghu(this.queryParams).then((response) => {
        this.shanghuList = response.rows;
        this.shanghuList.forEach((item) => {
          this.ids.forEach((it) => {
            if (item.id == it) {
              this.$nextTick(() => {
                this.$refs.multipleTable.toggleRowSelection(item, true);
              });
            }
          });
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    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>
