<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="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入网站名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="linkStatus">
        <el-select
          v-model="queryParams.linkStatus"
          clearable
          placeholder="状态"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.blog_link_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="网站地址" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </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="['blog:link: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="['blog:link: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="['blog:link:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['blog:link:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="linkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="" align="center" prop="id"/>
      <el-table-column label="网站名称" align="center" prop="title"/>
      <el-table-column label="网站图标" align="center" prop="headerImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.headerImage" :width="30" :height="30"/>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="描述" align="center" prop="description"/>
      <el-table-column :show-overflow-tooltip="true" label="网站地址" align="center" prop="url">
        <template slot-scope="scope">
          <a target="_blank" :href="scope.row.url">{{ scope.row.url }}</a>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="邮箱" align="center" prop="email"/>
      <el-table-column label="状态" align="center" prop="linkStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.blog_link_status" :value="scope.row.linkStatus"/>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="访问次数" align="center" prop="visitCount">
        <template slot-scope="scope">
          <el-badge type="primary" :value="scope.row.visitCount"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="申请时间" prop="createTime"
                       sortable="custom" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" 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="['blog:link:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['blog:link: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" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="网站名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入网站名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="网站图标" prop="headerImage">
              <div style="display:inline-block" @click="openHeaderChange">
                <img v-if="form.headerImage" alt="头像" class="header-img-box"
                     :src="(form.headerImage && form.headerImage.slice(0, 4) !== 'http')?'path'+form.headerImage:form.headerImage">
                <div v-else class="header-img-box">从媒体库选择</div>
              </div>
            </el-form-item>
          </el-col>

          <el-col :span="14">
            <el-form-item label="描述" prop="description">
              <el-input type="textarea" rows="3" v-model="form.description" placeholder="请输入描述"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="链接" prop="url">
              <el-input v-model="form.url" placeholder="请输入链接"/>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="邮箱地址" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="状态" prop="link">
              <el-select
                v-model="form.linkStatus"
                default-first-option
                placeholder="请输入状态"
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.blog_link_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="发送邮件" prop="sendEmail">
              <el-switch
                v-model="form.sendEmail"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="邮件内容">
              <editor v-model="form.emailContent" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </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>
    <ChooseImg hidden v-on:onChooseImg="onChooseImg" ref="chooseImg"></ChooseImg>
  </div>
</template>

<script>
import {addLink, delLink, getLink, listLink, updateLink} from "@/api/blog/link";
import ChooseImg from "@/components/ChooseImg";

export default {
  dicts: ['blog_link_status'],
  components: {ChooseImg},
  name: "Link",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      linkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "createTime",
        isAsc: "desc",
        content: null,
        email: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onChooseImg(value) {
      this.form.headerImage = value;
    },
    openHeaderChange() {
      this.$refs.chooseImg.openDrawer();
    },
    getList() {
      this.loading = true;
      listLink(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.linkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        username: null,
        headerImage: null,
        linkStatus: null,
        url: null,
        emailContent: "",
        sendEmail: true,
        email: 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
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLink(id).then(response => {
        this.form = response.data;
        this.form.linkStatus = response.data.linkStatus.toString();
        this.form.sendEmail = true;
        if (!this.form.emailContent) {
          this.form.emailContent="&lt;p&gt;尊敬的站长，您好，&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;您在&lt;a href=\"www.bianxf.com\" rel=\"noopener noreferrer\" target=\"_blank\"&gt;&lt;strong&gt;DimpleBlog&lt;/strong&gt;&lt;/a&gt;提交的友链申请已通过！&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=\"color: rgb(0, 0, 0);\"&gt;（本邮件为系统自动发出请勿回复）&lt;/span&gt;&lt;/p&gt;"
        }
        this.form.emailContent = this.unEscapeSpecialCharacters(this.form.emailContent);
        this.open = true;
        this.title = "修改友链";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var commitForm = JSON.parse(JSON.stringify(this.form));
          commitForm.emailContent = this.escapeSpecialCharacters(commitForm.emailContent);
          if (commitForm.id != null) {
            updateLink(commitForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLink(commitForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加友链";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除友链编号为"' + ids + '"的数据项？').then(function () {
        return delLink(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/link/export', {
        ...this.queryParams
      }, `link_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
