<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-17 14:33:14
 -->
<template>
  <div class="app-detail-wrap">
    <div class="border-box">
      <div class="list-body flex">
        <div class="list-left border-box">
          <org-tree v-if="gicData.length" :brandId="brandId" :departObj="departObj" :itemData="gicData" @getSelectData="getSelectData"></org-tree>
        </div>
        <div class="list-right p-20 border-box">
          <div class="list-right-body">
            <div class="table-top flex flex-space-between">
              <div>
                <el-select class="w-168" v-model="clerkId" placeholder="" @change="changeCondition"> <el-option v-for="item in clerkOptions" :key="item.clerkId" :label="item.clerkName" :value="item.clerkId"> </el-option> </el-select><el-input placeholder="请输入会员姓名/手机号/卡号" prefix-icon="el-icon-search" v-model="searchInput" class="w-264 m-l-10" style="width: 264px;" clearable @clear="clearSearch" @keyup.native="value => toInput(value, searchInput)"> </el-input>
              </div>
              <el-button type="primary" @click="toAssign">批量分配会员</el-button>
            </div>
            <div class="table-body  m-t-20">
              <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :style="{ width: '100%' }" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="35"> </el-table-column>
                <el-table-column width="25">
                  <template slot="header" slot-scope="scope">
                    <el-dropdown style="line-height: 10px; padding: 0; margin-left: -15px; transform: translateY(4px); -webkit-transform: translateY(4px);" @command="handleCommand" placement="bottom-start">
                      <span class="el-dropdown-link"><i class="iconfont icongengduo"></i> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="0"><span :style="{ color: selectRadio == 0 ? '#2f54eb' : '#606266' }">选择当页</span></el-dropdown-item>
                        <el-dropdown-item command="1"><span :style="{ color: selectRadio == 1 ? '#2f54eb' : '#606266' }">选择全部</span></el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </template>
                  <template slot-scope="scope"> </template>
                </el-table-column>
                <el-table-column label="会员姓名" width="120" show-overflow-tooltip>
                  <template slot-scope="scope">{{ scope.row.memberName || '--' }}</template>
                </el-table-column>
                <el-table-column prop="" label="会员等级">
                  <template slot-scope="scope">{{ scope.row.gradeName || '--' }}</template>
                </el-table-column>
                <el-table-column prop="" label="消费总额" show-overflow-tooltip>
                  <template slot-scope="scope">{{ scope.row.isRealTime || '--' }}</template>
                </el-table-column>
                <el-table-column prop="" label="最近消费时间" show-overflow-tooltip>
                  <template slot-scope="scope">
                    <div class="line-18">{{ scope.row.lastCostTime | timeStampToYmd }}</div>
                    <div class="line-18">{{ scope.row.lastCostTime | timeStampToHms }}</div>
                  </template>
                </el-table-column>
                <el-table-column prop="" label="生日" show-overflow-tooltip>
                  <template slot-scope="scope">{{ scope.row.birthday || '--' }}</template>
                </el-table-column>
                <el-table-column prop="" label="所属导购" show-overflow-tooltip>
                  <template slot-scope="scope">
                    {{ scope.row.clerkName || '--' }}
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button type="text" @click="toAssignMember(scope.$index, scope.row)">分配</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
                <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <customer-assign v-if="assignShow" :brandId="brandId" :clerkId="clerkId" :searchParams="searchInput" :chooseAll="selectRadio" :departObj="departObj" :multipleData="multipleSelection" @closeText="closeText" @submitText="submitText"></customer-assign>
  </div>
