<template>
  <!-- 分配任务（可做公共组件） -->
    <div>
        <div>
          <el-input v-model="queryParams.name" placeholder="请输入名字" style="margin-bottom: 10px; width: 200px; margin-right: 10px;"></el-input>
          <el-button type="primary" @click="getList">🔍</el-button>
        </div>
        <el-table
				:data="tableData"
				v-loading="loading"
				style="width: 100%"
				border>
				<el-table-column align="center" width="80px">
					<template slot-scope="scope" >
						<el-checkbox-group v-model="check">
							<el-checkbox :label="scope.row.id" @change="handleCheckAllChange(scope.row)" ><span></span></el-checkbox>
						</el-checkbox-group>
					</template>
				</el-table-column>
				<el-table-column prop="name" label="人员名称" align="center" show-overflow-tooltip>	</el-table-column>
				<!-- <el-table-column prop="personnelEmail" label="Email" align="center" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="tenantName" label="所属租户" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="email" label="邮箱" align="center" show-overflow-tooltip></el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination
					:current-page.sync="queryParams.current"
					:page-size="queryParams.size"
					:total="total"
					@current-change="handleCurrentChange"
					layout="total, prev, pager, next, jumper"
					background
				></el-pagination>
			</div>
      <div style="display:flex;justify-content:flex-end">
        <el-button @click="handleClose2">取 消</el-button>
        <el-button type="primary" @click="submitPerson" >确 定</el-button>
      </div>
    </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  props: ["checkS"],
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10,
        name: '',
        // departmentIds: []
      },
      total: 0,
      loading: false,
      tableData: [],
      check:[],
      allCheck: this.checkS?JSON.parse(JSON.stringify(this.checkS)):[],
    }
  },
  computed: {
    ...mapGetters(['userinfo'])
  },
  mounted() {
    this.check = []
    if (this.allCheck.length > 0) {
      console.log(1)
      var a = []
      for (let i = 0; i < this.allCheck.length; i++){
        a.push(this.allCheck[i].id)
      }
     this.check = a
     this.queryParams.notInPersonIds = a
    }
    this.getList()
    
  },
  watch: {
    "checkS"(val) {
      this.allCheck = JSON.parse(JSON.stringify(val))
      this.check = [] 
      var a = []
      for (let i = 0; i < this.allCheck.length; i++){
        a.push(this.allCheck[i].id)
      }
      this.check = a
      this.queryParams.notInPersonIds = a
      this.getList()
      console.log(this.check)
    },
  },
  methods: {
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      console.log(1)
      // this.queryParams.tenant = this.userinfo.tenantId
      this.$api.getPartPersonnel(this.queryParams)
        .then((response) => {
          this.tableData = response.data;
          this.total = response.pageColumn.total;
          this.loading = false;
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    handleCheckAllChange(row) {
      let a=this.allCheck.findIndex((item) => {
        return item.id==row.id
      })
      if (a!= -1) {
        this.allCheck.splice(a,1)
      }else {
        row.patentNos=[]
        row.isChoose = false
        row.isEdit = false 
        row.assignCount = 0
        row.vVisible = true
        row.personId = row.id
        this.allCheck.push(row)
        
      }
      
      console.log(this.allCheck)
    },
    submitPerson(){
      if(this.allCheck.length>0){
        this.$emit('checked', JSON.parse(JSON.stringify(this.allCheck)))
        this.allCheck = []
        this.check = []
      }else{
        this.$confirm('您未选择, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(() => {
          this.$emit('checked', this.allCheck)
        }).catch(() => {
                 
        });
      }
      this.queryParams.name = ''
      this.getList()
    },
    handleClose2(){
      this.allCheck = this.checkS?JSON.parse(JSON.stringify(this.checkS)):[]
      this.check = []
      this.$emit('checked', this.allCheck)
      this.queryParams.name = ''
      this.getList()
    }
  },
}
</script>