<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="广告商名称" prop="advertiserName">
        <el-input
          v-model="queryParams.advertiserName"
          placeholder="请输入广告商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="custom">
        <el-select v-model="queryParams.custom" placeholder="请选择类型" clearable size="small">
          <el-option label="线上广告商" :value="1" />
          <el-option label="自定义广告商" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择状态" clearable size="small">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="advertiserList">
      <el-table-column label="广告商名称" align="left" prop="advertiserName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enabled"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
            style="margin-right: 10px; vertical-align: middle;"
          >
          </el-switch>
          <span>{{ scope.row.advertiserName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="custom">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.custom === 1" type="success">线上广告商</el-tag>
          <el-tag v-else type="danger">自定义广告商</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否支持API" align="center" prop="supportApi">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.supportApi === 1" type="success">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否支持回传" align="center" prop="supportCallback">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.supportCallback === 1" type="success">是</el-tag>
          <el-tag v-else type="info">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="enabled">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enabled === 1" type="success">启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改广告商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="广告商名称" prop="advertiserName">
          <el-input v-model="form.advertiserName" placeholder="请输入广告商名称" />
        </el-form-item>
        <el-form-item label="类型" prop="custom">
          <el-select v-model="form.custom" placeholder="请选择类型" style="width: 100%" disabled>
            <el-option label="自定义广告商" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="enabled">
          <el-radio-group v-model="form.enabled">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageAdvertiser, getAdvertiser, addAdvertiser, updateAdvertiser, delAdvertiser, batchDelAdvertiser, enableAdvertiser } from "@/api/adv/advertiser";

export default {
  name: "Advertiser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告商表格数据
      advertiserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        advertiserName: undefined,
        custom: undefined,
        enabled: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        advertiserName: [
          { required: true, message: "广告商名称不能为空", trigger: "blur" }
        ],
        custom: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        enabled: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询广告商列表 */
    getList() {
      this.loading = true;
      pageAdvertiser(this.queryParams).then(response => {
        this.advertiserList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        advertiserName: undefined,
        custom: 2,
        enabled: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0];
      getAdvertiser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广告商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAdvertiser(this.form.id, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAdvertiser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 启用/停用状态切换 */
    handleStatusChange(row) {
      const statusText = row.enabled === 1 ? '启用' : '停用';
      this.$confirm(`确认要${statusText}广告商“${row.advertiserName}”吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return enableAdvertiser(row.id);
      }).then(() => {
        this.msgSuccess(`${statusText}成功`);
        this.getList();
      }).catch(() => {
        // 取消操作，恢复状态
        row.enabled = row.enabled === 1 ? 0 : 1;
      });
    }
  }
};
</script>

