<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cs:csCustomerFollow:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cs:csCustomerFollow:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cs:csCustomerFollow:remove']">删除</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['cs:csCustomerFollow:import']">导入</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cs:csCustomerFollow:export']">导出</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleExportTemplate"
          v-hasPermi="['cs:csCustomerFollow:export']">导出模板</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleSearch">高级搜索</el-button>
      </el-col>
      <el-col :span="8">
        <span style="padding-right: 10px;">表格设置</span>
        <el-select v-model="tableHeaderList" multiple collapse-tags placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div v-if="showSearch" ref="showSearch" class="searchForm">

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键词">
          <el-input placeholder="请输入关键词" v-model="queryParams.keyword" />
        </el-form-item>
        <el-form-item label="回访时间" prop="followTime">
          <el-date-picker clearable v-model="queryParams.followTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择回访时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="queryParams.customerName" placeholder="请输入客户姓名" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户单位" prop="companyNameId">
          <el-input v-model="queryParams.companyNameId" placeholder="请输入客户单位" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户类别" prop="customerClassId">
          <el-input v-model="queryParams.customerClassId" placeholder="请输入客户类别" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="回访方式" prop="followTypeId">
          <el-input v-model="queryParams.followTypeId" placeholder="请输入回访方式" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="回访结果" prop="followResult">
          <el-input v-model="queryParams.followResult" placeholder="请输入回访结果" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="客户成交意向" prop="transactionIntentionId">
          <el-input v-model="queryParams.transactionIntentionId" placeholder="请输入客户成交意向" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="提交时间" prop="dateRange">
          <el-date-picker clearable v-model="queryParams.dateRange" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </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>
    </div>
    <el-table v-loading="loading" :data="csCustomerFollowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort">
        <template slot-scope="scope">
          {{ ((queryParams.pageNum - 1) * queryParams.pageSize) + (scope.$index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="回访时间" align="center" prop="followTime"> v-if="tableHeaderList.indexOf('pr">') != -1"
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.followTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" prop="customerName"
        v-if="tableHeaderList.indexOf('customerName') != -1" />
      <el-table-column label="客户单位" align="center" prop="companyNameId"
        v-if="tableHeaderList.indexOf('companyName') != -1" />
      <el-table-column label="部门/院系" align="center" prop="deptName" v-if="tableHeaderList.indexOf('deptName') != -1" />
      <el-table-column label="客户电话" align="center" prop="customerPhone"
        v-if="tableHeaderList.indexOf('customerPhone') != -1" />
      <el-table-column label="客户类别" align="center" prop="customerClassId"
        v-if="tableHeaderList.indexOf('customerClass') != -1" />
      <el-table-column label="回访方式" align="center" prop="followTypeId"
        v-if="tableHeaderList.indexOf('followType') != -1" />
      <el-table-column label="回访内容和目的" align="center" prop="followContent"
        v-if="tableHeaderList.indexOf('followContent') != -1">
        <template slot-scope="{row}">
          <div v-html="row.followContent"></div>
        </template>
      </el-table-column>
      <el-table-column label="回访结果" align="center" prop="followResult"
        v-if="tableHeaderList.indexOf('followResult') != -1" />
      <el-table-column label="解决方法" align="center" prop="resolvent" v-if="tableHeaderList.indexOf('resolvent') != -1" />
      <el-table-column label="后续跟踪情况" align="center" prop="followSituation"
        v-if="tableHeaderList.indexOf('followSituation') != -1" />
      <el-table-column label="客户成交意向" align="center" prop="transactionIntention"
        v-if="tableHeaderList.indexOf('transactionIntention') != -1" />
      <el-table-column label="回访人" align="center" prop="followUser"
        v-if="tableHeaderList.indexOf('followUser') != -1" />
      <el-table-column label="累计回访次数" align="center" prop="followTotal"
        v-if="tableHeaderList.indexOf('followTotal') != -1" />
      <el-table-column label="下次回访日期" align="center" prop="nextFollowTime"
        v-if="tableHeaderList.indexOf('nextFollowTime') != -1" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextFollowTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" v-if="tableHeaderList.indexOf('remarks') != -1" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cs:csCustomerFollow:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csCustomerFollow:remove']">删除</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-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="回访时间" prop="followTime">
          <el-date-picker clearable v-model="form.followTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择回访时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="客户单位" prop="companyNameId">
          <el-input v-model="form.companyNameId" placeholder="请输入客户单位" />
        </el-form-item>
        <el-form-item label="二级单位(部门/学院)" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入二级单位(部门/学院)" />
        </el-form-item>
        <el-form-item label="客户电话" prop="customerPhone">
          <el-input v-model="form.customerPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="客户类别" prop="customerClassId">
          <el-input v-model="form.customerClassId" placeholder="请输入客户类别" />
        </el-form-item>
        <el-form-item label="回访方式" prop="followTypeId">
          <el-input v-model="form.followTypeId" placeholder="请输入回访方式" />
        </el-form-item>
        <el-form-item label="回访内容和目的">
          <editor v-model="form.followContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="回访结果" prop="followResult">
          <el-input v-model="form.followResult" placeholder="请输入回访结果" />
        </el-form-item>
        <el-form-item label="解决方法" prop="resolvent">
          <el-input v-model="form.resolvent" placeholder="请输入解决方法" />
        </el-form-item>
        <el-form-item label="后续跟踪情况" prop="followSituation">
          <el-input v-model="form.followSituation" placeholder="请输入后续跟踪情况" />
        </el-form-item>
        <el-form-item label="客户成交意向" prop="transactionIntention">
          <el-input v-model="form.transactionIntention" placeholder="请输入客户成交意向" />
        </el-form-item>
        <el-form-item label="回访人" prop="followUser">
          <el-input v-model="form.followUser" placeholder="请输入回访人" />
        </el-form-item>
        <el-form-item label="累计回访次数" prop="followTotal">
          <el-input v-model="form.followTotal" placeholder="请输入累计回访次数" />
        </el-form-item>
     <!--   <el-form-item label="附件" prop="annexUrl">
          <el-upload class="upload-demo" :action="$baseUrl + '/api/common/uploadFileLocal'" :limit="1"
                     :on-success="handleSuccess" :file-list="fileList">
            <el-button size="small" type="primary" icon="el-icon-upload2">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">支持扩展名：pdf.doc.jpg.png…</div>
          </el-upload>
        </el-form-item> -->
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { listCsCustomerFollow, getCsCustomerFollow, delCsCustomerFollow, addCsCustomerFollow, updateCsCustomerFollow } from "@/api/cs/csCustomerFollow";

export default {
  name: "CsCustomerFollow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 回访客户记录表格数据
      csCustomerFollowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        dateRange: [],
        resId: null,
        resName: null,
        resType: null,
        resourceClassId: null,
        resourceClass: null,
        followTime: null,
        customerName: null,
        companyNameId: null,
        companyName: null,
        deptId: null,
        deptName: null,
        customerPhone: null,
        customerClassId: null,
        customerClass: null,
        followTypeId: null,
        followType: null,
        followContent: null,
        followResult: null,
        resolvent: null,
        followSituation: null,
        transactionIntentionId: null,
        transactionIntention: null,
        followUser: null,
        followTotal: null,
        nextFollowTime: null,
        annexUrl: null,
        remarks: null,
        content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      options: [{
        value: 'followTime',
        label: '回访时间'
      }, {
        value: 'customerName',
        label: '客户姓名'
      }, {
        value: 'companyName',
        label: '客户单位'
      }, {
        value: 'deptName',
        label: '部门/院系'
      }, {
        value: 'customerPhone',
        label: '客户电话'
      }, {
        value: 'customerClass',
        label: '客户类别'
      }, {
        value: 'followType',
        label: '回访方式'
      }, {
        value: 'followContent',
        label: '回访内容和目的'
      }, {
        value: 'followResult',
        label: '回访结果'
      }, {
        value: 'resolvent',
        label: '解决方法'
      }, {
        value: 'followSituation',
        label: '后续跟踪情况'
      }, {
        value: 'transactionIntention',
        label: '客户成交意向'
      }, {
        value: 'followUser',
        label: '回访人'
      }, {
        value: 'followTotal',
        label: '累计回访次数'
      }, {
        value: 'nextFollowTime',
        label: '下次回访日期'
      }, {
        value: 'remarks',
        label: '备注'
      }],
      tableHeaderList: ['followTime', 'customerName', 'companyName',
        'deptName', 'customerPhone', 'customerClass', 'followType',
        'followContent', 'followResult', 'resolvent', 'followSituation', 'transactionIntention',
        'followUser',
        'followTotal', 'nextFollowTime', 'remarks'],
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/cs/csCustomerFollow/importCsCustomerFollow"
      },
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    // document.addEventListener(
    //   "click",
    //   (e) => {
    //     let showSearch = this.$refs.showSearch;
    //     if (!showSearch.contains(e.target)) {
    //       this.showSearch = false;
    //     }
    //   },
    //   true
    // );
  },
  methods: {
    /** 查询回访客户记录列表 */
    getList() {
      this.loading = true;
      if (null != this.queryParams.dateRange && '' != this.queryParams.dateRange) {
        this.queryParams["beginCreateAt"] = this.queryParams.dateRange[0];
        this.queryParams["endCreateAt"] = this.queryParams.dateRange[1];
      }
      listCsCustomerFollow(this.queryParams).then(response => {
        this.csCustomerFollowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSearch() {
      this.showSearch = !this.showSearch
    },
    handleSuccess(response){
      this.form.annexUrl = response.url
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        customerFollowId: null,
        resId: null,
        resName: null,
        resType: null,
        resourceClassId: null,
        resourceClass: null,
        followTime: null,
        customerName: null,
        companyNameId: null,
        companyName: null,
        deptId: null,
        deptName: null,
        customerPhone: null,
        customerClassId: null,
        customerClass: null,
        followTypeId: null,
        followType: null,
        followContent: null,
        followResult: null,
        resolvent: null,
        followSituation: null,
        transactionIntentionId: null,
        transactionIntention: null,
        followUser: null,
        followTotal: null,
        nextFollowTime: null,
        annexUrl: null,
        remarks: null,
        content: null,
        status: "0",
        sort: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.showSearch = false
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        dateRange: [],
        resId: null,
        resName: null,
        resType: null,
        resourceClassId: null,
        resourceClass: null,
        followTime: null,
        customerName: null,
        companyNameId: null,
        companyName: null,
        deptId: null,
        deptName: null,
        customerPhone: null,
        customerClassId: null,
        customerClass: null,
        followTypeId: null,
        followType: null,
        followContent: null,
        followResult: null,
        resolvent: null,
        followSituation: null,
        transactionIntentionId: null,
        transactionIntention: null,
        followUser: null,
        followTotal: null,
        nextFollowTime: null,
        annexUrl: null,
        remarks: null,
        content: null,
      }

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerFollowId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回访客户记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const customerFollowId = row.customerFollowId || this.ids
      getCsCustomerFollow(customerFollowId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改回访客户记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.customerFollowId != null) {
            updateCsCustomerFollow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCsCustomerFollow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const customerFollowIds = row.customerFollowId || this.ids;
      this.$modal.confirm('是否确认删除回访客户记录编号为"' + customerFollowIds + '"的数据项？').then(function () {
        return delCsCustomerFollow(customerFollowIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cs/csCustomerFollow/export', {
        ...this.queryParams
      }, `回访客户记录_${new Date().getTime()}.xlsx`)
    },
    handleExportTemplate() {
      this.download('/cs/csCustomerFollow/exportTemplate', {
        ...this.queryParams
      }, `回访客户记录模板_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  }
};
</script>


<style scoped>
::v-deep .dialog1 {
  background-color: #F8F8F8;
}

.searchForm {
  width: 650px;
  padding: 20px;
  position: absolute;
  left: 200px;
  z-index: 100;
  background-color: #FFFFFF;
  border: 1px solid #777777;
}
</style>
