<template>
  <div class="app-container">
    <div class="top-bar">
      <SearchBar :config="searchConfig" @search="search" @reset="reset" />
      <PermissionButton
        button
        perms="bizVirtual:add"
        size="mini"
        type="primary"
        @click.native="handleOperation({ type: 'add' })"
        icon="el-icon-plus"
      >
        添加</PermissionButton
      >
    </div>
    <TablePage
      :data="list.records"
      :tableTitle="tableTitle"
      :operates="tableOperates"
      v-loading="loading"
    >
      <template v-slot:faceImageUrl="{ scope }">
        <img
          :src="scope.faceImageUrl"
          alt="暂无图片"
          v-if="scope.faceImageUrl"
          style="cursor: pointer"
          width="100px"
          @click="handelPreviewImages(scope.faceImageUrl)"
        />
      </template>
      <template v-slot:status="{ scope }">
        <el-popconfirm
          :title="getStatusTitle(scope.status)"
          @confirm="handleChangeStatus(scope)"
        >
          <el-switch
            slot="reference"
            :value="Boolean(Number(scope.status))"
          ></el-switch>
        </el-popconfirm>
      </template>

      <template v-slot:operates="{ scope }">
        <TableOperation
          :operations="tableOperations"
          :rawData="scope"
          @handleOperation="handleOperation"
        >
        </TableOperation>
      </template>
    </TablePage>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Number(list.current)"
      :page-sizes="[10, 20, 40, 50]"
      :page-size="Number(list.size)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Number(list.total)"
      class="pagination"
    >
    </el-pagination>
    <InfoEditDialog
      :visible="drawerVisible"
      :form="form"
      :orgTreeData="orgTreeData"
      @handleClose="handleClose"
      @refresh="loadData"
    />
    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imgList"
    />
  </div>
</template>

<script>
import { title, operates, operations } from "./config";
import { deleteVirtual, editVirtual, getVirtualListPer } from "@/api/vitual";
import InfoEditDialog from "./components/InfoEditDialog";

export default {
  components: {
    InfoEditDialog,
  },
  data() {
    return {
      list: {
        records: [],
        size: 10,
        current: 1,
        total: 0,
      },
      searchForm: {
        name: "",
        status: "",
      },
      searchConfig: [
        {
          prop: "name",
          type: "input",
          label: "虚拟展名称",
        },
        {
          prop: "status",
          type: "select",
          label: "状态",
          selectOptions: [
            {
              label: "已上架",
              value: "1",
            },
            {
              label: "已下架",
              value: "0",
            },
          ],
        },
      ],
      drawerVisible: false,
      form: {
        name: "", //名称
        deptId: "", //所属博物馆
        regionCode: "", //所属地code
        status: true, //上下架状态
        faceImage: "", //封面
        url: "", //链接
        intro: "", //简介
      },
      loading: false,
      orgTreeData: [],
      imgViewerVisible: false,
      imgList: [],
    };
  },
  computed: {
    tableTitle() {
      return title;
    },
    tableOperates() {
      return operates;
    },
    tableOperations() {
      return operations;
    },
    getStatusTitle(status) {
      return (status) => {
        if (Number(status)) {
          return "是否确定要下架？";
        } else {
          return "是否确定要上架？";
        }
      };
    },
  },
  mounted() {
    this.loadData();
    this.loadOrgTree();
  },
  methods: {
    async search(form) {
      this.loading = true;
      var params = {
        page: this.list.current,
        limit: this.list.size,
        ...form,
      };
      if (params.status == "") {
        delete params.status;
      }
      let res = await getVirtualListPer(params);
      if (res.code == 0) {
        this.list = res.data;
      }
      this.loading = false;
    },

    reset() {
      this.loadData();
    },

    //   加载表格数据
    async loadData() {
      this.loading = true;
      var params = {
        page: this.list.current,
        limit: this.list.size,
      };
      let res = await getVirtualListPer(params);
      if (res.code == 0) {
        this.list = res.data;
      }
      this.loading = false;
    },
    loadOrgTree() {
      this.$store.dispatch("org/getMuseumTreeData", true).then((res) => {
        // this.orgTreeData = res[0].children; //去掉文旅厅根节点
        this.orgTreeData = res; //去掉文旅厅根节点
      });
    },
    async handleOperation(value, row) {
      switch (value.type) {
        case "add":
          this.drawerVisible = true;
          break;
        case "edit":
          this.form = row;
          this.drawerVisible = true;
          break;
        case "delete":
          let deleteRes = await deleteVirtual([row.bvId]);
          if (deleteRes.code == 0) {
            this.$message.success("删除成功！");
            this.loadData();
          }
          break;
      }
    },
    // 关闭预览图片
    closeImgViewer() {
      this.imgViewerVisible = false;
    },
    // 预览图片
    handelPreviewImages(images) {
      this.imgViewerVisible = true;
      if (images.length > 1) {
        this.imgList = images.split(",");
      } else {
        this.imgList = [images];
      }
    },

    async handleChangeStatus(row) {
      console.log("status", row);
      const { status } = row;
      let newStatus = status === "0" ? "1" : "0";
      const params = {
        ...row,
        status: newStatus,
      };
      let res = await editVirtual(params);
      if (res.code == 0) {
        this.loadData();
        this.$message.success("修改成功！");
      }
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 改变页容量
    handleSizeChange(value) {
      this.list.size = value;
      this.loadData();
    },

    // 改变当前显示页
    handleCurrentChange(value) {
      this.list.current = value;
      this.loadData();
    },

    // 搜索
    onSubmitSearch() {
      console.log("submit!");
    },

    // 打开drawer
    handleOpenDialog(type, value) {
      this.drawerVisible = true;
      switch (type) {
        case "add":
          console.log("新增");
          break;
        case "edit":
          console.log("编辑", value);
          break;
      }
    },
    // 关闭Dialog
    handleClose() {
      this.drawerVisible = false;
      this.form = {
        status: true,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.pagination {
  margin: 16px;
}

::v-deep .el-card__header {
  padding: 10px 20px;
  font-weight: bold;
}

.deptName {
  padding: 12px 16px;
  font-size: 13px;
}

.theme {
  padding: 4px 16px;
  font-size: 13px;
}

.faceImage {
  padding: 16px;

  .img {
    position: relative;
    // width: 100%;
    width: 100%;
    height: 160px;

    &:hover {
      .modal {
        background-color: rgba(0, 0, 0, 0.3);

        i {
          display: inline;
        }

        .view-text {
          display: block;
        }
      }
    }

    img {
      width: 100%;
      height: 100%;
    }

    .modal {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      transition: all ease 0.3s;
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;

      i {
        font-size: 28px;
        color: #fff;
        display: none;
        transition: all ease 0.3s;
      }

      .view-text {
        color: #fff;
        font-size: 12px;
        display: none;
        transition: all ease 0.3s;
      }

      // display: none;
    }
  }
}

::v-deep .el-image {
  .image-slot {
    width: 100%;
    height: 100%;
  }
}

.buttons {
  .el-button {
    border-radius: 0;
  }
}
</style>
