<template>
  <div id="tableList">
    <div class="headerNavTool">
      <div class="leftIndexText">
        {{ navText.title }} <strong>{{ navText.index }}</strong>
        {{ navText.ch }}
      </div>
      <div class="rightBtnBox">
        <el-button
          size="medium"
          type="primary"
          @click="addClick"
          v-if="!navText.addHide"
          >添加</el-button
        >
        <el-button
          v-if="navText.backFatherBtn.status"
          size="medium"
          type="warning"
          @click="backFather"
          >{{ navText.backFatherBtn.title }}</el-button
        >
        <el-popover
          popper-class="slotPopper"
          placement="bottom-end"
          trigger="click"
        >
          <div class="popoverDis">
            <div class="checkboxHeader">
              <el-checkbox
                :indeterminate="isIndeterminate"
                v-model="checkAll"
                @change="handleCheckAllChange"
                >列展示</el-checkbox
              >
              <div class="initbtns" @click="initVue">重置</div>
            </div>

            <div
              style="height: 1px; width: 100%; background-color: #eaeefb"
            ></div>
            <el-checkbox-group
              class="checkboxGroup"
              v-model="checkedCities"
              @change="handleCheckedCitiesChange"
            >
              <div
                class="checkboxchild"
                v-for="(item, index) in cities"
                :key="index"
              >
                <el-checkbox
                  :label="item"
                  @change="checkboxChange(item, $event)"
                  >{{ item }}</el-checkbox
                >
                <div class="icon-right">
                  <i class="el-icon-upload2" @click="upMove(item, index)"></i>
                  <i
                    class="el-icon-download"
                    @click="downMove(item, index)"
                  ></i>
                </div>
              </div>
            </el-checkbox-group>
          </div>
          <el-button style="margin-left: 10px" size="medium" slot="reference"
            >自定义列</el-button
          >
        </el-popover>
      </div>
    </div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @select-all="selectAll"
      @select="select"
      :row-key="rowKey"
      :load="load"
      lazy
      ref="pagerset"
      v-loading="loading"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        v-if="navText.choice"
        align="center"
        type="selection"
        width="55"
        header-align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="navText.num"
        type="index"
        label="序号"
        align="center"
        header-align="center"
      >
      </el-table-column>
      <template v-for="(item, index) in tableSet">
        <el-table-column
          v-if="item.hidden"
          :width="item.width"
          :key="index"
          :label="item.label"
          align="center"
          :show-overflow-tooltip="true"
          header-align="center"
        >
          <template slot-scope="scope">
            <div class="imgboxsq" v-if="item.scope === 'img'">
              <el-image
                class="el_images"
                lazy
                :src="$methodsTools.splitImgHost(scope.row[item.prop])"
                style="height: 100%"
                :preview-src-list="scope.row['srcList']"
                alt="加载失败..."
              >
              </el-image>
            </div>
            <span v-else-if="item.scope === 'status'">{{
              Number(scope.row[item.prop]) === 1
                ? "启用"
                : Number(scope.row[item.prop]) === 0
                ? "关闭"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'reStatus'">{{
              Number(scope.row[item.prop]) === 1
                ? "定时发布"
                : Number(scope.row[item.prop]) === 0
                ? "手动发布"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'statusRe'">{{
              Number(scope.row[item.prop]) === 0
                ? "已删除"
                : Number(scope.row[item.prop]) === 1 &&
                  Number(scope.row["reStatus"]) === 1
                ? "待发布"
                : Number(scope.row[item.prop]) === 1 &&
                  Number(scope.row["reStatus"]) === 0
                ? "未发布"
                : Number(scope.row[item.prop]) === 2
                ? "已撤回"
                : Number(scope.row[item.prop]) === 3
                ? "已发布"
                : Number(scope.row[item.prop]) === 4
                ? "已到期"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'TimeVoBtn'">
              <el-popover placement="left" trigger="click">
                <div
                  v-for="(items, indexs) in scope.row[item.prop]"
                  :key="indexs"
                  style="margin-bottom:6px;"
                >
                  {{ $methodsTools.onlyForma(items.startTime) }} ~
                  {{ $methodsTools.onlyForma(items.endTime) }}
                </div>
                <el-button slot="reference" size="mini">查看</el-button>
              </el-popover>
            </span>
            <span v-else-if="item.scope === 'activityType'">{{
              Number(scope.row[item.prop]) === 0
                ? "优惠券"
                : Number(scope.row[item.prop]) === 1
                ? "折扣券"
                : Number(scope.row[item.prop]) === 2
                ? "线上直播"
                : Number(scope.row[item.prop]) === 3
                ? "定价"
                : Number(scope.row[item.prop]) === 4
                ? "优惠"
                : Number(scope.row[item.prop]) === 5
                ? "折扣"
                : Number(scope.row[item.prop]) === 6
                ? "线下活动"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'activityRemarks'">{{
              Number(scope.row[item.prop]) === 0
                ? "金额：￥" + scope.row["price"]
                : Number(scope.row[item.prop]) === 1
                ? "折扣：" + scope.row["price"] + "折"
                : Number(scope.row[item.prop]) === 2
                ? "直播内容：" + scope.row["liveContent"]
                : Number(scope.row[item.prop]) === 3
                ? "商品金额：￥" + scope.row["price"]
                : Number(scope.row[item.prop]) === 4
                ? "优惠金额：￥" + scope.row["price"]
                : Number(scope.row[item.prop]) === 5
                ? "商品折扣：" + scope.row["price"] + "折"
                : Number(scope.row[item.prop]) === 6
                ? "活动地点：" + scope.row["liveAdress"]
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'statusActivity'">{{
              Number(scope.row[item.prop]) === 0
                ? "已删除"
                : Number(scope.row[item.prop]) === 1
                ? "已结束"
                : Number(scope.row[item.prop]) === 2
                ? "已撤回"
                : Number(scope.row[item.prop]) === 3
                ? "已开始"
                : Number(scope.row[item.prop]) === 4
                ? "未开始"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'statusFB'">{{
              scope.row[item.prop] === 0
                ? "删除"
                : scope.row[item.prop] === 1
                ? "发布"
                : scope.row[item.prop] === 2
                ? "撤回"
                : scope.row[item.prop] === 3
                ? "未发布"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'typeStatus'">{{
              scope.row[item.prop] === 0
                ? "报名指引"
                : scope.row[item.prop] === 1
                ? "打印准考证"
                : scope.row[item.prop] === 2
                ? "成绩查询指引"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'typeId'">{{
              scope.row[item.prop] === 1
                ? "普通练习"
                : scope.row[item.prop] === 2
                ? "押题试卷"
                : scope.row[item.prop] === 3
                ? "真题试卷"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'taskType'">{{
              scope.row[item.prop] === 0
                ? "每日签到"
                : scope.row[item.prop] === 1
                ? "完善信息"
                : scope.row[item.prop] === 2
                ? "邀请好友"
                : scope.row[item.prop] === 3
                ? "每日分享"
                : scope.row[item.prop] === 4
                ? "视频学习"
                : scope.row[item.prop] === 5
                ? "模拟考试"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'releaseType'">{{
              scope.row[item.prop] === 0
                ? "手动发布"
                : scope.row[item.prop] === 1
                ? "定时发布"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'releaseStatus'">{{
              scope.row[item.prop] === 0
                ? "删除"
                : scope.row[item.prop] === 1
                ? "待发布"
                : scope.row[item.prop] === 2
                ? "撤回"
                : scope.row[item.prop] === 3
                ? "发布成功"
                : scope.row[item.prop] === 4
                ? "未发布"
                : scope.row[item.prop] === 5
                ? "已结束"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'numberAll'">{{
              scope.row[item.prop1] + scope.row[item.prop2]
            }}</span>
            <span v-else-if="item.scope === 'moreLists'">{{
              scope.row[item.prop1] + " ~ " + scope.row[item.prop2]
            }}</span>
            <span v-else-if="item.scope === 'TimeLists'"
              >{{ $methodsTools.onlyForma(scope.row[item.prop1]) }} ~
              {{ $methodsTools.onlyForma(scope.row[item.prop2]) }}</span
            >
            <span v-else-if="item.scope === 'aTimeList'">{{
              scope.row[item.prop] === null
                ? "--"
                : $methodsTools.onlyForma(scope.row[item.prop])
            }}</span>
            <span v-else-if="item.scope === 'topic'">{{
              scope.row[item.prop] === 1
                ? "单选题"
                : scope.row[item.prop] === 2
                ? "多选题"
                : scope.row[item.prop] === 3
                ? "判断题"
                : scope.row[item.prop] === 4
                ? "案例题"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'statusOrder'">{{
              scope.row[item.prop] === -2
                ? "超时关闭"
                : scope.row[item.prop] === -1
                ? "手动关闭"
                : scope.row[item.prop] === 0
                ? "待付款"
                : scope.row[item.prop] === 1
                ? "已付款"
                : scope.row[item.prop] === 3
                ? "成功"
                : "未知"
            }}</span>
            <span v-else-if="item.scope === 'cType'">
              <el-tag
                type="success"
                v-if="
                  compType(scope.row[item.prop], scope.row['requestMethod']) ===
                  '新增'
                "
              >
                {{
                  compType(scope.row[item.prop], scope.row["requestMethod"])
                }}</el-tag
              >
              <el-tag
                v-if="
                  compType(scope.row[item.prop], scope.row['requestMethod']) ===
                  '修改'
                "
              >
                {{
                  compType(scope.row[item.prop], scope.row["requestMethod"])
                }}</el-tag
              >
              <el-tag
                type="warning"
                v-if="
                  compType(scope.row[item.prop], scope.row['requestMethod']) ===
                  '查询'
                "
              >
                {{
                  compType(scope.row[item.prop], scope.row["requestMethod"])
                }}</el-tag
              >
            </span>
            <span v-else-if="item.scope === 'time'">{{
              numTime(scope.row[item.prop])
            }}</span>
            <span v-else-if="item.scope === 'timeBtn'">{{
              numTime(scope.row[item.prop])
            }}
              <el-button type="text" @click="getxq(scope.row)">详情</el-button>
            </span>
            <span v-else-if="item.scope === 'minute'">{{
              numTimeminute(scope.row[item.prop])
            }}</span>
            <span
              v-else-if="item.scope === 'jumpPage'"
              @click="jumpPage(scope.row)"
              style="color: #409eff; cursor: pointer"
              >{{ scope.row[item.prop] }}</span
            >
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="180px"
        v-if="!navText.tableHide"
      >
        <template slot-scope="scope">
          <slot name="btn" :scope="scope"></slot>
          <!-- <el-button type="text" @click="edit(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  props: ["tableSets", "tableData", "navText", "rowKey", "loading"],
  data() {
    return {
      tableSet: this.tableSets,
      allCheckData: [], //表格多选
      checkAll: true,
      checkedCities: [], //自定义列 选中数组
      cities: [], //自定义列 总数组
      isIndeterminate: false,
    };
  },
  computed: {
    numTime: function () {
      return function (res) {
        return Number(res / 3600).toFixed(2);
      };
    },
    numTimeminute: function () {
      return function (res) {
        return Number(res / 60).toFixed(2);
      };
    },
    compType: function () {
      return function (res, data) {
        if (data === "POST") {
          if (res.indexOf("edit") !== -1 || res.indexOf("update") !== -1) {
            return "修改";
          } else {
            return "新增";
          }
        } else if (data === "GET") {
          return "查询";
        }
      };
    },
  },
  created() {
    this.inittableSet = JSON.stringify(this.tableSet);
    this.initTR();
  },
  methods: {
    //初始化
    initTR() {
      this.cities = [];
      this.checkedCities = [];
      this.checkAll = true;
      this.isIndeterminate = false;
      this.tableSet.forEach((item, index) => {
        this.cities.push(item.label);
        if (item.hidden) {
          this.checkedCities.push(item.label);
        }
      });
      if (this.checkedCities.length === 0) {
        this.isIndeterminate = false;
      } else if (this.checkedCities.length === this.cities.length) {
        this.isIndeterminate = false;
        this.checkAll = true;
      } else {
        this.isIndeterminate = true;
        this.checkAll = false;
      }
    },
    //自定义列全选按钮触发
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.cities : [];
      this.isIndeterminate = false;
      if (val) {
        this.tableSet.forEach((item, index) => {
          item.hidden = true;
        });
      } else {
        this.tableSet.forEach((item, index) => {
          item.hidden = false;
        });
      }
    },
    // 勾选自定义列子选项
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
    checkboxChange(v, e) {
      this.tableSet.forEach((item) => {
        if (item.label === v) {
          item.hidden = e;
        }
      });
    },
    // 下移
    downMove(option, index) {
      if (index !== this.tableSet.length - 1) {
        this.tableSet[index] = this.tableSet.splice(
          index + 1,
          1,
          this.tableSet[index]
        )[0];
        this.cities[index] = this.cities.splice(
          index + 1,
          1,
          this.cities[index]
        )[0];
      } else {
        this.tableSet.unshift(this.tableSet.splice(index, 1)[0]);
        this.cities.unshift(this.cities.splice(index, 1)[0]);
      }
    },
    // 上移
    upMove(option, index) {
      if (index != 0) {
        this.tableSet[index] = this.tableSet.splice(
          index - 1,
          1,
          this.tableSet[index]
        )[0];
        this.cities[index] = this.cities.splice(
          index - 1,
          1,
          this.cities[index]
        )[0];
      } else {
        this.tableSet.push(this.tableSet.shift());
        this.cities.push(this.cities.shift());
      }
    },
    //跳转页面
    jumpPage(options) {
      this.$router.push({
        path: "/dictData",
        query: {
          dictId: options.dictId,
          dictType: options.dictType,
        },
      });
    },
    //新增按钮
    addClick() {
      this.$emit("addClick");
    },
    //将选中值传回调用组件
    backFather() {
      this.$emit("emitData", this.allCheckData);
    },
    edit() {},
    selectAll(value) {
      this.allCheckData = value;
    },
    select(value) {
      this.allCheckData = value;
    },
    //自定义列重置
    initVue() {
      // this.$emit("initTableset");
      this.tableSet = JSON.parse(this.inittableSet);
      this.initTR();
    },
    load(tree, treeNode, resolve) {
      this.$emit("load", tree, treeNode, resolve);
    },
    getxq(option){
      this.$emit("getxq",option.userId)
    },
  },
};
</script>
<style lang="less">
.slotPopper {
  padding: 0px !important;
}
</style>
<style lang="less" scoped>
#tableList {
  padding: 0px 16px 16px;
  border-radius: 8px;
  box-shadow: 0px 0px 9px 1px rgba(28, 41, 90, 0.1);
  background: #ffffff;
  .headerNavTool {
    height: 72px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .rightBtnBox {
      display: flex;
      align-items: center;
    }
  }
}
.popoverDis {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  font-size: 14px;
  .checkboxHeader {
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 13px;
    padding-right: 15px;
    width: 100%;
    .initbtns {
      color: #47a6ff;
      cursor: pointer;
    }
  }
  .checkboxGroup {
    display: flex;
    flex-direction: column;
    width: 100%;
    .checkboxchild {
      width: 100%;
      justify-content: space-between;
      height: 40px;
      display: flex;
      align-items: center;
      padding: 0px 15px 0px 13px;
      transition: all 0.4s;
      &:hover {
        background-color: #ecf5ff;
      }
      &:hover .icon-right {
        display: flex;
      }
      .icon-right {
        display: flex;
        align-items: center;
        width: 30px;
        height: 30px;
        color: #666;
        font-size: 14px;
        display: none;
        i {
          cursor: pointer;
          margin: 0px 3px;
          &:hover {
            color: #47a6ff;
          }
        }
      }
    }
  }
}
/deep/.el-checkbox__input.is-checked + .el-checkbox__label {
  color: #666;
}
.imgboxsq {
  margin: 0 auto;
  height: 60px;
  max-height: 60px;
  .el_images {
    /deep/.el-image__inner {
      width: auto;
    }
  }
}
</style>

