<template>
  <div class="display">
    <!-- <NavBar /> -->
    <div class="content-wrapper">
      <div class="content">
        <el-row :gutter="40">
          <el-col :span="24">
            <div class="title wow animate__animated animate__fadeInLeft">
              博物馆
            </div>
            <div
              class="bottom-line wow animate__animated animate__fadeInLeft"
            ></div>
            <div class="search wow animate__animated animate__fadeIn">
              <el-input
                class="input item"
                suffix-icon="el-icon-search"
                v-model="keyword"
                placeholder="关键词"
                @keyup.enter.native="search"
                clearable
              >
              </el-input>
              <el-cascader
                class="years item"
                style="width: 100%"
                v-model="regionCode"
                :options="regionTree"
                :props="culturalRegionProps"
                placeholder="请选择所属地区"
                filterable
                @change="handleRegionChange"
                clearable
              >
              </el-cascader>

              <div class="search-button" @click="search">搜 索</div>
            </div>
            <el-row :gutter="40" class="cr-list">
              <el-col
                :span="8"
                class="cr-item"
                @click.native="handleClick(item)"
                v-for="(item, index) in list.records"
                :key="index"
              >
                <div class="container wow animate__animated animate__fadeInUp">
                  <div class="img">
                    <img
                      :src="
                        $getFullUrl(item.faceImagePressUrl || item.faceImageUrl)
                      "
                      width="100%"
                      class="img"
                      lazy
                    />
                  </div>
                  <div class="desc">
                    <span class="name">{{ item.name }}</span>
                  </div>
                </div>
              </el-col>
            </el-row>
            <el-empty
              description="暂无数据"
              v-if="list.records.length == 0"
            ></el-empty>
            <div class="pagination">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="Number(list.current)"
                :page-sizes="[20, 40, 100]"
                :page-size="Number(list.size)"
                layout="total, prev, pager, next, jumper"
                :total="Number(list.total)"
                class="pagination"
              >
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import SearchBar from "@/components/SearchBar";
// import NavBar from "@/components/NavBar";
// import Footer from "@/components/Footer";
import { getMuseumListPage } from "@/api/org";
import { mapGetters } from "vuex";
import { debounce } from "@/utils/index";
export default {
  name: "CulturalRelic",
  // components: { SearchBar, NavBar, Footer },
  components: { SearchBar },
  data() {
    return {
      list: {
        records: [],
        size: 20,
        current: 1,
        total: 0,
      },
      culturalRegionProps: {
        value: "code",
        label: "name",
        children: "children",
        checkStrictly: true, //单选选择任意一级选项
      },
      culturalRelicYears: [],
      onlyShow3d: false,
      keyword: "",
      type: "",
      regionCode: [],
      regionTree: [],
    };
  },
  computed: {
    ...mapGetters(["dicts"]),
  },
  async created() {
    await this.$store.dispatch("dict/getDictList", ["display_type"]);
    let res = await this.$store.dispatch("dict/getDictTree", [
      "culturalRelicYears",
    ]);
    this.culturalRelicYears = res.culturalRelicYears;
  },
  mounted() {
    this.loadData();
    this.loadRegionTree();
  },
  watch: {
    keyword(value) {
      debounce(this.handleKeyWordChange(value), 1000); //500ms
    },
  },
  methods: {
    search() {
      this.loadData();
    },

    async loadData() {
      const params = {
        page: this.list.current,
        limit: this.list.size,
        name: this.keyword,
        regionCode: this.regionCode[this.regionCode.length - 1],
      };
      let res = await getMuseumListPage(params);
      if (res.code == 0) {
        this.list = res.data;
      }
    },
    // 改变页容量
    handleSizeChange(value) {
      this.list.size = value;
      this.loadData();
    },

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

    defImg(e) {
      e.target.src = require("@/assets/404_images/no-pic.png");
    },
    handleClick(item) {
      this.$router.push({
        path: "museum/" + item.id,
      });
    },

    handleTypeChange(value) {
      this.type = value;
      this.loadData();
    },
    handleKeyWordChange(value) {
      this.keyword = value;
      this.loadData();
    },

    handleRegionChange(value) {
      console.log(value);
      console.log(value[value.length - 1]);
      this.regionCode = value;
      this.loadData();
    },

    loadRegionTree() {
      let parentId = "";
      this.$store.dispatch("org/getSysRegionTreeData", parentId).then((res) => {
        this.regionTree = res;
        loopTree(this.regionTree);
        function loopTree(arr) {
          if (arr && arr.length > 0) {
            arr.forEach((item) => {
              if (item.children && item.children.length == 0) {
                delete item.children;
              } else {
                loopTree(item.children);
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
$blue: #2069c4;
$text-indent: 16px;
.display {
  width: 100%;
  // background-color: #2069c4;
  // color: #fff;
  .content-wrapper {
    padding-top: 60px;
    background-color: #fff;
    .title {
      font-family: KaiTi;
      font-size: 44px;
      margin-bottom: 16px;
    }
    .bottom-line {
      width: 50px;
      background-color: #b8b8b8;
      height: 2px;
      margin-bottom: 40px;
    }
    .search {
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: rgba($blue, 0.2);
      padding: 20px 40px;
      margin-bottom: 40px;

      .item {
        margin-right: 20px;
      }
      .threeD {
        display: flex;
        width: 420px;
        cursor: pointer;
        .box {
          width: 36px;
          height: 36px;
          display: flex;
          justify-content: center;
          align-items: center;
          border: 2px solid #563f17;
          margin-right: 6px;
          i {
            font-size: 28px;
          }
        }
        .text {
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
      .search-button {
        cursor: pointer;
        width: 420px;
        height: 48px;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: $blue;
        color: #fff;
      }
    }
    .content {
      width: 100%;
      .cr-list {
        .cr-item {
          margin-bottom: 40px;
          .container {
            border: 1px solid #f1f1f1;
            height: 300px;
            position: relative;
            display: flex;
            flex-direction: column;
            cursor: pointer;
            &:hover {
              img {
                transform: scale(1.2);
              }
              .desc {
                background-color: $blue;
                color: #fff;
              }
            }
            .img {
              background-color: #f8f8f8;
              height: 240px;
              cursor: pointer;
              transition: all 0.5s ease;
              overflow: hidden;
              img {
                height: 100%;
                object-fit: contain;
                // width: auto;
              }
            }
            .desc {
              padding: 16px 0;
              display: flex;
              justify-content: center;
              transition: all 0.5s ease;
              flex: 1;
            }
          }
        }
      }
    }
  }

  .pagination {
    margin: 24px;
    display: flex;
    justify-content: center;
  }
}

::v-deep .el-cascader .el-input .el-icon-arrow-down,
::v-deep .el-select .el-input .el-select__caret {
  color: $blue;
  font-weight: bold;
}

::v-deep .el-input {
  .el-input__inner {
    border: none;
    border-radius: 0;
  }
}
</style>