<template>
    <d2-container>
        <div v-if="isExpend" class="d2-fl d2-tree-view">
            <el-tree
                    ref="tree"
                    highlight-current
                    :data="treeData"
                    default-expand-all
                    :props="defaultProps"
                    @node-click="handleNodeClick"
            >
        <span class="icon-tree" slot-scope="{ node, data }">
          <div v-if="data.alias == 'top'">
          	<i class="fa fa-university"></i>
          	<span>&nbsp;{{node.label}}</span>
          </div>
          <div v-if="data.alias == 'JG001'">
          	<i class="fa fa-building"></i>
          	<span>&nbsp;{{node.label}}</span>
          </div>
             <div v-if="data.alias == 'JG002'">
          	<i class="fa fa-building"></i>
          	<span>&nbsp;{{node.label}}</span>
          </div>
            	<div v-if="data.alias == 'JG003'">
				 		 				<i class="fa fa-users"></i>
				 		 				<span>&nbsp;{{node.label}}</span>
				 		 			</div>
            <!--<div v-if="data.alias == 'user'">-->
            <!--<i class="fa fa-users"></i>-->
            <!--<span>&nbsp;{{node.label}}</span>-->
            <!--</div>-->
        </span>
            </el-tree>
        </div>
        <div
                @click="isExpend = !isExpend"
                :class="[{'angle-view':isExpend},{'angle-view-none':!isExpend}]"
        >
            <i :class="['fa', {'fa-angle-left':isExpend},{'fa-angle-right':!isExpend}]"></i>
        </div>
        <div @click="isExpend = !isExpend" :class="[{'angle-view':isExpend},{'angle-view-none':!isExpend}]">
            <i :class="['fa', {'fa-angle-left':isExpend},{'fa-angle-right':!isExpend}]"></i>
        </div>
        <div :class="[{'d2-fl':isExpend},{'d2-table-view':isExpend},{'d2-table-view-full':!isExpend}]" style="overflow: hidden">
            <div class="d2-table-view-header">
                <el-form size="small" :inline="true" :model="filter" @keyup.enter.native="handleList" class="demo-form-inline" labelPosition="right">
                    <el-form-item label="房间号">
                        <el-input v-model.trim="filter.userNo" placeholder="人员编号" clearable/>
                    </el-form-item>
                    <el-form-item label="所属楼栋">
                        <el-input v-model.trim="filter.userNm" placeholder="姓名" clearable/>
                    </el-form-item>
                    <el-form-item label="入住类型">
                        <el-select v-model="filter.lssued" style="width: 100%" clearable>
                            <el-option label="下发" value="1"></el-option>
                            <el-option label="未下发" value="0"></el-option>
                        </el-select>
                    </el-form-item>
                    <!--<el-form-item label="所在楼层">-->
                        <!--<el-select v-model="filter.lssued" style="width: 100%" clearable>-->
                            <!--<el-option label="下发" value="1"></el-option>-->
                            <!--<el-option label="未下发" value="0"></el-option>-->
                        <!--</el-select>-->
                    <!--</el-form-item>-->
                    <el-form-item>
                        <el-button type="primary" @click="handleList('1')">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <div class="d2-table-view-content">
                <div class="d2-table-view-handle">
                    <!--<el-button @click="operVisible = !operVisible" size="small" icon="el-icon-plus">新增</el-button>-->
                    <label slot="headerButton" for="upload-input_1">
                        <span   class="import-button">批量导入</span>
                    </label>
                    <el-button  size="small" @click="onMutilDelClick">批量退宿</el-button>
                </div>
                <d2-crud
                        title="入住管理"
                        ref="d2Crud"
                        :options="tableOptions"
                        :loading="loading"
                        :columns="columns"
                        :data="data"
                        :rowHandle="rowHandle"
                        @edit="handleRowEdit"
                        @delete="handleRowRemove"
                        @exchange="handleExchangeRoom"
                        selection-row
                        @selection-change="handleSelectionChange"
                >
                </d2-crud>
            </div>
            <div class="d2-table-view-footer">
                <el-pagination
                        @current-change="handleCurrentChange"
                        @size-change="handleSizeChange"
                        :current-page.sync="currentPage"
                        :page-size="tableOptions.pageList[0]"
                        :page-sizes="tableOptions.pageList"
                        layout="total,sizes, prev, pager, next,jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
        <el-dialog
                :title="diaLogName"
                :visible.sync="operVisible"
                @close="clearForm"
                width="50%"
                :close-on-click-modal="false"
        >
            <el-form ref="form" :model="form" label-width="80px" labelPosition="right" :rules="rules">
                <el-row :gutter="15">
                    <el-col :span="20">
                        <el-form-item label="房间号" prop="userNo">
                            <el-input v-model.trim="form.userNo" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20">
                        <el-form-item label="所属楼栋" prop="userNo">
                            <el-input v-model.trim="form.userNo" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20">
                        <el-form-item label="入住性别" prop="userNm">
                            <el-input v-model.trim="form.userNm" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20">
                        <el-form-item label="入住类型" prop="userNo">
                            <el-input v-model.trim="form.userNo" disabled/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20">
                        <el-form-item label="入住人员" >
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-for="(item,index) in form.checkInList" :key="index">
                        <el-form-item :label="item.label">
                            <department-tree  ref="saffTree"  :str="item.name" v-model="item.value" :isClear="isClear" @clearApartment="clearApartment(index)"></department-tree>
                        </el-form-item>
                    </el-col>
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="2号">-->
                            <!--<staffcard-tree  ref="saffTree2" :depType="type" :strr="form.card2" v-model="form.card2"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="3号">-->
                            <!--<staffcard-tree  ref="saffTree3" :depType="type" :strr="form.card3" v-model="form.card3"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="4号">-->
                            <!--<staffcard-tree  ref="saffTree4" :depType="type" :strr="form.card4" v-model="form.card4"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="5号">-->
                            <!--<staffcard-tree  ref="saffTree5" :depType="type" :strr="form.card5" v-model="form.card5"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="6号">-->
                            <!--<staffcard-tree  ref="saffTree6" :depType="type" :strr="form.card6" v-model="form.card6"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="7号">-->
                            <!--<staffcard-tree  ref="saffTree7" :depType="type" :strr="form.card7" v-model="form.card7"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <!--<el-col :span="12">-->
                        <!--<el-form-item label="8号">-->
                            <!--<staffcard-tree  ref="saffTree8" :depType="type" :strr="form.card8" v-model="form.card8"></staffcard-tree>-->
                        <!--</el-form-item>-->
                    <!--</el-col>-->
                    <el-col :span="24">
                        <el-form-item label="备注" prop="userNm">
                            <el-input v-model.trim="form.userNm"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
        <zl-button :loading="staLoading" @click="operInstance(2)">保存</zl-button>
      </span>
        </el-dialog>
        <input id="upload-input_1" style="display: none" type="file"
               multiple
               ref="upload_1"
               name="file"
               @change="upload_1($event)">
        <input id="upload-input" style="display: none" type="file"
               multiple
               ref="upload"
               name="file"
               accept = "image/*"
               @change="upload($event)">
        <el-dialog
                :title="diaLogName1"
                :visible.sync="operVisible1"
                @close="clearForm1"
                width="40%"
                :close-on-click-modal="false">
            <div class="double-tree">
                <div>
                    <div>选择寝室</div>
                    <el-tree :data="orgTree"
                             default-expand-all
                             :props="defaultProps"
                             node-key="id"
                             ref="orgTree"
                             :check-strictly="true"
                             style="height: 400px;overflow: auto;padding: 20px"
                             show-checkbox
                             @check-change="handleNodeClick1">
									<span class="icon-tree" slot-scope="{ node, data }">
										<div v-if="data.alias == 'top'">
											<i class="fa fa-university"></i>
											<span>&nbsp;{{node.label}}</span>
										</div>
										<div v-if="data.alias == 'JG001'">
											<i class="fa fa-building"></i>
											<span>&nbsp;{{node.label}}</span>
										</div>
										<div v-if="data.alias == 'JG002'">
											<i class="fa fa-users"></i>
											<span>&nbsp;{{node.label}}</span>
										</div>
										 <div v-if="data.alias == 'user'">
												<i class="fa fa-cogs"></i>
												<span>&nbsp;{{node.label}}</span>
										</div>
								</span>
                    </el-tree>
                </div>
            </div>

            <span slot="footer" class="dialog-footer">
    <zl-button @click="operInstance1()"/>
  </span>
        </el-dialog>
    </d2-container>
