<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-09-18 15:25:12
 * @LastEditors: 无尘
 * @LastEditTime: 2019-09-25 17:33:50
 -->

<!--
<chat-log :brandId="brandId"></chat-log>
import chatLog from '@/components/app/chat-log.vue';
components: {
  chatLog
}
-->

<template>
  <div class="chat-log boder-box">
    <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
    <div class="chat-log-content boder-box">
      <div class="table-condition flex flex-space-between m-b-23">
        <div class="table-condition-left">
          <el-input :placeholder="conditionObj.searchType == 2 ? '请输入会员手机号/卡号 ' : '请输入导购姓名/手机号/code'" v-model="conditionObj.searchInput" class="w-369" @keyup.native="value => toInput(value, conditionObj.searchInput)" clearable @clear="clearInput">
            <el-select class="w-103" v-model="conditionObj.searchType" slot="prepend" placeholder="请选择">
              <el-option label="按导购" value="1"></el-option>
              <el-option label="按会员" value="2"></el-option>
            </el-select>
          </el-input>
          <el-date-picker class="m-l-10" prefix-icon="el-icon-time" :picker-options="pickerOptions" v-model="conditionObj.dateRange" @change="changeDate" :editable="false" :value-format="'yyyy-MM-dd'" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"> </el-date-picker>
        </div>
        <div class="table-condition-right">
          <el-button :disabled="conditionObj.searchType == 2" type="primary" plain @click="showDialogLayer('store', conditionObj.storeGroup)">门店架构选人</el-button>
        </div>
      </div>
      <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }">
        <el-table-column label="姓名" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.name || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="员工状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1">正式</span>
            <span v-if="scope.row.status == 2">试用</span>
            <span v-if="scope.row.status == -1">无状态</span>
          </template>
        </el-table-column>
        <el-table-column prop="" label="导购手机号" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.phoneNumber || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="门店名称" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.storeName || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="员工code" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.code || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="消息数" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.messageCount }}</template>
        </el-table-column>
        <el-table-column prop="" label="会员姓名" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.memberName || '--' }}</template>
        </el-table-column>
        <el-table-column prop="" label="会员手机号" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.memberPhoneNumber || '--' }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="showDetail(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">
        <el-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"> </el-pagination>
      </div>
    </div>
    <chat-log-list :brandId="brandId" :itemRow="itemRow" :showFlag="showFlag" @hideDetail="hideDetail"></chat-log-list>
    <vue-select-store ref="storeSelector" :currentBrand="activeGroup" :appScene="appScene" :treeSet="storeTreeSet" :selectType="'all'" :defaultList="defaultStoreList" @handleSelectedList="handleSelectedList"> </vue-select-store>
  </div>
