<template>
  <div class="app-container">


    <div class = "search-bx"  >
        <div class="search">
          <el-row :gutter="0">


              <el-input v-model="tableObj.getdataListParm.parammaps.telVisitCode" placeholder="回访单号" style="width: 180px;padding-bottom:10px;" class="filter-item" clearable />

              <!-- <el-select v-model="tableObj.getdataListParm.parammaps.customerId" placeholder="客户名称" class="filter-item" style="width: 120px;" @change="changeCustomer0" clearable filterable>
                <el-option v-for="item in customerNameList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select> -->

              <!-- <el-autocomplete v-model="tableObj.getdataListParm.parammaps.customerName" :fetch-suggestions="customer_search" placeholder="客户名称" style="width: 200px;margin-right:10px" @select="changeCustomer0" @clear="clear_customer_select" @blur="customer_search_blur" clearable>
                <template slot-scope="{ item }">
                  <span class="name">{{ item.name }}</span>
                </template>
              </el-autocomplete> -->

              <el-input v-model="tableObj.getdataListParm.parammaps.customerName" placeholder="客户名称" style="width: 180px;" class="filter-item" clearable />

              <el-select v-model="tableObj.getdataListParm.parammaps.groupId" placeholder="集团" class="filter-item" style="width: 120px;" clearable>
                <el-option v-for="(item,index)  in groupNameList" :key="index" :label="item.name" :value="item.id" />
              </el-select>

              <el-select v-model="tableObj.getdataListParm.parammaps.visiterId" placeholder="回访人" class="filter-item" style="width: 120px;" clearable>
                <el-option v-for="(item,index)  in visiterNameList" :key="index" :label="item.name" :value="item.id" />
              </el-select>

              <el-input v-model="tableObj.getdataListParm.parammaps.receiverName" placeholder="被回访人" style="width: 180px;padding-bottom:10px;" class="filter-item" clearable />
