<!-- 模板区域 -->
<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="companyName" label="公司名称" width="180">
      </el-table-column>
      <el-table-column align="center" prop="companyDesc" label="公司描述" width="180">
      </el-table-column>
      <el-table-column align="center" prop="advertisingProbability" label="广告权重" width="180">
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间">
      </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="MarketList"
    />
    <el-dialog title="提示" :visible.sync="MarketdialogVisible" width="30%">
      <el-form label-width="80px" :model="marketform">
        <el-form-item label="公司名称">
          <el-input v-model="marketform.companyName" placeholder="公司名称"></el-input>
        </el-form-item>
        <el-form-item label="公司描述">
          <el-input type="textarea" placeholder="请输入公司描述，最多不超过200字" v-model="marketform.companyDesc" maxlength="200"
            show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="广告权重">
          <el-input v-model="marketform.advertisingProbability" 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";

export default {
  // 数据存放区域
  data() {
    return {
      loading: false,
      MarketdialogVisible: false,
      tableData: [],
      marketform: {
        companyName: "",//公司名称
        companyDesc: "",//描述
        id: "",
      },
      pagetotal:0,
      pageRequest: {
        pageNum: 1,
        pageSize: 20,
      },
      dialogType: "add"
    }
  },
  // 生命周期函数
  created() {
    this.MarketList();
  },
  // 方法函数
  methods: {
    Marketupdate(row) {
      this.dialogType = 'edit';
      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.MarketList();
          this.$message({
            type: 'success',
            message: result.msg,
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    marketAdd() {
      if (this.dialogType == 'add') {
        adAgencyApi.companyAdd(this.marketform).then((result) => {
          if (result.code == '200') {
            this.MarketList();
            this.MarketdialogVisible = false
            this.$message({
              type: 'success',
              message: result.msg,
            });
          } else {
            this.$message({
              type: 'error',
              message: result.msg,
            });
          }
        })
      } else {
        adAgencyApi.companyUpdate(this.marketform).then((result) => {
          if (result.code == '200') {
            this.MarketList();
            this.MarketdialogVisible = false;
            this.dialogType = 'add';
            this.$message({
              type: 'success',
              message: result.msg,
            });
          } else {
            this.$message({
              type: 'error',
              message: result.msg,
            });
          }
        })
      }
    },
    MarketList() {
      this.loading = true;
      adAgencyApi.companyList(this.pageRequest).then((result) => {
        this.tableData = result.data.records;
        this.pagetotal=result.data.total;
        this.loading = false;
      })
    }
  },
  // 计算属性
  computed: {
  }
}
</script>
<!-- 样式区域 -->
<style lang='scss' scoped></style>