</template>

<script>
  import { Loading } from 'element-ui';
  import { tableOptions } from '../../../assets/options'
  import {treeNodeAll} from '@/api/face/record'
  import {orgPersonTree} from '@/api/face/userRights'
  import {getFaceManageList,getPaIdOption,uploadImagess,uploadFaceImg,getCreateFaceMessage,saveOrUpdataFaceImage,deleteFaceMess} from '@/api/face/faceManage'
  export default {
    name: 'system-merchant',
    data () {
      let checkUserNo = (rule, value, callback) => {
        let reg = /^\w+$/;
        if (!reg.test(value)) {
          return callback(new Error("请输入由数字或字母组成的正确编号"));
        } else {
          callback();
        }
      };
      return {
        isClear:true,
        orgTree:[],
        staLoading:false,
        diaLogName:'编辑入住信息',
        diaLogName1:"宿舍调动",
        operVisible:false,
        operVisible1:true,
        time:'',
        falg:true,
        leaveOverId:[],
        selection:[],
        disabled:false,
        isExpend:true,
        currentPage:1,
        tableOptions,
        loading: false,
        filter: {
          userNo: '',
          userNm: '',
          depId:'',
          lssued:"",
          paId:"",
          pageSize: 10,
          pageNum: 1
        },
        form:{
          checkInList:[{label:"1号",name:"郑飞",value:"35143543645"},{label:"2号",name:"发嘀咕",value:"5773565"}],
          faceUserId:"",
          face1:"",
          face2:"",
          face3:"",
          userId:"",
          userNo:"",
          userNm:"",
          lssued:''
        },
        form1:{},
        pagination: {
          total: 0
        },
        columns: [
          {
            title: '房间号',
            key: 'userNo',
            minWidth: '120'
          },
          // {
          //   title: '房间名',
          //   key: 'userNm',
          //   minWidth: '120',
          //   showOverflowTooltip: true
          // },
          {
            title: '所属楼栋',
            key: 'depNm',
            minWidth: '120',
            // formatter (row, column, cellValue, index) {
            //   if (cellValue === '1') {
            //     return '人脸识别'
            //   } else if (cellValue === '2') {
            //     return '卡识别'
            //   }else if (cellValue === '3') {
            //     return '人脸+卡识别'
            //   }
            // }
          },
          {
            title: '入住性别',
            key: 'lssued',
            minWidth: '90',
            // formatter (row, column, cellValue, index) {
            //   if (cellValue === '0') {
            //     return '未下发'
            //   } else if (cellValue === '1') {
            //     return '下发'
            //   }
            // }
          },
          {
            title: '入住类型',
            key: 'lssuedTime',
            minWidth: '120'
          },
          {
            title: '入住人员',
            key: 'paName',
            minWidth: '220'
          },
          {
            title: '备注',
            key: 'extIdCard',
            minWidth: '180'
          }
        ],
        rowHandle: {
          columnHeader: '操作',
          minWidth: '240',
          align: 'center',
          custom: [
            {
              text: '编辑',
              type: 'primary',
              size: 'small',
              emit: 'edit'
            }, {
              text: '调寝',
              type: 'primary',
              size: 'small',
              emit: 'exchange'
            },
            {
              text: '退宿',
              type: 'primary',
              size: 'small',
              emit: 'delete'
            }
          ]
        },
        currentParentId:'',
        treeData:[],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        data: [],
        rules:{

        }
      }
    },
    methods: {
      clearApartment(val){
        this.$set(this.form.checkInList[val],'value','')
        this.$set(this.form.checkInList[val],'name','')
      },
      handleNodeClick1(data, checked, node) {
        if(checked === true) {
          this.checkedId = data.id;
          this.$refs.orgTree.setCheckedKeys([data.id]);
        } else {
          if (this.checkedId == data.id) {
            this.$refs.orgTree.setCheckedKeys([data.id]);
          }
        }
      },
      operInstance() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true
            // console.log(this.form)
            saveOrUpdataFaceImage(this.form).then(res => {
              this.loading=false
              this.$message({
                message:'操作成功',
                type: 'success'
              })
              this.operVisible=false
              this.handleTreeNode()
            }).catch(err => {
              this.loading=false
            })

          }
        })
      },
      operInstance1 () {
        let temp = this.$refs.orgTree.getCheckedKeys()
        let arr = []
        temp.forEach((item,index)=>{
          arr.push(item)
        })
        this.form1.userIds = arr.map(item => item).join(',')
        if(this.form1.userIds === ''){
          this.$message({
            message: '请选择员工',
            type: 'warning'
          })
          return false
        }
        // giveRightsSave (this.form1).then(res=>{
        //   this.operVisible1=false
        //   this.$message({
        //     message: '批量授权成功',
        //     type: 'success'
        //   })
        //   this.clearForm1()
        //   setTimeout(()=>{
        //     this.handleTreeNode()
        //   },5000)
        // })
      },
      // upload_1 (e) {
      //   //this.files= []
      //   let files = e.target.files
      //   let formData = new FormData()
      //   for (let k in files) {
      //     if (files.hasOwnProperty(k)) {
      //       formData.append('file', files[k])
      //     }
      //   }
      //   let loadingInstance_2=Loading.service({ fullscreen: true,text:'导入中...',background:"rgba(0, 0, 0, 0.8)" })
      //   uploadImagess(formData).then(res => {
      //     this.$message({
      //       message: '导入成功',
      //       type: 'success'
      //     })
      //     this.$nextTick(() => {
      //       loadingInstance_2.close();
      //     });
      //     this.handleList()
      //   }).catch(err=>{
      //     this.$nextTick(() => {
      //       loadingInstance_2.close();
      //     });
      //   })
      //
      //   this.$refs.upload_1.value = null
      // },

      handleNodeClick(data) {
        if(data.alias !=='top'){
          this.falg = true
          this.filter.depId = data.id
        }else{
          this.falg = false
          this.filter.depId = ''
        }
        this.handleList()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleTreeNode () {
        treeNodeAll({
          depType: ''
        }).then(res => {
          this.treeData = JSON.parse(res.msgDat)
          // this.form.orgId = this.treeData[0].id
        })
        this.handleList()
      },

      handleList (val) {
        if(val){this.filter.pageNum=1
          this.currentPage = 1}
        this.loading = true
        getFaceManageList(this.filter).then(res => {
          this.loading = false
          this.data = res.page.list
          this.pagination.total = res.page.total
        }).catch(err => {
          this.loading = false
        })
      },

      handleCurrentChange (val) {
        this.filter.pageNum = val
        this.handleList()
      },

      handleSizeChange (val) {
        this.filter.pageNum = 1
        this.filter.pageSize = val
        this.handleList()
      },
      handleRowEdit ({index, row}) {
        // this.diaLogName = '编辑入住信息'
        // this.form.userId=row.userId
        // teacherById({userId:this.form.userId}).then(res => {
        //   this.form = res.update
        this.operVisible = true
        // })

      },
      // 调寝
      handleExchangeRoom(){

      },
      onMutilDelClick(){
        if(!this.multipleSelection.length){
          this.$message({
            message: '请选择记录',
            type: 'warning'
          })
        }else{
          this.$confirm(`确认对应宿舍全体人员退宿?`, '确认操作', {
            confirmButtonText: '确定',
            cancelButtonText: '放弃',
            type: 'warning'
          }).then(() => {
            this.areaIds=[]
            this.paIds=[]
            this.multipleSelection.forEach((item,index)=>{
              this.areaIds.push(item.userId)
              this.paIds.push(item.paIds)
            })
            this.areaIdss=this.areaIds.join(",")
            this.paIdss=this.paIds.join(",")
            // deleteRights({userIds:this.areaIdss,paIds:this.paIdss,status:2}).then(res=>{
            //   this.$message({
            //     message: '撤销成功',
            //     type: 'success'
            //   })
            //   setTimeout(()=>{
            //     this.handleTreeNode()
            //   },5000)
            // })
          })
        }
      },
      handleRowRemove ({index, row}) {
        this.$confirm(`确认对应宿舍全体人员退宿`, '确认操作', {
          confirmButtonText: '确定',
          cancelButtonText: '放弃',
          type: 'warning'
        }).then(() => {
          let faceUserId = row.faceUserId
          deleteFaceMess({
            faceUserId:faceUserId,lssued:row.lssued
          }).then(res=>{
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.handleList ()
          })
        })
      },
      clearForm () {
        if (this.$refs['form']!==undefined) {
          this.$refs['form'].resetFields()
        }
        this.$nextTick(()=>{
           this.$refs.saffTree.cleanName()
        })
        this.form= {}
      },
      clearForm1 () {
        if (this.$refs['form1']!==undefined) {
          this.$refs['form1'].resetFields()
        }
        this.form1={
          userIds:"",
          paIds:'',
          status:1
        }
        this.$refs.orgTree.setCheckedKeys([])
      },
    },
    mounted () {
      orgPersonTree({userId:''}).then(res=>{
        this.orgTree = JSON.parse(res.msgDat)
      })
      this.tableOptions = this.$calcTableHeight(tableOptions,90)
      this.filter.pageSize = this.tableOptions.pageList[0]
      this.handleTreeNode()
    }
  }
</script>