<!--
              <el-select v-model="tableObj.getdataListParm.parammaps.receiverName" placeholder="被回访人" class="filter-item" style="width: 120px;" clearable filterable>
                <el-option v-for="(item,index) in receiverNameList" :key="index" :label="item.name" :value="item.name" />
              </el-select> -->

              <el-select v-model="tableObj.getdataListParm.parammaps.typeIds" placeholder="回访类型" class="filter-item" style="width: 120px;" clearable>
                <el-option v-for="(item,index)  in typeNameList" :key="index" :label="item.name" :value="item.id" />
              </el-select>

              <el-select v-model="tableObj.getdataListParm.parammaps.statusId" placeholder="回访状态" class="filter-item" style="width: 120px;" clearable>
                <el-option v-for="(item,index)  in statusNameList" :key="index" :label="item.name" :value="item.name" />
              </el-select>

              <el-date-picker v-model="tableObj.getdataListParm.parammaps.inputDatetime" class="inputDatetime filter-item" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 250px;" />


                <el-button class="successBorder" @click="form_search">查询</el-button>
                <el-button class="successBorder" @click="form_clear">重置</el-button>


                <el-button v-if="isTelephoneAdd" class="success" icon="el-icon-plus" @click="form_add">新增</el-button>
                <!-- <el-button  class="success" icon="el-icon-upload2" @click="form_export">导出</el-button> -->

          </el-row>
        </div>

        <!-- <div class="operation">
          <el-row :gutter="0">
            <el-col :span="21">
              <el-button v-if="isRoleEdit" class="success" icon="el-icon-plus" @click="form_add">新增</el-button>
            </el-col>
            <el-col :span="3">

            </el-col>
          </el-row>
        </div> -->
    </div>


    <div class="table"    >
      <el-table :key="tableObj.tableKey" v-loading="tableObj.listLoading" element-loading-text="给我一点时间" :data="tableObj.list" border fit highlight-current-row style="width: 100%;" :row-style="rowStyle" :cell-style="cellStyle" class="elTable table-fixed" :max-height="myHeight" >
        <!-- <el-table-column type="selection" align="center" width="50" /> -->
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (tableObj.pageNum-1) * tableObj.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回访单号" min-width="150px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.telVisitCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" min-width="120px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.customerName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="集团" min-width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.groupName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回访日期" min-width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.tdate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回访人" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.visiterName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="被回访人" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.receiverName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="被回访人电话" min-width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.receiverTel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回访类型" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.typeNames }}</span>
          </template>
        </el-table-column>
        <el-table-column label="回访状态" min-width="110px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.statusName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="120px" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
        <el-table-column label="录入日期" min-width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.created }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center"  :width="autoWidth"  class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <!-- <el-button v-if="isRoleEdit" class="miniSuccess" @click="form_edit(row)">编辑</el-button>
            <el-button v-if="isRoleEdit" class="miniDanger" @click="form_delete(row)">删除</el-button> -->
            <el-button class="miniPrimary" @click="form_see(row)">查看</el-button>
            <el-button class="miniSuccess" v-if="isTelephoneAdd && row.isModify == '1' && row.createrId == employeid" @click="form_edit(row)">编辑</el-button>
            <el-button class="miniDanger" v-if="isTelephoneAdd && row.isDel == '1' && row.createrId == employeid" @click="form_delete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="tableObj.total>=0" :total="tableObj.total" :page.sync="tableObj.getdataListParm.offset" :limit.sync="tableObj.getdataListParm.pagecount" @pagination="get_table_data" />
    </div>

    <!-- 新增/编辑 -->
    <el-dialog :title="textMap[create.dialogStatus]" :visible.sync="create.dialogFormVisible" :close-on-click-modal="false" :before-close="dialog_close"  width="40%">
      <div class="app-add">
        <el-form ref="temp" :rules="create.rules" :model="create.temp" label-position="right" label-width="150px" style="width: 80%;margin:0 auto 50px">
          <!-- <el-form-item label="客户名称：" prop="customerName">

            <el-autocomplete ref="customerName" v-model="create.temp.customerName" :fetch-suggestions="customer_search" placeholder="客户名称" style="width: 100%;margin-right:10px" @select="changeCustomer" :disabled="create.dialogStatus=='see' || create.dialogStatus=='update'" @clear="clear_customer_selectTemp" @blur="customer_search_blurTemp" clearable >
              <template slot-scope="{ item }">
                <span class="name">{{ item.name }}</span>
              </template>
            </el-autocomplete>


          </el-form-item>
          <el-form-item label="集团：" prop="groupName">
            <el-input ref="groupName" v-model="create.temp.groupName" class="filter-item" style="width: 100%;" disabled/>
          </el-form-item> -->
          <!-- <el-form-item label="所属集团：" prop="groupId">
              <el-select v-model="create.temp.groupId" placeholder="所属集团" class="filter-item" style="width: 100%;" filterable  @change="change_group" clearable :disabled="create.dialogStatus=='see' || create.dialogStatus=='update'  || isHome=='home'  " >
                  <el-option v-for="item in groupNameList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
           </el-form-item> -->

          <el-form-item label="客户名称：" prop="customerId">
              <el-select v-model="create.temp.customerId" filterable placeholder="客户名称" class="filter-item"  style="width: 100%;" clearable @change="change_customer" :disabled="create.dialogStatus=='see' || create.dialogStatus=='update' || isHome=='home'  " >
                  <el-option v-for="item in create.customerNameList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
          </el-form-item>

          <el-form-item label="集团：" prop="groupName">
            <el-input ref="groupName" v-model="create.temp.groupName" class="filter-item" style="width: 100%;" disabled/>
          </el-form-item>

          <el-form-item label="回访日期：" prop="tdate">
            <el-date-picker v-model="create.temp.tdate" type="date" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 100%;" :clearable="false" :disabled="create.dialogStatus=='see'" />
          </el-form-item>
          <el-form-item label="回访人：" prop="visiterId">
            <el-select ref="visiterId" v-model="create.temp.visiterId" filterable placeholder="请选择回访人" class="filter-item" style="width: 100%;" :disabled="create.dialogStatus=='see'">
              <el-option v-for="item in visiterNameList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="被回访人：" prop="receiverId">
            <el-select ref="receiverId" v-model="create.temp.receiverId" filterable placeholder="请选择被回访人" class="filter-item" style="width: 100%;" @change="changeReceiver" allow-create :disabled="create.dialogStatus=='see'">
              <el-option v-for="item in create.receiverNameList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="被回访人电话：" prop="receiverTel">
            <el-input ref="receiverTel" v-model="create.temp.receiverTel" class="filter-item" style="width: 100%;" maxlength="11" placeholder="请输入电话" :disabled="create.dialogStatus=='see'" />
          </el-form-item>

          <el-form-item label="回访类型：" prop="typeIds">

            <el-checkbox-group v-model="create.temp.typeIdsArr">
              <el-checkbox v-for="(item) in typeNameList" :key="item.id" :label="item.id" @change="changeChecked" :disabled="create.dialogStatus=='see'" >{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>

          <el-form-item label="回访状态：" prop="statusName">
            <el-radio v-model="create.temp.statusName" label="良好" :disabled="create.dialogStatus=='see'" >良好</el-radio>
            <el-radio v-model="create.temp.statusName" label="不佳" :disabled="create.dialogStatus=='see'" >不佳</el-radio>
          </el-form-item>

          <el-form-item label="回访情况：" prop="content">
            <el-input ref="content" v-model="create.temp.content" type="textarea" placeholder="请输入情况说明" maxlength="255" class="filter-item" style="width: 100%;" :disabled="create.dialogStatus=='see'" />
          </el-form-item>

          <el-form-item label="下一步计划：" prop="nextId">
            <el-select ref="nextId" v-model="create.temp.nextId" filterable placeholder="请选择下一步计划" class="filter-item" style="width: 100%;" :disabled="create.dialogStatus=='see'" clearable filterable>
              <el-option v-for="item in nextNameList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>

          <el-form-item label="备注：" prop="remark">
            <el-input ref="remark" v-model="create.temp.remark" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" placeholder="请输入备注，最多255个字" maxlength="255" class="filter-item" style="width: 100%;" :disabled="create.dialogStatus=='see'" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="cancelClose" @click="dialog_close()">关闭</el-button>
          <el-button v-if="create.dialogStatus==='create' || create.dialogStatus==='update'" class="success" :disabled="isokDisable" @click="create.dialogStatus==='create'?add_dialog_save():form_edit_save()">确认</el-button>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { GetDataByName, PostDataByName, failproccess, ExecDataByConfig, GetDataByNames, checkButtons, calculativeWidth } from '@/api/common'
import Cookies from 'js-cookie'
import {  parseTime, json2excel } from '@/utils/index.js'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
export default {
  name: 'TelephoneFollow',
  components: { Pagination },
  data() {
    return {
      myHeight:document.documentElement.clientHeight - 85 - 160,

      rowData:{},
      isHome:"",

      employeid: Cookies.get('employeid'),
      isTelephoneAdd: [],  isRoleEdit: [],

      send_select_list: [
         { name: 'getCustomerSelect', offset: 0, pagecount: 0, parammaps: {} },
        { name: 'getDictListSelect1', offset: 0, pagecount: 0, parammaps: { "pid": "54" } },
        { name: 'getEmployeesSelect', offset: 0, pagecount: 0, parammaps: {} },
        // { name: 'getContacts', offset: 0, pagecount: 0, parammaps: { "customerId": "" } },
        { name: 'getDictListSelect2', offset: 0, pagecount: 0, parammaps: { "pid": "56" } },
        { name: 'getDictListSelect4', offset: 0, pagecount: 0, parammaps: { "pid": "55" } },
      ],

      customerNameList: [], // 客户名称
      groupNameList: [], // 集团
      visiterNameList: [],
      receiverNameList: [],
      typeNameList: [],
      statusNameList: [{ id: "良好", name: "良好" }, { id: "不佳", name: "不佳" }],

      nextNameList: [],


      //表格请求参数~~
      tableObj: {
        getdataListParm: {
          name: 'getTelvisit',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pagecount')),
          returntype: 'Map',
          parammaps: {
            roleId: Cookies.get('g_roleId'),
            userId: Cookies.get('g_userId'),
            telVisitCode: "",
            customerName: "",
            customerId: "",
            groupId: "",
            visiterId: "",
            receiverName: "",
            typeIds: "",
            statusId: "",
            inputDatetime: "",
            beginDate: "",
            endDate: "",
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },

      download: {
        getdataListParm: {
          name: 'getTelvisit',
          page: 1,
          offset: 1,
          pagecount: 0,
          returntype: 'Map',
          parammaps: {
            roleId: Cookies.get('g_roleId'),
            userId: Cookies.get('g_userId'),
            telVisitCode: "",
            customerName: "",
            customerId: "",
            groupId: "",
            visiterId: "",
            receiverName: "",
            typeIds: "",
            statusId: "",
            inputDatetime: "",
            beginDate: "",
            endDate: "",
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },

      // 新增/编辑
      create: {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: { customerId: '', customerName: '',groupId: '', groupName: '', tdate: parseTime(new Date(), '{y}-{m}-{d}'), visiterId: '', receiverId: '', receiverName: '', receiverTel: '', typeIds: '', typeIdsArr: [], statusName: '良好', remark: '', content: '', nextId: '', modifyName: 'add', receiverChoose: 'true' },
        rules: {
          customerId: [{ required: true, message: '必填', trigger: 'blur' }],
          //  groupId: [{ required: true, message: '必填', trigger: 'blur' }],
          tdate: [{ required: true, message: '必填', trigger: 'blur' }],
          visiterId: [{ required: true, message: '必填', trigger: 'blur' }],
          receiverId: [{ required: true, message: '必填', trigger: 'blur' }],
          receiverTel: [{ required: true, message: '必填', trigger: 'blur' }],
          typeIds: [{ required: true, message: '必填', trigger: 'blur' }],
          statusName: [{ required: true, message: '必填', trigger: 'blur' }],
          content: [{ required: true, message: '必填', trigger: 'blur' }],


        },
        // 接待人
        getdataListParm: {
          name: 'getContacts',
          returntype: 'Map',
          parammaps: { customerId: "" }
        },
        // 接待人
        getdataListParm2: {
          name: 'getgroupNameById',
          returntype: 'Map',
          parammaps: { id: "" }
        },
        // 客户名称
        getdataListParmCust: {
          name: 'getCustomerByGroupId',
          returntype: 'Map',
          parammaps: { groupId: "" }
        },
        customerNameList:[],
        // 接待人电话
        getdataListParm3: {
          name: 'getTelById',
          returntype: 'Map',
          parammaps: { id: "" }
        },
        receiverNameList: [] // 接待人
      },
      textMap: {
        create: '新增',
        see: '查看',
        update: '编辑'
      },

      requestParam: {},
      isokDisable: false,

      autoWidth: 130,
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },

  created() {
    this.get_table_data()
    this.get_select_list()
    this.get_auto_buttons()


    var rowData = this.$route.params.rowData
    var isHome = this.$route.params.isHome

    console.log("rowData",rowData)
    console.log("isHome",isHome)

    if(isHome == 'home'){
      this.rowData = rowData
      this.isHome = isHome
      this.form_add()
    } else {
      this.rowData = {}
      this.isHome = ""
    }


  },

  methods: {
    //获取按钮权限
    get_auto_buttons() {
      const Edit = 'TelephoneFollow'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
      console.log("isRoleEdit", isRoleEdit)


      const TelephoneAdd = 'business:telephone:add'
      const isTelephoneAdd = checkButtons(this.$store.state.user.buttons, TelephoneAdd)
      this.isTelephoneAdd = isTelephoneAdd

      //操作的宽度
      // var widthCount = calculativeWidth(isTelephoneAdd,isTelephoneAdd) + 90
      // this.autoWidth = widthCount

    },
    //获取所有下拉框的内容
    get_select_list() {
      GetDataByNames(this.send_select_list).then(response => {
        this.create.customerNameList = response.data.getCustomerSelect.list
        console.log("客户名称下拉框", this.create.customerNameList)
        this.groupNameList = response.data.getDictListSelect1.list
        console.log("集团下拉框", this.groupNameList)
        this.visiterNameList = response.data.getEmployeesSelect.list
        // console.log("回访人下拉框", this.visiterNameList)
        // this.receiverNameList = response.data.getContacts.list
        console.log("被回访人下拉框", this.receiverNameList)
        this.typeNameList = response.data.getDictListSelect2.list
        console.log("回访类型", this.typeNameList)

        this.nextNameList = response.data.getDictListSelect4.list
        console.log("下一步下拉框", this.nextNameList)

      })

    },

    //模糊查询客户
    customer_search(queryString, cb) {
      console.log('模糊查询输入值', queryString)
      GetDataByName({ name: 'getCustomerNameFuzzy', page: 1, offset: 1, pagecount: 10, returntype: 'Map', parammaps: { inputvalue: queryString } }).then(response => {
        this.customerNameList = response.data.list
        console.log("客户名称下拉框", response)
        cb(response.data.list)
      })
    },

    //清空客户选择
    clear_customer_select(item) {
      document.activeElement.blur()
      this.tableObj.getdataListParm.parammaps.customerId = ""
      this.tableObj.getdataListParm.parammaps.customerName = ""
    },

    //客户失去焦点
    customer_search_blur(item) {
      console.log("客户失去焦点")
      this.tableObj.getdataListParm.parammaps.customerId = ""
      this.tableObj.getdataListParm.parammaps.customerName = ""
      this.customerNameList = []
    },
    customer_search_blurTemp(item) {

      this.create.temp.customerId = ""
      this.create.temp.customerName = ""
      this.create.temp.groupName = ""
      this.customerNameList = []
      this.$forceUpdate();
    },


    //监听 - 查询下拉中客户名称
    changeCustomer0(item) {
      console.log('客户名称模糊查询选中值', item)
      this.tableObj.getdataListParm.parammaps.customerId = item.id
      this.tableObj.getdataListParm.parammaps.customerName = item.name


      var send_data = { name: 'getContacts', returntype: 'Map', parammaps: { customerId: item.id } }
      GetDataByName(send_data).then(response => {
        this.receiverNameList = response.data.list
        console.log("被回访人下拉框", this.receiverNameList)

      })
    },

    //加载表格
    get_table_data() {
      var me = this
      this.tableObj.listLoading = true
      console.log("this.tableObj.getdataListParm======>", this.tableObj.getdataListParm)
      GetDataByName(this.tableObj.getdataListParm).then(response => {
        console.log('table数据======>', response.data.list)
        if (response.data.list !== null) {

          response.data.list.map(function (i, j) {
            return (i.typeIdsArr = i.typeIds.split(","))
          })
          this.tableObj.list = response.data.list
          this.tableObj.pageNum = response.data.pageNum
          this.tableObj.pageSize = response.data.pageSize
          this.tableObj.total = response.data.total


          //计算操作栏宽度
          var btnNum = 0
          var arrList = response.data.list
          arrList.forEach(function(row){
              var itemBtnNum = 0
              if(me.isTelephoneAdd && row.isModify == '1' && row.createrId == me.employeid){
                itemBtnNum = itemBtnNum + 1
              }
              if(me.isTelephoneAdd && row.isDel  == '1' && row.createrId == me.employeid){
                itemBtnNum = itemBtnNum + 1
              }
              console.log(itemBtnNum)
              if(itemBtnNum > btnNum){
                btnNum = itemBtnNum
              }
          })
          console.log("btnNum=============",btnNum)
          me.autoWidth = btnNum * 90 + 90

        } else {
          this.tableObj.list = []
        }
        setTimeout(() => {
          this.tableObj.listLoading = false
        }, 100)
      })
    },
    form_search() {
      console.log('点击了查询')

      if (this.tableObj.getdataListParm.parammaps.inputDatetime !== '' && this.tableObj.getdataListParm.parammaps.inputDatetime !== null) {
        this.tableObj.getdataListParm.parammaps.beginDate = parseTime(this.tableObj.getdataListParm.parammaps.inputDatetime[0], '{y}-{m}-{d}')
        this.tableObj.getdataListParm.parammaps.endDate = parseTime(this.tableObj.getdataListParm.parammaps.inputDatetime[1], '{y}-{m}-{d}')
      } else {
        this.tableObj.getdataListParm.parammaps.inputDatetime = ''
        this.tableObj.getdataListParm.parammaps.beginDate = ''
        this.tableObj.getdataListParm.parammaps.endDate = ''
      }
      this.tableObj.getdataListParm.offset = 1
      this.get_table_data()
    },
    form_clear() {
      console.log('点击了重置')
      this.tableObj.getdataListParm.parammaps.customerName = ''

      this.tableObj.getdataListParm.parammaps.telVisitCode = ''
      this.tableObj.getdataListParm.parammaps.customerId = ''
      this.tableObj.getdataListParm.parammaps.groupId = ''
      this.tableObj.getdataListParm.parammaps.visiterId = ''
      this.tableObj.getdataListParm.parammaps.receiverName = ''

      this.tableObj.getdataListParm.parammaps.typeIds = ''
      this.tableObj.getdataListParm.parammaps.statusId = ''
      this.tableObj.getdataListParm.parammaps.beginDate = ''
      this.tableObj.getdataListParm.parammaps.endDate = ''
      this.tableObj.getdataListParm.parammaps.inputDatetime = ''
      this.tableObj.getdataListParm.offset = 1
      this.get_table_data()
    },

    // 新增
    form_reset() {
      this.create.temp = { customerId: '', customerName: '',groupId: '', groupName: '', tdate: parseTime(new Date(), '{y}-{m}-{d}'), visiterId: '', receiverId: '', receiverName: '', receiverTel: '', typeIds: '', typeIdsArr: [], statusName: '良好', remark: '', content: '', nextId: '', modifyName: 'add', receiverChoose: 'true' }
    },


    //监听 - 集团
    change_group(item) {
      console.log(item)
      console.log(this.create.temp.groupId)
      this.create.temp.groupId = item
      this.create.getdataListParmCust.parammaps.groupId = item

      this.create.temp.customerName = ""
      this.create.temp.customerId = ""
      this.create.customerNameList = []

      this.get_customer_list()
      this.$forceUpdate();

    },
// 弹窗中的客户
    get_customer_list() {
      GetDataByName(this.create.getdataListParmCust).then(response => {
        if (response.data.list !== null) {
          this.create.customerNameList = response.data.list
          console.log('弹窗中的客户数据', response.data.list)
        } else {
          this.create.customerNameList = []
        }
      })
    },


    //监听 - 下拉中客户名称
    change_customer(item) {
      console.log(item)
      this.create.temp.customerId = item

      this.get_groupName(item)
      this.create.getdataListParm.parammaps.customerId = item
      this.getReceiverNameList()

    },

    // 获取集团名称
    get_groupName(customerId) {
      var me = this
      var send_data = {
        name: 'getgroupNameById',  returntype: 'Map',  parammaps: {  id: customerId }
      }
      GetDataByName(send_data).then(response => {

        console.log(response)
        if (response.data.list !== null) {
           me.create.temp.groupName = response.data.list[0].groupName
           me.create.temp.groupId = response.data.list[0].groupId
        } else {
           me.create.temp.groupName = ""
           me.create.temp.groupId = ""
        }
      })
    },


    //监听 - 下拉中客户名称
    changeCustomer(item) {

      console.log(item)
      this.create.temp.customerId = item.id
      this.create.temp.customerName = item.name

       this.$forceUpdate();


      this.create.getdataListParm.parammaps.customerId = item.id
      this.getReceiverNameList()
      this.create.getdataListParm2.parammaps.id = item.id
      this.getGroupNameId()


    },

    clear_customer_selectTemp(item) {
      document.activeElement.blur()
      this.create.temp.customerId = ""
      this.create.temp.customerName = ""
      this.create.temp.groupName = ""
      this.$forceUpdate();
    },

    //监听 - 多选
    changeChecked(item) {
      this.create.temp.typeIds = item
      console.log('多选选中', this.create.temp.typeIdsArr)
    },
    // 集团名称
    getGroupNameId() {
      GetDataByName(this.create.getdataListParm2).then(response => {
        if (response.data.list !== null) {
          this.create.temp.groupName = response.data.list[0].groupName
          this.create.temp.groupId = response.data.list[0].groupId
          console.log('集团名称', response.data.list)
        }
      })
    },
    // 回访人
    getReceiverNameList() {
      GetDataByName(this.create.getdataListParm).then(response => {
        if (response.data.list !== null) {
          this.create.receiverNameList = response.data.list
          console.log('回访人数据', response.data.list)
        } else {
          this.create.receiverNameList = []
        }
      })
    },

    //监听 - 下拉中回访人
    changeReceiver(item) {
      this.create.getdataListParm3.parammaps.id = item
      this.create.temp.receiverName = this.create.receiverNameList.find(obj => obj.id === item).name
      this.getTelephoneName()
    },

    // 回访人电话
    getTelephoneName() {
      GetDataByName(this.create.getdataListParm3).then(response => {
        if (response.data.list !== null) {
          this.create.temp.receiverTel = response.data.list[0].telephone
          console.log('回访人电话', response.data.list)
        }
      })
    },

    //新增
    form_add() {
      console.log('点击了新增')
      this.form_reset()
      this.create.dialogStatus = 'create'
      this.create.dialogFormVisible = true

      this.$nextTick(() => {
      //需要执行的内容
        this.form_reset()
        //首页跳过来的新增，要带出客户信息
        if(this.isHome == "home"){
            this.create.temp.groupId = this.rowData.groupId
            this.create.temp.customerId = this.rowData.customerId
            this.create.temp.groupName = this.rowData.groupName
            this.create.temp.customerName = this.rowData.customerName
            // this.create.getdataListParmCust.parammaps.groupId = this.rowData.groupId
            // this.get_customer_list()
            this.create.getdataListParm.parammaps.customerId = this.rowData.customerId
            this.getReceiverNameList()
        }
      })


    },
    //新增保存
    add_dialog_save() {
      console.log('点击了新增保存',this.create)
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)


          this.requestParam.name = 'modifyTelvisit'
          this.create.temp.modifyName = "add"
          this.create.temp.id = ""
          this.create.temp.typeIds = this.create.temp.typeIdsArr.join(",")


          if (this.create.receiverNameList.find(obj => obj.id == this.create.temp.receiverId)) {
            console.log("receiverNameList下拉直接选的！")
            this.create.temp.receiverChoose = "true"
          } else {
            this.create.temp.receiverName = this.create.temp.receiverId
            this.create.temp.receiverChoose = "false"
            console.log("receiverNameList下拉自己填的！")
          }
          this.create.temp.createrId = Cookies.get('employeid')

          this.create.temp.receiverId2 = ""
          var send_data = {
            "common": { "returnmap": "0" },
            "data": [
              {
                "name": "insertContactsNew", "type": "e",
                "parammaps": {
                  "customerId": this.create.temp.customerId,
                  "contactName": this.create.temp.receiverName,
                  "telephone": this.create.temp.receiverTel,
                  "address": "",
                  "remark": this.create.temp.remark,
                  "receiverChoose": this.create.temp.receiverChoose
                }
              },
              {
                "name": "insertTelvisit",
                "resultmaps": {
                  "list": [this.create.temp]
                },
                "children": [{
                  "name": "insertTelvisit",
                  "type": "e",
                  "parammaps": {
                    "customerId": "@insertTelvisit.customerId",
                    "groupId": "@insertTelvisit.groupId",
                    "tdate": "@insertTelvisit.tdate",
                    "visiterId": "@insertTelvisit.visiterId",
                    "receiverId": "@insertTelvisit.receiverId",
                    "receiverId2": "@insertContactsNew.LastInsertId",
                    "receiverTel": "@insertTelvisit.receiverTel",
                    "typeIds": "@insertTelvisit.typeIds",
                    "statusName": "@insertTelvisit.statusName",
                    "remark": "@insertTelvisit.remark",
                    "content": "@insertTelvisit.content",
                    "nextId": "@insertTelvisit.nextId",
                    "createrId": "@insertTelvisit.createrId",
                    "receiverChoose": "@insertTelvisit.receiverChoose"
                  }
                }]
              }
            ]
          }
          console.log('新增保存发送参数 ', send_data)


          if (this.create.temp.typeIdsArr.length == 0) {
            this.$notify({ title: '失败', message: '回访类型必选！' + response.data, type: 'error', duration: 2000 })
          } else {
            ExecDataByConfig(send_data).then(response => {
              if (response.msg !== 'fail') {
                this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
                this.create.dialogFormVisible = false
                this.get_table_data()

                if(this.isHome == 'home'){
                  // 回到首页
                   this.$router.push({ name: 'Dashboard', params: { isTodo: 'telephone' }})
                }
              } else {
                this.$notify({ title: '失败', message: '保存失败' + response.data, type: 'error', duration: 2000 })
              }
            })


          }


        }
      })
    },
    // 查看
    form_see(row) {
      console.log('点击了查看', row)
      // this.$nextTick(() => {
      //   this.$refs.temp.resetFields()
      // })
      this.create.temp = Object.assign({}, row)
      this.create.getdataListParm.parammaps.customerId = row.customerId
      this.getReceiverNameList()

      this.create.getdataListParmCust.parammaps.groupId = row.groupId
      this.get_customer_list()

      this.create.temp.id = row.id
      this.create.temp.typeIdsArr = row.typeIdsArr.map(Number)
      this.create.dialogStatus = 'see'
      this.create.dialogFormVisible = true
    },


    // 编辑
    form_edit(row) {
      console.log('点击了编辑', row)
      this.create.temp = Object.assign({}, row)

      // this.create.getdataListParmCust.parammaps.groupId = row.groupId
      // this.get_customer_list()
      this.create.getdataListParm.parammaps.customerId = row.customerId
      this.getReceiverNameList()
      this.create.temp.id = row.id
      this.create.temp.typeIdsArr = row.typeIdsArr.map(Number)
      this.create.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },

    // 编辑保存
    form_edit_save() {
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['temp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)

          this.create.temp.modifyName = "update"
          this.requestParam.name = 'modifyTelvisit'
          this.create.temp.typeIds = this.create.temp.typeIdsArr.join(",")

          if (this.create.receiverNameList.find(obj => obj.id == this.create.temp.receiverId)) {
            console.log("receiverNameList下拉直接选的！")
            this.create.temp.receiverChoose = "true"
          } else {
            this.create.temp.receiverName = this.create.temp.receiverId
            this.create.temp.receiverChoose = "false"
            console.log("receiverNameList下拉自己填的！")
          }


          this.create.temp.receiverId2 = ""
          var send_data = {
            "common": { "returnmap": "0" },
            "data": [
              {
                "name": "insertContactsNew", "type": "e",
                "parammaps": {
                  "customerId": this.create.temp.customerId,
                  "contactName": this.create.temp.receiverName,
                  "telephone": this.create.temp.receiverTel,
                  "address": "",
                  "remark": this.create.temp.remark,
                  "receiverChoose": this.create.temp.receiverChoose
                }
              },
              {
                "name": "updateTelvisit",
                "resultmaps": {
                  "list": [this.create.temp]
                },
                "children": [{
                  "name": "updateTelvisit",
                  "type": "e",
                  "parammaps": {
                    "tdate": "@updateTelvisit.tdate",
                    "visiterId": "@updateTelvisit.visiterId",
                    "receiverId": "@updateTelvisit.receiverId",
                    "receiverId2": "@insertContactsNew.LastInsertId",
                    "receiverTel": "@updateTelvisit.receiverTel",
                    "typeIds": "@updateTelvisit.typeIds",
                    "statusName": "@updateTelvisit.statusName",
                    "remark": "@updateTelvisit.remark",
                    "content": "@updateTelvisit.content",
                    "nextId": "@insertTelvisit.nextId",
                    "receiverChoose": "@insertTelvisit.receiverChoose",
                    "id": "@insertTelvisit.id"

                  }
                }]
              }
            ]
          }
          console.log('编辑保存发送参数 ', send_data)


          if (this.create.temp.typeIdsArr.length == 0) {
            this.$notify({ title: '失败', message: '回访类型必选！' + response.data, type: 'error', duration: 2000 })
          } else {
            ExecDataByConfig(send_data).then(response => {
              if (response.msg !== 'fail') {
                this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
                this.create.dialogFormVisible = false
                this.get_table_data()
              } else {
                this.$notify({ title: '失败', message: '保存失败' + response.data, type: 'error', duration: 2000 })
              }
            })

          }

        }
      })
    },

    // 删除
    form_delete(row) {
      MessageBox.confirm('是否确认删除此信息?', {
        confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning'
      }).then(() => {

        this.requestParam.name = 'delTelvisit'
        this.requestParam.parammaps = {}

        this.requestParam.parammaps.id = row.id
        PostDataByName(this.requestParam).then(response => {
          if (response.msg === 'fail') {
            this.$notify({ title: '删除失败', message: response.data, type: 'warning', duration: 2000 })
          } else {
            this.$notify({ title: '成功', message: '删除成功', type: 'success', duration: 2000 })
            this.get_table_data()
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消删除' })
      })
    },

    dialog_close(){
      this.create.dialogFormVisible = false
      this.get_table_data()
      if(this.isHome == 'home'){
       // 返回首页
       this.$router.push({ name: 'Dashboard', params: { isTodo: 'telephone' }})
      }
    },

    // 导出
    form_export(item) {

       this.download.getdataListParm.parammaps = this.tableObj.getdataListParm.parammaps

       GetDataByName(this.download.getdataListParm).then(response => {
         if (response.data.list !== null) {
           this.download.list = response.data.list
         } else {
           this.download.list = []
         }
         var excelDatas = [
           {
             tHeader: ['序号', '回访单号', '客户名称', '客户集团', '回访日期', '回访人', '被回访人电话', '回访类型', '回访状态', '备注', '录入日期' ],
             filterVal: ['sort', 'telVisitCode', 'customerName', 'groupName', 'tdate', 'receiverTel', 'typeNames', 'statusName', 'remark', 'created'],
             tableDatas: this.download.list,
             sheetName: 'Sheet1'
           }
         ]
         json2excel(excelDatas, '拜访记录', true, 'xlsx')
       })

     },

    // 导入
    form_inport() {
      console.log('点击了导入')
    }

  }
}
</script>
<style lang="scss" scoped>
.search {
  padding-top: 10px;
  clear: both;
}
.table {
  margin-top: 10px;
}


/deep/ .el-input.is-disabled .el-input__inner {color: #000000!important;}
/deep/ .el-textarea.is-disabled .el-textarea__inner {color: #000000!important;}
/deep/ .el-radio__input.is-disabled+span.el-radio__label {color: #000000!important;}
/deep/ .el-checkbox__input.is-disabled+span.el-checkbox__label { color: #000000!important;}

</style>
