<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="跳转链接" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入跳转链接"
          clearable
          @input="handleQuery"
          @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-has-permi="['bst:ad:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-has-permi="['bst:ad:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="广告所属" align="center" prop="belong">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ad_belong" :value="scope.row.belong"/>
        </template>
      </el-table-column>
      <el-table-column label="所属运营区" align="center" prop="areaName">
        <template slot-scope="scope">
          <span>{{ scope.row.areaName || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="广告图片" align="center" prop="picture" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="跳转类型" align="center" prop="urlType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ad_url_type" :value="scope.row.urlType"/>
        </template>
      </el-table-column>
      <el-table-column label="跳转链接" align="center" prop="url" />
      <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-has-permi="['bst:ad:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-has-permi="['bst:ad: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" label-width="80px" v-loading="detailLoading">
        <el-form-item label="运营区" prop="areaId" >
            <area-remote-select
              v-model="form.areaId"
              :init-options="initAreaOptions"
              v-if="!detailLoading"
              placeholder="请选择运营区"
              style="width: 58%"
            />
        </el-form-item>
        <el-form-item label="广告所属" prop="belong" v-if="isSysAdmin()">
          <el-select v-model="form.belong" placeholder="请选择广告所属">
            <el-option
              v-for="dict in dict.type.ad_belong"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="广告图片" prop="picture">
          <image-upload v-model="form.picture" :limit="1"/>
        </el-form-item>
        <el-form-item label="跳转类型" prop="urlType">
          <el-select v-model="form.urlType" placeholder="请选择跳转类型">
            <el-option
              v-for="dict in dict.type.ad_url_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listAd, getAd, delAd, addAd, updateAd } from "@/api/bst/ad";
import { $showColumns } from '@/utils/mixins';
import FormCol from "@/components/FormCol/index.vue";
import AreaRemoteSelect from "@/components/Business/Area/AreaRemoteSelect.vue";
import {isSysAdmin} from "@/utils/permission";

  // 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "Ad",
  dicts:['ad_type','ad_url_type','ad_belong'],
  props:{
    id: {
      type: [String, Number],
      default: null
    },
  },
  mixins: [$showColumns],
  components: {AreaRemoteSelect, FormCol},
  data() {
    return {
      span: 24,
      // 字段列表
      columns: [
        {key: 'adId', visible: true, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'type', visible: true, label: '广告类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'areaName', visible: true, label: '所属运营区', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'picture', visible: true, label: '广告图片', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'url', visible: true, label: '跳转链接', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'remark', visible: true, label: '备注', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        //{key: 'deleted', visible: true, label: '逻辑删除标志，0否，1是', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        //{key: 'urlType', visible: true, label: '跳转类型，1外链跳转，2站内跳转', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告表格数据
      adList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        type: null,
        picture: null,
        url: null,
        deleted: null,
        urlType: null,
        belong: null
      },
      // 表单参数
      form: {},
      detailLoading: false,
    };
  },
  computed:{
    initAreaOptions() {
      return [{id: this.form.areaId, name: this.form.areaName}]
    },
  },
  created() {
    this.getList();
  },
  methods: {
    isSysAdmin,
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询广告列表 */
    getList() {
      this.loading = true;
      listAd(this.queryParams).then(response => {
        this.adList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        adId: null,
        areaId: null,
        type: null,
        picture: null,
        url: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null,
        urlType: 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.adId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const adId = row.adId || this.ids
      this.detailLoading = true;
      getAd(adId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广告";
      }).finally(() => {
        this.detailLoading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.adId != null) {
            updateAd(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const adIds = row.adId || this.ids;
      this.$modal.confirm('是否确认删除广告编号为"' + adIds + '"的数据项？').then(function() {
        return delAd(adIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bst/ad/export', {
        ...this.queryParams
      }, `ad_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
