<template>
  <div class="app-container">
    <div class="top-bar">
      <SearchBar :config="searchConfig" @search="search" @reset="reset" />
      <el-button
        type="primary"
        @click.native="handleOperation({ type: 'add' })"
        icon="el-icon-s-promotion"
      >
        发布</el-button
      >
      <!-- <el-button type="primary" @click.native="handleCopy">复制</el-button> -->
    </div>
    <TablePage
      :data="list.records"
      :tableTitle="tableTitle"
      :operates="tableOperates"
    >
      <template v-slot:status="data">
        <el-popconfirm
          :title="getStatusTitle(data.scope.status)"
          @confirm="handleChangeStatus(data.scope.status)"
        >
          <el-switch
            slot="reference"
            :value="Boolean(Number(data.scope.status))"
          ></el-switch>
        </el-popconfirm>
      </template>
      <template v-slot:displayType="data">
        {{ dicts.display_type[data.scope.type] }}
      </template>
      <template v-slot:faceImageUrl="data">
        <img
          :src="
            $getFullUrl(data.scope.faceImagePressUrl || data.scope.faceImageUrl)
          "
          alt="查看大图"
          v-if="
            $getFullUrl(data.scope.faceImagePressUrl || data.scope.faceImageUrl)
          "
          style="cursor: pointer"
          width="100px"
          @click="handelPreviewImages(data.scope.faceImageUrl)"
        />
      </template>
      <template v-slot:themeType="data">
        {{ themeTypeCode(data.scope.themeType) }}
      </template>
      <template v-slot:operates="scope">
        <TableOperation
          :operations="tableOperations"
          :rawData="scope.scope.row"
          @handleOperation="handleOperation"
        ></TableOperation>
      </template>
    </TablePage>
    <el-pagination
      style="margin: 16px 0"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Number(list.current)"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="Number(list.size)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Number(list.total)"
      class="pagination"
    >
    </el-pagination>
    <InfoEditDialog
      :visible="editDialogVisible"
      :form="form"
      @handleClose="handleClose"
      @refresh="loadData"
    />
    <PreviewDialog
      v-if="Object.keys(curPreviewObj).length > 0"
      :visible="previewDialogVisible"
      :displayDetail="curPreviewObj"
      @handleClose="handleClosePreviewDialog"
    />
    <CopyDialog
      :visible="copyDialogVisible"
      @handleClose="handleCloseCopyDialog"
      @handleCopySelect="handleCopySelect"
      :list="list"
    />
    <el-image-viewer
      v-if="imgViewerVisible"
      :on-close="closeImgViewer"
      :url-list="imgList"
    />
  </div>
</template>

