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

    <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="height: 93%">
      <el-row style="height: 100%">
        <el-col :span="12">
          <el-form-item
            label="选择组号"
            prop="groupId"
            :rules='[{ required: true, message: "请选择组号", trigger: "change" }]'
          >
            <el-select
              v-model="form.groupId"
              placeholder="请选择组号"
              size="small"
              style="width: 95%"
              @change="groupChange"
            >
              <el-option
                v-for="item in groups"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24" style="height: 100%">
          <div class="choice-title">
            <div class="choice-title-item">待选组员</div>
            <div class="choice-title-item">已选组员</div>
          </div>
          <div class="pipe-choice">
            <div class="pipe-choice-left">
              <div style="padding: 0px 1px 7px 0px;">
                <el-input
                  v-model="contactParam.username"
                  placeholder="姓名"
                  clearable
                  size="small"
                  style="width: 250px;margin-right: 10px"
                />
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-search"
                  @click="queryContactsList"
                >
                  搜索
                </el-button>
              </div>

              <el-table
                v-loading="loading"
                :data="contactsList"
                ref="pipeTable"
                @select="select"
                @select-all="selectAll"
              >
                <el-table-column type="selection"></el-table-column>
                <el-table-column label="姓名" align="left" prop="username" :show-overflow-tooltip="true" />
                <el-table-column label="电话" align="center" prop="phone" width="120" :show-overflow-tooltip="true"/>
                <el-table-column label="所在部门" align="center" prop="dept" :show-overflow-tooltip="true"/>
                <el-table-column label="单位职务" align="center" prop="job" :show-overflow-tooltip="true"/>
                <el-table-column label="应急职务" align="center" prop="urgentJob" :show-overflow-tooltip="true"/>
              </el-table>
            </div>

            <div class="pipe-choice-middle">
              <div>>></div>
            </div>

            <div class="pipe-choice-right">

              <div class="select-box">
                <div
                  class="pipe-select"
                  v-for="(contacts,index) in selectContactsInfoList"
                >
                  <el-tooltip class="item" effect="dark" :content="contacts.username" placement="bottom-start">
                    <div class="pipe-name">{{ contacts.username }}</div>
                  </el-tooltip>

                  <div class="pipe-delete" @click="deleteContact(contacts,index)">X</div>
                </div>
              </div>

              <div class="bottom-text">已选择{{selectContactsInfoList.length}}人</div>

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

    </el-form>

  </div>
</template>

