<template>
  <section>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="请输入活码ID/名称、创建人" style="width: 300px" @change="searchList" v-model="searchForm.search">
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.selectMemberType" style="width: 130px">
          <el-option label="成员所属门店" :value="1"></el-option>
          <el-option label="使用成员" :value="2"></el-option>
        </el-select>
        <el-select v-model="searchForm.searchInfo" filterable remote :placeholder="`${selectMemberType == 1 ? '门店名称' : '成员姓名'} / code`" :remote-method="remoteMethod" :loading="selectLoading" style="margin-left: -4px" @change="searchList">
          <el-option v-for="item in memberOrShopList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.searchStatus" placeholder="全部状态" clearable class="w160" @change="searchList">
          <el-option :key="0" label="正常" :value="0"> </el-option>
          <el-option :key="1" label="待生效" :value="1"> </el-option>
          <el-option :key="1" label="已废弃" :value="2"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.codeType" placeholder="全部类型" clearable class="w160" @change="searchList">
          <el-option :key="0" label="单人活码" :value="0"> </el-option>
          <el-option :key="1" label="多人活码" :value="1"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="searchForm.dateTime" @change="searchList" :picker-options="pickerOptions" :editable="false" :value-format="'yyyy-MM-dd'" type="datetimerange" align="right" unlink-panels range-separator="至" start-placeholder="创建开始日期" end-placeholder="创建结束日期"> </el-date-picker>
      </el-form-item>
      <el-form-item class="fr">
        <el-button>批量分组</el-button>
        <el-button class="ghost-btn ">批量创建活码</el-button>
        <el-button type="primary">新增活码</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="actCodeTableRef" v-loading="loadingStatus" :data="actCodeList" tooltip-effect="dark" class="act-code-table" @sort-change="sortChange" :max-height="tableMaxHeight">
      <el-table-column type="selection" width="60" fixed> </el-table-column>
      <el-table-column width="60" class-name="table-select-cell" fixed>
        <template #header>
          <dm-table-select-page class="table-select" :type="selectPage.type" :data-ready="selectPage.dataReady" :table-vm="$refs.actCodeTableRef" :table-data="actCodeList" :total="totalCount" @change="t => (selectPage.type = t)" @selection-change="handleSelectionChange"></dm-table-select-page>
        </template>
      </el-table-column>
      <el-table-column label="活码ID" fixed width="110">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="活码信息" width="100">
        <template slot-scope="{ row }">
          <p class="overflow-ellipsis" :title="row.actCodeId">{{ row.actCodeId }}</p>
          <p><span class="oneActCode" v-if="row.actCodeType == 1">单人活码</span><span class="moreActCOde" v-else>多人活码</span></p>
        </template>
      </el-table-column>
      <el-table-column label="活码状态" width="130">
        <template slot-scope="{ row }">
          <p><span class="dot normal" v-if="row.actCodeStatus == 1">正常</span><span class="dot needSure" v-if="row.actCodeStatus == 2">待生效</span><span class="dot voided" v-if="row.actCodeStatus == 3">已作废</span></p>
          <p class="toTopTip" v-if="row.actCodeStatus == 1">(今日已达上限)</p>
        </template>
      </el-table-column>
      <el-table-column label="添加好友人数" sortable="custom" width="150">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="今日新增人数" sortable="custom" width="150">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="使用成员" width="120">
        <template slot-scope="{ row }">
          <p>{{ row.useMember[0].name }}</p>
          <p class="ft-12 phone">15784243524</p>
          <p>
            <el-popover width="200" trigger="click">
              <ul class="useMember-ul">
                <li v-for="(item, index) in row.useMember.slice(1)" :key="index">
                  <p>{{ item.name }}</p>
                  <p class="ft-12 phone">15784243524</p>
                </li>
              </ul>
              <el-button slot="reference" type="text" class="ft-12">查看更多</el-button>
            </el-popover>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="成员所属门店" width="120">
        <template slot-scope="{ row }">
          <p>{{ row.useMember[0].name }}</p>
          <p class="ft-12 phone">15784243524</p>
          <p>
            <el-popover width="200" trigger="click">
              <ul class="useMember-ul">
                <li v-for="(item, index) in row.useMember.slice(1)" :key="index">
                  <p>{{ item.name }}</p>
                  <p class="ft-12 phone">15784243524</p>
                </li>
              </ul>
              <el-button slot="reference" type="text" class="ft-12">查看更多</el-button>
            </el-popover>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="所属分组" width="100">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="100">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" sortable="custom" width="120">
        <template slot-scope="{ row }">
          <p>{{ row.actCodeId }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" fixed="right">
        <template slot-scope="{ row }">
          <dm-dropdown :scope-data="row" :configs="operateBtnArr" @command="onCommand" />
        </template>
      </el-table-column>
    </el-table>
    <div class="block common-wrap__page text-right m-t-24" v-if="actCodeList.length != 0">
      <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParam.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageParam.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"> </dm-pagination>
    </div>
  </section>
</template>

<script>
import { actCodeListApi } from '@/api/actCode.js';
// import errMsg from '@/common/js/error';
export default {
  name: 'actCodeTable',
  data() {
    let tableMaxHeight = document.body.clientHeight - 312 + 'px';
    return {
      tableMaxHeight: tableMaxHeight,
      searchForm: {
        search: '',
        searchInfo: '',
        searchStatus: '',
        selectMemberType: 1,
        codeType: '',
        dateTime: ''
      },
      tableData: [],
      total: 1,
      totalCount: 100,
      selectPage: {
        type: 0,
        dataReady: false
      },
      actCodeList: [
        {
          actCodeId: 12313213111111111,
          actCodeType: 1,
          actCodeStatus: 1,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 123132132222222222222,
          actCodeType: 1,
          actCodeStatus: 2,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 12313213,
          actCodeStatus: 3,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 12313213,
          actCodeType: 1,
          actCodeStatus: 1,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 12313213,
          actCodeStatus: 2,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 12313213,
          actCodeStatus: 2,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        },
        {
          actCodeId: 12313213,
          actCodeStatus: 2,
          useMember: [
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            },
            {
              name: 'text'
            }
          ]
        }
      ],
      loadingStatus: false,
      pickerOptions: {
        disabledDate: time => {
          const end = new Date();
          return time.getTime() < end.getTime() - 3600 * 1000 * 24 * 365 || time.getTime() > end.getTime() - 3600 * 1000 * 24;
        }
      },
      operateBtnArr: [
        {
          text: '数据',
          visible: true,
          handler: row => {
            console.log(row);
          }
        },
        {
          text: '详情',
          visible: true,
          handler: row => {
            console.log(row);
          }
        },
        {
          text: '下载活码',
          visible: true,
          handler: row => {
            console.log(row);
          }
        },
        {
          text: '作废',
          visible: true,
          handler: row => {
            console.log(row);
          }
        },
        {
          text: '编辑',
          visible: true,
          handler: row => {
            console.log(row);
          }
        }
      ],
      selectLoading: false,
      memberOrShopList: [
        {
          value: 1,
          label: '测试1'
        },
        {
          value: 2,
          label: '测试2'
        }
      ],
      // 分页参数
      pageParam: {
        pageSize: 20,
        currentPage: 1,
        sortColName: '',
        sortType: ''
      },
      multipleSelection: []
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.pageParam.currentPage = val;
      sessionStorage.setItem('currentPage', val);
      // this.getAjaxMembers();
    },
    // 页码
    handleSizeChange(val) {
      this.pageParam.pageSize = val;
      this.getTableList();
      // this.getAjaxMembers();
    },
    remoteMethod(query) {
      console.log(query);
    },
    // 排序
    sortChange(column) {
      this.pageParam.sortColName = column.prop;
      if (column.order === 'descending') {
        this.pageParam.sortType = 'desc';
      }
      if (column.order === 'ascending') {
        this.pageParam.sortType = 'asc';
      }
      this.getTableList();
    },
    searchList() {
      this.currentPage = 1;
      this.getTableList();
    },
    getTableList() {
      this.loadingStatus = true;
      actCodeListApi(Object.assign({}, this.searchForm, this.pageParam))
        .then(res => {
          this.totalCount = res.data.result.totalCount;
          if (Array.isArray(res.data.result.result) && res.data.result.result.length) {
            this.tableData = [].concat(res.data.result.result);
          } else {
            this.tableData = [];
          }
        })
        .finally(_ => {
          this.loadingStatus = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
.w160 {
  width: 160px;
}
.act-code-table {
  width: 100%;
  max-height: calc(100vh - 312px);
}
.act-code-table /deep/ .table-select-cell .cell {
  padding: 0;
}
.act-code-table /deep/ .table-select-cell .cell .el-dropdown {
  padding: 0;
}
.table-select {
  display: block;
  line-height: 1;
}
.act-code-table /deep/ td:not(.table-header__dropdown) .cell:empty::before {
  content: '' !important;
}
.oneActCode {
  background: #cff2f5;
  border-radius: 2px;
  color: #0faab6;
  font-size: 12px;
}
.moreActCOde {
  background: #dee3fc;
  border-radius: 2px;
  color: #2f54eb;
  font-size: 12px;
}
.toTopTip {
  color: #fa8c16;
  font-size: 12px;
}
.dot {
  display: inline-block;
  position: relative;
  margin-left: 14px;
  &::before {
    content: '';
    position: absolute;
    top: 50%;
    left: -14px;
    transform: translateY(-50%);
    display: block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
  }
}

.voided::before {
  background: #d9d9d9;
}
.normal::before {
  background: #33af4a;
}
.needSure::before {
  background: #fa8c16;
}
.ft-12 {
  font-size: 12px;
}
.phone {
  color: #606266;
}
.useMember-ul {
  max-height: 230px;
  overflow: auto;
  li {
    height: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-bottom: 1px solid #dcdfe6;
  }
}
</style>
