<template>
  <div  >
    <el-row :gutter="20">
      <!--部门数据-->
      <!-- <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="true" :filter-node-method="filterNode" ref="tree"  @node-click="handleNodeClick" />
        </div>
      </el-col> -->

      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="公司姓名" prop="companyName">
            <el-input v-model="queryParams.companyName" placeholder="请输入公司姓名" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="公司电话" prop="phonenumber">
            <el-input v-model="queryParams.phonenumber" placeholder="请输入手机号码" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="创建时间">
            <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item> -->
          <el-form-item>
            <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button icon="el-icon-add" size="mini" @click="addCompany">添加公司</el-button>
          </el-form-item>
        </el-form>
        <div  class="tags">
          <el-tag closable @close="delCompany(index)" style="margin-bottom:5px;" v-for="(item,index) in companys"  >{{item.companyName}}</el-tag>
        </div>
        <el-table  ref="multipleTable"  v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="ID" width="50px" align="center" prop="companyId" :show-overflow-tooltip="true" />
          <el-table-column label="公司名称" width="220"  align="center" prop="companyName" :show-overflow-tooltip="true" />
          <el-table-column label="公司联系人" align="center" prop="linkName" :show-overflow-tooltip="true" />
          <el-table-column label="公司电话" align="center" prop="companyMobile" width="120" />
          <el-table-column label="今日分配客户数" align="center" prop="nowDayCustomerCount"   />
          <el-table-column label="创建时间"  sortable align="center" prop="createTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        
        <div class="btns">
          <el-button type="primary" @click="submit"  >确 定</el-button>
        </div>
        </el-col>
    </el-row>
  </div>
</template>

<script>
import { crmDayCountlist } from "@/api/company/company";
import { treeselect } from "@/api/company/companyDept";
export default {
  name: "Compnay",
  
  data() {
    return {
      selectCompany:[],
      companys:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司表格数据
      companyList: [],
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      hasClickClose:false,
      // 用户导入参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    submit(){
      this.addCompany();
      this.$emit('selectCompany',this.companys);
    },
    itemChcek(){
        let that=this;
        setTimeout(() => {
              var companyIds=[];
              for (let index = 0; index < this.companys.length; index++) {
                 const compnay = this.companys[index];
                 companyIds.push(compnay.companyId);
              }
              that.companyList.forEach(row => {
                  if(companyIds.indexOf(row.companyId)!=-1){
                      that.$refs.multipleTable.toggleRowSelection(row, true);
                  }
              });

        }, 0);
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      let that=this;
      crmDayCountlist(this.queryParams).then((response) => {
            this.companyList = response.rows;
            this.total = response.total;
            this.loading = false;
            that.itemChcek();
        }
      );
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
     
     
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectCompany=selection;
      this.ids = selection.map((item) => item.companyId);
    },
    addCompany(){
      var that=this;
      this.selectCompany.forEach(element => {
        var flag= that.companys.some(item => item.companyId === element.companyId)
        if(!flag){
           this.companys.push(element);
        }
      });
    },
    delCompany(index){
      this.companys.splice(index,1)
    }
  },
};
</script>
<style >
 
.btns{
  padding-top: 15px;
  display: flex;
  align-content: center;
  justify-content: flex-end;
}
</style>