<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="员工姓名" prop="companyUserNickName">
        <el-input
          v-model="queryParams.companyUserNickName"
          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 label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" 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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleBind"
          v-hasPermi="['company:companyVoiceCaller:bind']"
        >绑定坐席</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['company:companyVoiceCaller:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['company:companyVoiceCaller:remove']"
        >删除</el-button>
      </el-col> -->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  height="500" border v-loading="loading" :data="companyCallerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="callerId" />
      <el-table-column label="员工姓名" align="center" prop="companyUserNickName" />
      <el-table-column label="线路" align="center" prop="callerLine" />
      <el-table-column label="坐席号" align="center" prop="callerNo" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <!-- <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"  :type="scope.row.status==1?'success':'danger'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作"   width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['company:companyVoiceCaller: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="员工" prop="companyUserId">
          <el-select v-model="form.companyUserId"  filterable  placeholder="请选择员工" clearable size="small">
              <el-option
                @change="userChange"
                v-for="item in users"
                v-if="item.status !== '1'"
                :key="item.userId"
                :label="item.nickName"
                :value="item.userId"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="线路" prop="callerLine">
          <el-select v-model="form.callerLine"  @change="handleCallerLineChange"  placeholder="请选择线路" clearable size="small">
              <el-option
                v-for="item in callerApis"
                :key="item.apiId"
                :label="item.apiName"
                :value="item.apiId"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="坐席" prop="callerId" v-if="form.callerLine != null">
          <el-select v-model="form.callerId"    placeholder="请选择坐席" clearable size="small">
              <el-option
                v-for="item in callers"
                :key="item.callerId"
                :label="item.callerNo"
                :value="item.callerId"
              />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" maxlength="11" style="width: 215px" placeholder="请输入手机号" />
        </el-form-item>
         <!-- <el-form-item label="验证码" prop="code">
          <el-input v-model="form.code" maxlength="4" style="width: 215px" placeholder="请输入验证码" />
          <span v-if="codeShow" class="get-code"  @click="getSmsCode">获取验证码</span>
          <span v-if="!codeShow" class="get-code">{{ codeCount }}s</span>
        </el-form-item> -->
        <!-- <el-form-item label="坐度有效期" prop="userLimitTime">
          <el-date-picker clearable size="small" style="width: 215px"
            v-model="form.userLimitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择坐度有效期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="状态">
           <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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>
  </div>
</template>

<script>
import { bindCallerNo,listCompanyVoiceCaller, getCompanyVoiceCaller, delCompanyVoiceCaller, addCompanyVoiceCaller, updateCompanyVoiceCaller, exportCompanyVoiceCaller } from "@/api/company/companyVoiceCaller";
import { getUserList } from "@/api/company/companyUser";
import { getCallerList } from "@/api/company/companyVoiceCaller";
import {getCallerApiList} from "@/api/company/companyVoiceApi"

import { sendSmsCode } from "@/api/common";
export default {
  name: "CompanyCaller",
  data() {
    return {
      callerApis:[],
      callers:[],
      // codeShow: true, // 获取验证码样式
      // codeFlag:true,
      // codeCount: 60, // 验证码倒计时
      // codeText:"获取验证码",
      companys:[],
      users:[],
      statusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业用户坐席表格数据
      companyCallerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        userId: null,
        mobile: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: { 
        companyUserId: [
          { required: true, message: "员工不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        callerId: [
          { required: true, message: "坐席编号不能为空", trigger: "blur" }
        ],
        callerLine:[
          { required: true, message: "线路不能为空", trigger: "blur" }
        ]
        
        // code: [
        //   { required: true, message: "验证码不能为空", trigger: "blur" }
        // ],
        // userLimitTime: [
        //   { required: true, message: "坐席到期时间不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getDicts("company_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getList();
    this.getUserList();
  },
  methods: {
    // getSmsCode(){
    //   if(!this.codeFlag){
    //       return;
    //   }
    //   if(this.form.mobile==null){
    //      this.msgError("请输入手机号");
    //      return;
    //   }
    //   this.codeFlag=false;
    //   var that=this;
    //   var data={mobile:this.form.mobile}
    //   sendSmsCode(data).then(
    //     response => {
    //       if (response.code === 200) {
    //           that.getCode()
    //       }
    //       else{
    //         this.msgError(response.msg);
    //       }
    //     }
    //   ).catch(() => {
    //     this.codeFlag = true;
    //   });
    // },
    // // 获取验证码
    // getCode() {
    //     const TIME_COUNT = 60;
    //     var that=this;
    //     if (!this.timer) {
    //         this.codeCount = TIME_COUNT;
    //         this.codeShow = false;
    //         this.timer = setInterval(() => {
    //             if (this.codeCount > 0 && this.codeCount <= TIME_COUNT) {
    //                 this.codeCount--;
    //             } else {
    //                 that.codeFlag=true;
    //                 that.codeShow = true;
    //                 clearInterval(that.timer);
    //                 that.timer = null;
    //             }
    //         }, 1000)
    //     } 
    // },
    userChange(e){
      console.log(e)
    },
    handleCallerLineChange(){
      this.getCallerList();
    },

    getCallerList() {
      console.log("-------------------" + JSON.stringify(this.form))
      const param = {callerLine :this.form.callerLine}
      getCallerList(param).then(response => {
        this.callers = response.data;
      });
    },
    getCallerApiList() {
      const param = {status:1}
      getCallerApiList(param).then(response => {
        this.callerApis = response.rows;
      });
    },
    getUserList() {
      getUserList().then(response => {
        this.users = response.data;
      });
    },
    /** 查询企业用户坐席列表 */
    getList() {
      this.loading = true;
      listCompanyVoiceCaller(this.queryParams).then(response => {
        this.companyCallerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        callerId: null,
        companyId: null,
        companyUserId: null,
        mobile: null,
        remark: null,
        status: "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.callerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleBind() {
      this.reset();
      this.open = true;
      this.title = "员工坐席绑定";
      this.getCallerApiList();
      // this.getCallerList();
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   var that=this;
    //   this.reset();
    //   this.getCallerList();
    //   const callerId = row.callerId || this.ids
    //   getCompanyVoiceCaller(callerId).then(response => {
    //     this.form = response.data;
    //     this.form.nickName=row.companyUserNickName;
    //     this.form.status = response.data.status.toString();
    //     this.open = true;
    //     this.title = "修改员工坐席";
    //   });
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          bindCallerNo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              }
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const callerIds = row.callerId || this.ids;
      this.$confirm('是否确认删除坐席编号为"' + callerIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCompanyVoiceCaller(callerIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业用户坐席数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCompanyVoiceCaller(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};

</script>
<style scoped>
.get-code{
  display: inline-block;
  text-align: center;
  font-size: 14px;
  color: #0051ff;
  cursor: pointer;
}
</style>