</template>
<script>
import vueSelectStore from 'components/common/vueSelectStore';
import commonDetailTop from '@/components/app/common-detail-top.vue';
import chatLogList from '@/components/app/chat-log-list.vue';
import { getRequest } from '@/api/api';
import errMsg from '@/common/js/error';
import { _debounce } from '@/common/js/public';
export default {
  name: 'chat-log',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  components: {
    commonDetailTop,
    vueSelectStore,
    chatLogList
  },
  data() {
    return {
      activeBrand: this.brandId, // 商户(品牌) id
      activeGroup: this.activeGroupId, // 商户(品牌) groupId
      activeId: '1',
      topMenuData: [
        {
          id: '1',
          name: '聊天记录'
        }
      ],
      tableData: [],
      // 条件
      conditionObj: {
        searchInput: '',
        searchType: '1',
        storeGroup: [],
        dateRange: []
      },
      choiceDateCopy: [],
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }
        ],
        onPick: ({ maxDate, minDate }) => {
          this.choiceDateCopy = [minDate.getTime()];
          if (maxDate) {
            this.choiceDateCopy = [];
          }
        },
        disabledDate: time => {
          if (this.choiceDateCopy.length) {
            const one = 365 * 24 * 3600 * 1000;
            const minTime = this.choiceDateCopy[0] - one;
            const maxTime = this.choiceDateCopy[0] + one;
            return time.getTime() < minTime || time.getTime() > maxTime || time.getTime() > Date.now() - 8.64e6;
          }
          return time.getTime() > Date.now() - 8.64e6;
        }
      },
      // 门店选择
      appScene: 2,
      defaultStoreList: [],
      storeTreeSet: {
        isSelectPerson: false, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false // 是否单选
      },
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      showFlag: false,
      itemRow: {}
    };
  },
  methods: {
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     *  选择日期
     */
    changeDate(val) {
      const that = this;
      if (!val) {
        that.choiceDateCopy = [];
        that.conditionObj.dateRange = [];
      }
      that.getTableList();
    },
    /**
     * 弹窗显示事件
     */
    showDialogLayer(type, selData) {
      const that = this;
      that.selectType = type;
      that.changed = type;
      that.defaultStoreList = selData;
      that.storeTreeSet = {
        dialogVisible: true,
        isSingle: false,
        isSelectPerson: false
      };
    },
    /**
     * 处理已选部门
     */
    handleSelectedList(group) {
      const that = this;
      // type:	1:员工,2:部门,3:门店;
      that.conditionObj.storeGroup = group;
      that.getTableList();
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

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

    /**
     * 显示详情
     */
    showDetail(index, row) {
      const that = this;
      that.showFlag = true;
      let rowObj = JSON.parse(JSON.stringify(row));
      rowObj.userMessageId = '';
      that.itemRow = rowObj;
    },

    hideDetail() {
      const that = this;
      that.showFlag = false;
      that.itemRow = {};
      that.$nextTick(() => {
        document.body.style.overflow = 'auto';
      });
    },

    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      let storeGroups = [];
      let filterStoreIds = [];
      let clerkIds = [];
      that.conditionObj.storeGroup.forEach(ele => {
        if (ele.type == 1) {
          clerkIds.push(ele.employeeClerkId);
        }
        if (ele.type == 2) {
          storeGroups.push(ele.groupId);
        }
        if (ele.type == 3) {
          filterStoreIds.push(ele.storeId);
        }
      });
      if (!that.conditionObj.dateRange) {
        that.conditionObj.dateRange = [];
      }
      let para = {
        keyWord: that.conditionObj.searchInput || '', // 搜索字段
        type: that.conditionObj.searchType || '', // 搜索类型
        groupIds: storeGroups.join(',') || '', // 门店分组, 门店
        storeIds: filterStoreIds.join(',') || '', // 门店分组,分组
        clerkIds: clerkIds.join(',') || '', //
        startTime: that.conditionObj.dateRange[0] || '',
        endTime: that.conditionObj.dateRange[1] || '',
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize, // 一页显示个数
        brandId: that.activeBrand
        // taskType: 1 //0不良评价，1话务任务
      };
      getRequest('/haoban-app-member-web/customer/find_message_list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.list || [];
            that.total = resData.result.total;
            return false;
          }
          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.activeBrand = newData;
        that.getTableList();
      }
    },
    activeGroupId: function(newData, oldData) {
      const that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    }
  },
  mounted() {
    const that = this;
    that.getTableList();
  }
};
</script>
<style type="text/less" lang="less" scoped>
.chat-log {
  width: 100%;
  .w-103 {
    width: 103px;
  }
  .w-369 {
    width: 369px;
  }
  .el-input {
    /deep/ .el-input-group__prepend {
      background-color: #fff;
    }
  }
  .chat-log-content {
    padding: 20px;
    .el-button--primary.is-plain {
      color: #2f54eb;
      background: #fff;
      border-color: #2f54eb;
      &:hover {
        color: #fff;
        background: #2f54eb;
        border-color: #2f54eb;
      }
      &.is-disabled.is-plain {
        background-color: #f5f7fa;
        border-color: #dcdfe6;
        color: #c0c4cc;
      }
    }
  }
}
</style>