<script>
import TablePage from "@/components/Table/TablePage.vue";
import TableOperation from "@/components/Table/TableOperation.vue";
import { title, operates, operations } from "./config";
import { getList, deleteDisplay, getDisplayById } from "@/api/display";
import InfoEditDialog from "./components/InfoEditDialog";
import PreviewDialog from "./components/PreviewDialog";
import CopyDialog from "./components/CopyDialog";
import SearchBar from "@/components/SearchBar";
import { mapGetters } from "vuex";
import { themeTypeCode } from "./contants";
export default {
  components: {
    TablePage,
    TableOperation,
    InfoEditDialog,
    PreviewDialog,
    SearchBar,
    CopyDialog,
    "el-image-viewer": () =>
      import("element-ui/packages/image/src/image-viewer"),
  },
  data() {
    return {
      list: {
        record: [],
        size: 10,
        current: 1,
        total: 0,
      },
      searchForm: {
        name: "",
        status: "",
      },
      searchConfig: [
        {
          prop: "title",
          type: "input",
          label: "展览名称",
        },
        {
          prop: "status",
          type: "select",
          label: "状态",
          selectOptions: [
            {
              label: "已上线",
              value: "1",
            },
            {
              label: "已下线",
              value: "0",
            },
          ],
        },
      ],
      editDialogVisible: false,
      isAdd: true,
      form: {
        title: "", //标题
        type: "", //	类别(待定)--枚举值（社会、生活等）
        displayCharacter: "1", //展览性质(精品展2、布展1、文物展3)--此处填写布展类别
        keyword: "", //	关键词
        deptId: "", //展览单位id--暂填入用户自己的单位
        regionCode: "", //	所在地域--暂填入用户自己的地区
        intro: "", //展览介绍,
        themeType: "1", //模板主题--前端枚举
        literature: "", //关联文献。id1,id2,id3--接口查询
        remark: "", //备注
        status: false, //上下架状态(0-下架，1-上架）
        faceImage: "", //	封面（图片1张)
        images: "", //展览图片
        videos: "", //展览视频
        audios: "", //展览音频
        exhibitionUnits: [], //布展单元
      },
      loading: false,
      imgViewerVisible: false,
      previewDialogVisible: false,
      copyDialogVisible: false,
      previewVideos: [],
      displayTypes: {},
      curPreviewObj: {}, //当前预览的对象
    };
  },
  computed: {
    ...mapGetters(["dicts"]),
    tableTitle() {
      return title;
    },
    tableOperates() {
      return operates;
    },
    tableOperations() {
      return operations;
    },
    getStatusTitle(status) {
      return (status) => {
        if (Number(status)) {
          return "是否确定要下架？";
        } else {
          return "是否确定要上架？";
        }
      };
    },
  },

  async created() {
    await this.$store.dispatch("dict/getDictList", ["display_type"]);
    this.loadData();
  },
  methods: {
    async search(form) {
      var params = {
        page: this.list.current,
        limit: this.list.size,
        ...form,
      };
      if (params.status == "") {
        delete params.status;
      }
      let res = await getList(params);
      if (res.code == 0) {
        this.list = res.data;
      }
    },
    reset() {
      this.loadData();
      console.log("reset");
    },

    // 加载表格数据
    async loadData() {
      var params = {
        page: this.list.current,
        limit: this.list.size,
      };
      let res = await getList(params);
      if (res.code == 0) {
        console.log(res.data);
        this.list = res.data;
      }
    },
    async handleOperation(value, row) {
      console.log(value, row);
      switch (value.type) {
        case "add":
          this.editDialogVisible = true;
        case "view":
          if (row) {
            this.previewDialogVisible = true;
            let res = await getDisplayById({ exhibitionId: row.exhibitionId });
            this.curPreviewObj = res.data;
          }
          break;
        case "edit":
          let editRes = await getDisplayById({
            exhibitionId: row.exhibitionId,
          });
          this.form = editRes.data;
          this.editDialogVisible = true;
          break;
        case "delete":
          let deleteRes = await deleteDisplay([row.exhibitionId]);
          if (deleteRes.code === 0) {
            this.loadData();
            this.$message.success("删除成功！");
          }
          break;
      }
    },
    handleChangeStatus(status) {
      console.log("status", status);
    },
    // 多选
    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!");
    },
    // 关闭Dialog
    handleClose() {
      this.editDialogVisible = false;
      this.form = {
        status: 0,
      };
    },
    // 关闭预览图片
    closeImgViewer() {
      this.imgViewerVisible = false;
    },
    // 预览图片
    handelPreviewImages(images) {
      this.imgViewerVisible = true;
      if (images.length > 1) {
        this.imgList = images.split(",");
      } else {
        this.imgList = [images];
      }
    },

    handleClosePreviewDialog() {
      this.previewDialogVisible = false;
    },

    handleCopy() {
      this.copyDialogVisible = true;
    },
    handleCloseCopyDialog() {
      this.copyDialogVisible = false;
    },
    async handleCopySelect(value) {
      const { exhibitionId } = value;
      let res = await getDisplayById({ exhibitionId });
      delete res.data.exhibitionId;
      this.form = { ...res.data };
      this.editDialogVisible = true;
      console.log("this.form", this.form);
    },

    themeTypeCode(code) {
      return themeTypeCode[code];
    },
  },
};
</script>

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