<template>
  <div class="app-container">
    <div class="head-container">
      <el-input
        v-model="columnName"
        clearable
        placeholder="请输入落地页名称"
        style="width: 200px;margin-right: 10px"
      />
      <el-button size="mini" @click="getLists(1)">查询</el-button>
      <el-button type="success" size="mini" @click="addTag()"
        >创建落地页</el-button
      >
    </div>

    <el-table
      v-loading="loading"
      :data="data"
      size="small"
      style="width: 100%;"
    >
      <el-table-column label="序号" align="center" type="index" fixed />
      <el-table-column prop="name" label="落地页名称"> </el-table-column>
      <el-table-column prop="bgUrl" label="图片" width="100">
        <template #default="scope">
          <el-popover placement="right" :width="400" trigger="hover">
            <img :src="scope.row.bgUrl" width="375" height="300" />
            <template #reference>
              <img
                :src="scope.row.bgUrl"
                style="max-height: 60px; max-width: 60px"
              />
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="250">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="copyUrl($event, scope.row)"
            >复制链接</el-button
          >
          <el-button type="text" size="mini" @click="createQrCode(scope.row.id)"
            >预览</el-button
          >
          <el-button
            v-if="modifiable"
            type="text"
            size="mini"
            @click="addTag(scope.row.id, true)"
            >修改</el-button
          >
          <el-button type="text" size="mini" @click="update(scope.row, 0)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="margin-top: 50px;"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

    <el-dialog
      v-if="showQrCode"
      :visible.sync="showQrCode"
      append-to-body
      width="400px"
      title="落地页预览"
    >
      <QrCode ref="QrCode" :is-show="0" @closePopover="showQrCode = false" />
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
// import { queryTagList, tagSave, tagUpdate, updateStatus } from "@/api/tag.js";
import { getToken } from "@/utils/auth";
import { getPageList, updatePageStatus } from "@/api/page.js";
import { handleClipboard } from "@/views/liveBroadcast/manage/config.js";
import { urlResize } from "@/api/videoLive.js";
import QrCode from "@/components/QrCode";

export default {
  components: { QrCode },
  props: {},
  name: "page",
  data() {
    return {
      showQrCode: false,
      isUpdate: false,
      dialog: false,
      dialogTitle: "",
      loading: false,
      delLoading: false,
      editLoading: false,
      newTag: "",
      total: 0,
      page: 1,
      size: 10,
      data: [],
      columnName: "",
      form: {
        bgUrl: "",
        btnPosition: 0,
        btnStyle: "",
        btnUrl: "",
        name: "",
        paymentUrl: ""
      },
      adjustForm: {
        event: 103,
        reason: ""
      },
      isAdjust: false,
      headers: {
        Authorization: "Bearer " + getToken(),

        "X-File-Size": 0
      }
    };
  },
  computed: {
    ...mapGetters(["user"]),
    title() {
      const role = this.user && this.user.roles && this.user.roles[0];
      return role ? `${role}列表` : "";
    },
    modifiable() {
      return this.user ? this.user.user.deptId !== "1" : true;
    }
  },
  mounted() {
    this.getLists(1);
  },
  methods: {
    async createQrCode(id) {
      this.showQrCode = true;
      const ret = await urlResize({
        url: `/page?id=${id}&preview=1`
      });
      if (ret.code === 0) {
        this.$refs.QrCode.createCode(ret.data);
      }
    },
    async copyUrl(event, item) {
      const currentTarget = event.currentTarget;
      const ret = await urlResize({
        url: `/page?id=${item.id}&preview=1`
      });
      if (ret.code === 0) {
        handleClipboard(ret.data, event, currentTarget);
      }
    },
    async getLists(val) {
      if (val) {
        this.page = 1;
      }
      this.loading = true;
      const { data } = await getPageList({
        current: this.page,
        name: this.columnName,
        size: this.size,
        status: 1
      });
      this.data = data.list;
      this.total = data.total;
      this.loading = false;
    },
    // 添加标签
    async addTag(val) {
      this.$router.push(`/page/detail/${val}?isUpdate=true`);
    },
    cancel() {
      this.form = {
        advisorId: "",
        name: "",
        type: 1,
        remark: "",
        coverImage: "",
        authorityId: "",
        authority: "1",
        weight: null
      };
      this.adjustForm = {
        event: 103,
        reason: ""
      };
      this.dialog = false;
    },
    // 删除标签
    async update(item, status) {
      const eventMap = {
        1: "启用",
        0: "删除"
      };
      this.$confirm(`您确定${eventMap[status]}?`, {
        confirmButtonText: "提交",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const params = {
            id: item.id,
            status
          };
          const data = await updatePageStatus(params);
          // console.log(data)
          if (data.code === 0) {
            this.$message.success("操作成功");
            if (this.data.length === 1 && this.page !== 1) {
              this.page -= 1;
            }
            this.getLists();
          } else {
            this.$message.error(data.message);
          }
        })
        .catch(() => {});
    },
    // 分页器
    sizeChange(e) {
      this.page = 1;
      this.size = e;
      this.getLists();
    },
    pageChange(e) {
      this.page = e;
      this.getLists();
    },
    handlePictureCardPreview() {},
    handleRemove() {},
    handleSuccess(response, file, fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      setTimeout(() => {
        this.form.coverImage = response.data.url;
      }, 100);
      this.$notify({
        title: "封面上传成功",
        type: "success",
        duration: 2500
      });
    },
    // 监听上传失败
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message);
      this.$notify({
        title: msg.message,
        type: "error",
        duration: 2500
      });
    },
    beforeUpload(file) {
      if (file.type !== "image/jpeg" && file.type !== "image/png") {
        this.$message.error("上传只支持jpeg格式和png格式!");
        return false;
      }
      const isLt = file.size / 1024 < 1024;
      if (!isLt) {
        this.$message.error("上传文件大小不能超过 1MB!");
      }
      this.headers["X-File-Size"] = file.size;
      return isLt;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-pagination {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