<script>
import { listInfo, contactsInfoList, deleteTUrgentContactsGroupInfoByGroupId, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/urgent/group";
import { urgentContactsList } from "@/api/urgent/contacts";
export default {
  name: "Info",
  components: {
  },
  data() {
    return {
      groups: [],
      form: {
        groupId: "0"
      },
      rules: {},
      loading: false,
      contactsList: [],
      selectContactsInfoList: [],
      contactParam: {}
    };
  },
  created() {
    this.getContactsInfo();
    this.getDicts("t_urgent_contacts_group").then(res =>{
        this.groups = res.data;
    })
  },
  methods: {
    //回显选中人员
    initSelectContacts(){
      this.selectContactsInfoList = [];
      contactsInfoList({groupId:this.form.groupId}).then(res =>{
        if(res.code == 200){
          res.data.forEach(item =>{
              this.selectContactsInfoList.push(
                {
                  urgentContactsId: item.urgentContactsId,
                  username: item.username,
                  id: item.id
                }
              );
              let info = this.contactsList.find(contact => contact.id == item.urgentContactsId);
              this.$refs.pipeTable.toggleRowSelection(info,true);
          })
        }
      })
    },
    //获取紧急联系人
    getContactsInfo(){
      urgentContactsList().then(res =>{
          if(res.code == 200){
            this.contactsList = res.data;
            this.initSelectContacts();
          }
      })
    },
    //紧急联系查询
    queryContactsList(){
      urgentContactsList(this.contactParam).then(res =>{
        if(res.code == 200){
          this.contactsList = res.data;
          this.initSelectContacts();
        }
      })
    },
    //全选
    selectAll(all){
      //删除所有
      if (all.length == 0 || (all.length == 1 && [0] == undefined)){
        this.$refs["form"].validate(valid => {
          if(valid){
            deleteTUrgentContactsGroupInfoByGroupId({groupId:this.form.groupId}).then(res =>{
              if(res.code == 200){
                this.selectContactsInfoList = [];
              }
            })
          }
        })

      }
      //全选
      if(all.length >0){
        all.forEach((item) => {
          let index = this.selectContactsInfoList.findIndex(contactsItem => contactsItem.urgentContactsId ==  item.id);
          if(index == -1){
            this.$refs["form"].validate(valid => {
              if(valid){
                addInfo({groupId:this.form.groupId,urgentContactsId:item.id}).then(res =>{
                  this.selectContactsInfoList.push(
                    {
                      urgentContactsId: item.id,
                      username: item.username,
                      id: res.data.id
                    }
                  )
                });
              }
            })
          }
        })
      }

    },
    //选中
    select(all,row){
      let index = this.selectContactsInfoList.findIndex(item => item.urgentContactsId == row.id);
      if(index >= 0) {
        delInfo(this.selectContactsInfoList[index].id).then(res =>{
          if(res.code == 200){
            this.selectContactsInfoList.splice(index, 1);
          }else{
            this.$refs.pipeTable.toggleRowSelection(row,true);
          }
        }).catch(() => {
          this.$refs.pipeTable.toggleRowSelection(row,true);
        });
      }else{
        this.$refs["form"].validate(valid => {
          if(valid){
            addInfo({groupId:this.form.groupId,urgentContactsId:row.id}).then(res =>{
                this.selectContactsInfoList.push(
                  {
                    urgentContactsId: row.id,
                    username: row.username,
                    id: res.data.id
                  }
                )
            });
          }
        })

      }
    },
    deleteContact(contact,index){
      delInfo(contact.id).then(res =>{
          if(res.code == 200){
            this.selectContactsInfoList.splice(index, 1);
            let item = this.contactsList.find(item => item.id == contact.urgentContactsId);
            this.$refs.pipeTable.toggleRowSelection(item,false);
          }
      })
    },
    //切换组号
    groupChange(){
      this.getContactsInfo();
    }
  }
};
</script>
<style lang="scss" scoped>
  .app-container{
    height: calc(100vh - 84px);
  }

  .pipe-choice{
    display: flex;
    padding: 0px 10px 5px 10px;
    height: 100%;
  }

  .choice-title{
    display: flex;
    .choice-title-item{
      flex: 1;
      padding: 0px 0px 6px 12px;
      font-size: 13px;
      color: #82848a;
    }
  }

  .pipe-choice-left{
    border: 1px solid #e5e6eb;
    width: 48%;
    height: 100%;
    padding: 13px;
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 3px;
      background:  #dfe4ed;
      position: absolute;
      top: 0;
    }
    &::-webkit-scrollbar-thumb {
      /*滚动条里面小方块*/
      // border-radius: 10px;
      // box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: #cccccc;
      border-radius: 5px;
    }
  }

  .pipe-choice-middle{
    height: 100%;
    width: 4%;
    align-items: center;
    justify-content: center;
    display: flex;
    color: #1890ff;
  }

  .pipe-choice-right{
    border: 1px solid #e5e6eb;
    height: 100%;
    width: 48%;
    .select-box{
      padding: 10px;
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      overflow-y: auto;
      height: 94%;
      width: 100%;
      &::-webkit-scrollbar {
        width: 3px;
        background:  #dfe4ed;
        position: absolute;
        top: 0;
      }
      &::-webkit-scrollbar-thumb {
        /*滚动条里面小方块*/
        // border-radius: 10px;
        // box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #cccccc;
        border-radius: 5px;
      }
      .pipe-select{
        border: 1px solid #a3d3ff;
        border-radius: 3px;
        height: 40px;
        width: 48%;
        display: flex;
        padding: 5px;
        margin: 3px;
        color: #3d3d3d;
        font-size: 12px;
        .pipe-name{
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          line-height: 26px;
          width: 90%;
          text-align: center;
        }
        .pipe-delete{
          line-height: 26px;
          cursor: pointer;
        }
      }
    }

    .bottom-text {
      padding: 8px 5px 0px 0px;
      font-size: 14px;
      height: 20px;
      line-height: 20px;
      text-align: right;
      color: #1890ff;
    }
  }
</style>
