<!--  -->
<template>
  <div class="table" :key="$route.params.type">
    <el-table row-key="id" ref="table" :data="table.records" height="55vh" class="collection-con" style="width: 100%">
      
      <!-- <el-table-column label="排序" width="100" v-if="$route.name == 'collection'">
        <i style="cursor: pointer" class="el-icon-d-caret"></i>
      </el-table-column> -->
      
      <el-table-column v-for="(item, idx) in data" :key="idx" :prop="item.prop" :label="item.label" :width="item.prop === 'thumb' ? 300 : item.width || 'auto'">
        <template slot-scope="scope">
          <el-image
            v-if="item.prop === 'thumb'"
            style="min-width:180px;width:auto;height:220px;"
            :fit="'cover'"
            :src="scope.row[item.prop] || (item.default ? require('@/assets/img/defaultImg.jpg') : '')"
            :preview-src-list="[scope.row[item.prop] || (item.default ? require('@/assets/img/defaultImg.jpg') : '')]"
          >
          </el-image>

          <template v-else-if="item.prop === 'display'">
            <template>
              <el-switch
                @change="
                  (e) => {
                    handleSwitch(e, scope.row);
                  }
                "
                v-model="scope.row[item.prop]"
              >
              </el-switch>
            </template>
          </template>

          <template v-else-if="item.prop === 'isEnabled'">
              <span v-if="scope.row.roleKey && scope.row.roleKey != 'sys_visitor' && ($route.name == 'role' || $route.name == 'user')">-</span>
              <el-switch
                @change="
                  (e) => {
                    handleDisableSwitch(e, scope.row);
                  }
                "
                v-else
                v-model="scope.row[item.prop]"
              >
              </el-switch>
          </template>

          <template v-else-if="item.prop === 'record'">
            <slot :data="scope.row" name="record"></slot>
          </template>

          <template v-else-if="item.canEdit">
            <slot :name="item.prop" :data="scope.row"></slot>
          </template>

          <template v-else-if="item.prop == 'roleName'">
            <template v-if="scope.row[item.prop] == '游客'">
              <span>-</span>
            </template>
            <span v-else>{{ scope.row[item.prop] || item.default || "-" }}</span>
          </template>

          <span v-else>{{ scope.row[item.prop] || item.default || "-" }}</span>
        </template>
      </el-table-column>

      <slot name="operate" ></slot>
    </el-table>
    <div class="e-pagination">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="page.pageNum"
        @size-change="handleSizeChange"
        :page-size="page.size"
        :page-sizes="PAGESIZES"
        layout="total, sizes, prev, pager, next, jumper"
        :total="table.total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { handleDisplay, handleDisable,goodsSort } from "@/configue/api";
import { mapGetters } from "vuex";
export default {
  props: {
    data: {
      type: Array,
      default() {
        return [];
      },
    },
    page: {
      type: Object,
      default() {
        return {};
      },
    },
    type: String,
    tableData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  components: {},
  computed: {
    ...mapGetters({
      activeSub: "activeSub",
    }),
    table() {
      this.tableData.records &&
        this.tableData.records.forEach((item, i) => {
          item["idx"] = i + 1 + this.page.size * (this.page.pageNum - 1);
          item["display"] = Boolean(item["display"]);
          item["isEnabled"] = Boolean(item["isEnabled"]);
          item["sexStr"] = item["sex"] == 0 ? "男" : "女";
          item["typeName"] = item["type"] == 'temp' ? "临时展览" : "常设展览";
          console.log(this.type);
          if (this.type=='dict') {
            item["typeName"] = item["type"] == 'age' ? "藏品年代" : "藏品类型";
          }
          
        });
      return this.tableData;
    },
  },
  data() {
    return {
      sorab:'',
      role: window.localStorage.getItem("role") == "sys_admin",
    };
  },
  methods: {
    handleSwitch(val, item) {
      handleDisplay(
        this.type,
        {
          id: item.id,
          display: Number(val),
        },
        () => {
       
        }
      );
    },
    handleDisableSwitch(val, item) {
      handleDisable(
        this.$route.name,
        {
          id: item.id,
          isEnabled: Number(val),
        },
        () => {}
      );
    },
    handleCurrentChange(data) {
      this.$emit("pagechange", data);
    },
    handleSizeChange(data) {
      this.$emit("pagesizechange", data);
    },
    goodsSort(data){
      goodsSort({
        ...data
      },()=>{

      })
    }
  },
  mounted() {
  },
};
</script>

<style lang="less" scoped>
.table {
  .e-pagination {
    bottom: 10px;
  }
}
</style>
