<template>
  <div class="app-container">
    <div class="top-bar">
      <SearchBar :config="searchConfig" @search="search" @reset="reset" />
      <PermissionButton
        button
        perms="bizCcProducts: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:images="{ scope }">
        <img
          :src="$getFullUrl(scope.imagesVo[0].pressUrl)"
          alt="暂无图片"
          v-if="scope.imagesVo.length > 0"
          style="
            cursor: pointer;
            height: 100px;
            width: 100px;
            object-fit: contain;
          "
          width="100px"
          @click="handelPreviewImages($getFullUrl(scope.imagesVo[0].pressUrl))"
        />
      </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 { getCcProduct, deleteCcProduct, updateCcProduct } from "@/api/product";
import InfoEditDialog from "./components/InfoEditDialog";

export default {
  components: {
    InfoEditDialog,
  },
  data() {
    return {
      list: {
        records: [],
        size: 10,
        current: 1,
        total: 0,
      },
      searchConfig: [
        {
          prop: "title",
          type: "input",
          label: "文创名称",
        },
        {
          prop: "status",
          type: "select",
          label: "状态",
          selectOptions: [
            {
              label: "已上架",
              value: "1",
            },
            {
              label: "已下架",
              value: "0",
            },
          ],
        },
      ],
      drawerVisible: false,
      form: {
        title: "", //名称
        deptId: "", //所属博物馆
        regionCode: "", //所属地code
        status: true, //上下架状态
        images: "", //封面
        intro: "", //简介
      },
      loading: false,
      orgTreeData: [],
      imgViewerVisible: false,
      imgList: [],
      searchParams: {}, //记录SearchForm中的搜索条件
    };
  },
  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.searchParams = form;
      this.resetPage();
      this.loadData(form);
    },

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

    // 清除分页到初始状态
    resetPage() {
      this.list.current = 1;
    },

    //   加载表格数据
    async loadData(form) {
      this.loading = true;
      var params = {
        page: this.list.current,
        limit: this.list.size,
      };
      const queryParams = form || this.searchParams;
      if (Object.keys(queryParams)) {
        params = { ...params, ...queryParams };
      }
      let res = await getCcProduct(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 deleteCcProduct([row.bccpId]);
          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) {
      const callback = this.loadData;
      this.$bizCommon.changeStatus(row, updateCcProduct, callback);
    },
    // 多选
    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>
