<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-15 10:43:52
 -->
<template>
  <div class="app-detail-wrap">
    <div class="border-box p-20">
      <div class="list-body flex">
        <div class="list-left">
          <org-tree v-if="gicData.length" :itemData="gicData"></org-tree>
        </div>
        <div class="list-right">
          <div class="list-right-body">
            <div class="table-top flex flex-space-between">
              <el-select class="w-168 m-l-10" v-model="conditionObj.effectiveStatus" placeholder="全部状态" @change="changeCondition">
                <el-option label="全部状态" value=""></el-option>
                <el-option label="有效" value="1"></el-option>
                <el-option label="失效" value="0"></el-option>
              </el-select>
              <el-button type="primary" @click="toAssign">批量分配会员</el-button>
            </div>
            <div class="table-body">
              <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>
                  <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>
  </div>
</template>
<script>
import orgTree from '@/components/company/org-tree.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 : '',
      gicData: [],
      tableData: [],
      currentPage: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      selectRadio: 0, // 0：选择当页； 1：选择全部
      multipleSelection: [],
      assignShow: false
    };
  },
  methods: {
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * 显示选择本页/全部
     */
    handleCommand(command) {
      this.selectRadio = command;
    },
    /**
     * 分配
     */
    toAssignMember(index, row) {
      const that = this;
      that.assignShow = true;
    },
    /**
     * 批量分配
     */
    toAssign() {
      const that = this;
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择会员'
        });
        return false;
      }
      that.assignShow = true;
    },
    /**
     * 保存
     */
    submit: _debounce(function(type) {
      const that = this;
      that.loading = true;
      that.setData(type);
    }, 500),

    /**
     * 保存-API
     */
    setData(type) {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId
        // enterpriseId: JSON.parse(localStorage.getItem('userInfos')).enterpriseId
      };
      let url;
      if (type != 'showFlag') {
        para[type] = !!that.myCustomData[type] ? 1 : 0;
        url = '/haoban-app-customer-web/customer/save-online-chat-setting';
        if (type === 'searchFlag') {
          url = '/haoban-app-customer-web/customer/save-customer-setting';
        }
      }
      postRequest(url, para)
        .then(res => {
          let resData = res.data;
          that.loading = false;
          if (resData.errorCode == 1) {
            showMsg.showmsg('保存成功', 'success');
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.loading = false;
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取 gic 数据
     */
    getGicData() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId
      };
      getRequest('/haoban-manage3-web/unbind-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
          });
        });
    },
    /**
     *  分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 获取列表数据（只有门店才有）
     */
    getTableList() {
      const that = this;
      let para = {
        enterpriseId: that.brandId,
        wxEnterpriseRelatedId: that.wxEnterpriseRelatedId,
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      postRequest('/haoban-app-customer-web/customer/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 }
};
</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: 200px;
      min-width: 200px;
      max-width: 200px;
    }
    .list-right {
      position: relative;
      min-width: calc(100% - 200px);
      min-height: calc(100vh - 94px);
      border-left: 1px solid #e4e7ed;
    }
  }
}
</style>