</template>
<script>
import orgTree from '@/components/company/org-tree.vue';
import customerAssign from '@/components/app/customer/customer-assign.vue';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { _debounce } from '@/common/js/public';
import { getRequest, postRequest } from '@/api/api';
export default {
  name: 'app-detail',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    tabType: {
      type: String,
      default() {
        return '1';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名
      wxEnterpriseRelatedId: localStorage.getItem('userInfos') ? JSON.parse(localStorage.getItem('userInfos')).wxEnterpriseId : '',
      departObj: {}, //选中的部门、门店
      gicData: [], // gic
      clerkId: '', // 导购
      searchInput: '',
      clerkOptions: [],
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      selectRadio: 0, // 0：选择当页； 1：选择全部
      multipleSelection: [],
      assignShow: false
    };
  },
  methods: {
    /**
     * 关闭分配
     */
    closeText() {
      const that = this;
      that.assignShow = false;
    },
    submitText() {
      const that = this;
      that.assignShow = false;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      if (that.departObj.isStore == 0) {
        return false;
      }
      that.currentPage = 1;
      that.getTableList();
    }, 500),
    /**
     * 搜索标签清空
     */
    clearSearch() {
      const that = this;
      if (that.departObj.isStore == 0) {
        return false;
      }
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 每次选中一个树中节点，把当前数据返回，
     * 获取选中部门/门店数据
     */
    getSelectData(obj) {
      const that = this;
      console.log(obj);
      that.departObj = obj;
      // 判断是不是门店
      if (obj.isStore == 0) {
        that.currentPage = 1;
        that.tableData = [];
        that.clerkOptions = [];
        that.clerkId = ''; // 导购
        that.searchInput = '';
        that.multipleSelection = [];
        return false;
      }
      // 如果是门店
      that.getClerkData();
      that.getTableList();
    },
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * 显示选择本页/全部
     */
    handleCommand(command) {
      this.selectRadio = command;
    },
    /**
     * 分配
     */
    toAssignMember(index, row) {
      const that = this;
      that.editRow = row;
      // 如果选中一个分配，就要把批量已选的全部置空
      that.multipleSelection = [];
      that.$refs.multipleTable.clearSelection();
      that.assignShow = true;
    },
    /**
     * 批量分配
     */
    toAssign() {
      const that = this;
      that.editRow = {};
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择会员'
        });
        return false;
      }
      that.assignShow = true;
    },

    /**
     * 获取 gic 数据
     */
    getGicData() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId
      };
      getRequest('/haoban-app-customer-web/gic-department-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (resData.result.length) {
              resData.result.forEach(ele => {
                ele.loading = false;
                ele.expand = false;
                ele.children = [];
                ele.level = 0;
                ele.gicBind = true;
                ele.label = ele.departmentName;
              });
            }
            that.gicData = resData.result || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 条件
     */
    changeCondition() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     *  分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },

    /**
     * 获取导购列表
     */
    getClerkData() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId,
        storeId: that.departObj.departmentId
      };
      postRequest('/haoban-app-customer-web/clerk-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.clerkOptions = resData.result || [];
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取列表数据（只有门店才有）
     */
    getTableList() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId,
        pageNum: that.currentPage,
        pageSize: that.pageSize,
        storeId: that.departObj.departmentId,
        clerkId: that.clerkId,
        keyword: that.searchInput
      };
      postRequest('/haoban-app-customer-web/app-get-member-info-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result || [];
            that.total = resData.result.totalCount;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if (newData) {
        that.getGicData(that.brandId);
      }
    }
  },
  mounted() {
    const that = this;
    that.$emit('showTab', '2');
    if (that.brandId) {
      that.getGicData(that.brandId);
    }
  },
  components: { orgTree, customerAssign }
};
</script>
<style lang="less" scoped>
.w-500 {
  width: 500px;
}

.w-195 {
  width: 195px;
}

.color-1890ff {
  color: #2f54eb;
}

.p-20 {
  padding: 20px;
}

.p-l-38 {
  padding-left: 38px;
}

.p-l-199 {
  padding-left: 199px;
}

.app-detail-wrap {
  height: 100%;
  background: #fff;
  .list-body {
    position: relative;
    width: 100%;
    .list-left {
      position: relative;
      width: 300px;
      min-width: 300px;
      max-width: 300px;
      padding: 20px 0 0 0px;
    }
    .list-right {
      position: relative;
      min-width: calc(100% - 300px);
      min-height: calc(100vh - 94px);
      border-left: 1px solid #e4e7ed;
    }
  }
}
</style>
