<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="客户名称" prop="externalUserName">
        <el-input
          v-model="queryParams.externalUserName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入备注电话号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

        <el-tabs type="card" v-model="queryParams.Status" @tab-click="handleClickX">
          <el-tab-pane v-for="(item,index) in stageOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
        </el-tabs>

        <el-table v-loading="loading" :data="crmCourseStudyList" border>
          <el-table-column label="企微客户名称" align="center" prop="externalUserName" width="120px"/>
          <el-table-column label="头像" align="center" prop="avatar" width="120px">
            <template slot-scope="scope">
              <el-popover
                placement="right"
                title=""
                trigger="hover">
                <img slot="reference" :src="scope.row.avatar" width="100px">
                <img :src="scope.row.avatar" style="max-width: 150px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="电话" align="center" prop="mobile" width="150px">
          </el-table-column>
          <el-table-column label="是否绑定CRM" align="center" prop="customerId"  width="120px" >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.customerId" type="success">已绑定CRM</el-tag>
              <el-tag v-else type="danger">未绑定CRM</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="课程名称" align="center" prop="courseName"/>
          <el-table-column label="课程学习开始时间" align="center" prop="studyCreateTime" width="180px"/>
          <el-table-column label="操作" align="center" prop="rate" width="120px">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-notebook-2"
                @click="hangdleDetails(scope.row)"
              >详情</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

    <el-drawer v-loading="detailsTitle.loading" :title="detailsTitle.title" :visible.sync="detailsTitle.open" size="75%" style="font-weight: bolder">
      <contactStageDetails :stageDetailsFrom="detailsTitle.detailsFrom"></contactStageDetails>
    </el-drawer>

  </div>
</template>

<script>
import {getQwCrmCourseStudyList,getQwCrmCourseStudyDetail } from "@/api/qw/externalContactCrm";
import contactStageDetails from '@/views/qw/externalContactStage/stageDetail'
import dayPartingIndexDetails from '@/views/qw/autoTags/dayPartingIndexDetails.vue'

export default {
  name: "contactCrm",
  components: { dayPartingIndexDetails,contactStageDetails },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业微信客户表格数据
      crmCourseStudyList: [],
      //客户课程详情
      crmCourseStudyDetailList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailsTitle: {
        title: "",
        open: false,
        loading:false,
        detailsFrom:{},
      },
      //课程看客状态
      stageOptions:[],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        externalUserName: null,
        courseName: null,
        Status: 0,
        mobile: null,
        logCreateTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    this.getDicts("sys_qw_external_stage_status").then(response => {
      this.stageOptions = response.data;
    });

    this.getList();
  },
  methods: {

    /** 查询企业微信客户列表 */
    getList() {
      this.loading = true;
      getQwCrmCourseStudyList(this.queryParams).then(response => {

        this.crmCourseStudyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    handleClickX(tab, event) {
      this.queryParams.Status=tab.name;
      this.handleQuery();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        contactCrmId: null,
        externalUserId: null,
        externalUserName: null,
        avatar: null,
        customerId: null,
        mobile: null,
        studyId: null,
        courseId: null,
        userId: null,
        periodTotal: null,
        periodStudy: null,
        status: null,
        studyCreateTime: null,
        course_name: null,
        logId: null,
        videoId: null,
        currentTimes: null,
        logCreateTime: null,
        times: null,
        rate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    hangdleDetails(row) {

      this.reset();

      // 深拷贝表单数据
      const requestData = { ...row };

      this.detailsTitle.title="课程详情";
      this.detailsTitle.open=true;
      this.detailsTitle.detailsFrom=requestData;

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信客户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportExternalContact(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
  .tag-box{
    margin: 10px;
    padding: 5px;
    background-color: #f0f0f0 ;
    border-radius: 4px;
  }
  .tag-selectd{
    background-color: rgb(231, 244, 255) ;
    border: 1px solid rgb(24, 144, 255) ;
  }
  .el-tag + .el-tag {
      margin-left: 10px;
    }
    .button-new-tag {
      margin-left: 10px;
      height: 32px;
      line-height: 30px;
      padding-top: 0;
      padding-bottom: 0;
    }
    .input-new-tag {
      width: 90px;
      margin-left: 10px;
      vertical-align: bottom;
    }
</style>
