<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="模板" prop="templateId">
        <el-select v-model="queryParams.templateId" @change="accountChange" placeholder="请选择模板">
          <el-option
            v-for="item in templateList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="媒体类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择媒体类型" clearable size="small">
          <el-option
            v-for="item in typeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="账号ID" prop="accountId">-->
<!--        <el-input-->
<!--          v-model="queryParams.accountId"-->
<!--          placeholder="请输入账号ID"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @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>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ad:adSite:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ad:adSite:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ad:adSite:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['ad:adSite:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="adSiteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="获客连接" align="center" prop="linkName" />
      <el-table-column label="媒体类型" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag prop="type" v-for="(item, index) in typeOptions" v-if="scope.row.type==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="accountName" />
      <el-table-column label="链接" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="复制链接" placement="top-start">
            <el-link type="primary" @click="copyUrl(scope.row.url)">{{scope.row.url}}</el-link>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ad:adSite:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['ad:adSite:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改站点管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="模板" prop="templateId">
          <el-select v-model="form.templateId" placeholder="请选择模板">
            <el-option
              v-for="item in templateList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="媒体类型" prop="type">
          <el-select v-model="form.type" @change="accountChange" placeholder="请选择媒体类型">
            <el-option
              v-for="item in typeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="Number(item.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="accountId" v-if="form.type != 99">
          <el-select v-model="form.accountId" filterable v-loading="accountLoading">
            <el-option v-for="item in accountList" :value="item.id" :key="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="域名" prop="domainId">
          <el-select v-model="form.domainId" filterable>
            <el-option v-for="item in domainList" :value="item.id" :key="item.id" :label="item.domain" />
          </el-select>
        </el-form-item>
        <el-form-item label="获客链接" prop="workId">
          <el-select v-model="form.workId" filterable>
            <el-option v-for="item in workList" :value="item.id" :key="item.id" :label="item.linkName" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </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 { listAdSite, getAdSite, delAdSite, addAdSite, updateAdSite, exportAdSite } from "@/api/ad/adSite";
import {listAll as templateListAll} from "@/api/ad/htmlTemplate";
import {listAll as domainListAll} from "@/api/ad/adDomain";
import {listAll as accountListAll} from "@/api/ad/adAccount";
import {listAll as workUrlListAll} from '@/api/qw/workLink'

export default {
  name: "AdSite",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      accountLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点管理表格数据
      adSiteList: [],
      accountList: [],
      accountAllList: [],
      typeOptions: [],
      templateList: [],
      domainList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateId: null,
        type: null,
        accountId: null,
        url: null,
      },
      // 表单参数
      form: {},
      workList: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getDicts("ad_type").then((response) => {
      this.typeOptions = response.data;
    });
    templateListAll().then(e => {
      this.templateList = e.data;
    })
    domainListAll().then(e => {
      this.domainList = e.data;
    })
    accountListAll().then(e => {
      this.accountAllList = e.data;
    })
    workUrlListAll().then(e => {
      this.workList = e.data;
    })
    this.getList();
  },
  methods: {
    /** 查询站点管理列表 */
    getList() {
      this.loading = true;
      listAdSite(this.queryParams).then(response => {
        this.adSiteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        templateId: null,
        type: null,
        accountId: null,
        url: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 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
    },
    copyUrl(url){
      navigator.clipboard.writeText(url)
        .then(() => {
          this.msgSuccess("复制成功");
        })
        .catch(err => {
          this.msgError("复制失败");
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAdSite(id).then(response => {
        this.form = response.data;
        this.accountList = this.accountAllList.filter(item => item.type == this.form.type);
        this.open = true;
        this.title = "修改站点管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAdSite(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdSite(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除站点管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAdSite(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有站点管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAdSite(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },

    accountChange(){
      let type = this.form.type;
      this.form.accountId = undefined;
      this.accountList = this.accountAllList.filter(item => item.type == type);
    },
  }
};
</script>
