<template>
  <div class="organization">
    <el-row type="flex" class="row-bg">
      <el-col  class="add-organization">
        <i class="el-icon-circle-plus-outline" @click="dialogMemberFn()"></i>
        <i class="text" @click="dialogMemberFn()">添加人员</i>
      </el-col>
      <el-col class="organization-search">
        <el-form :inline="true" :model="formOrganization" class="demo-form-inline" >
          <el-form-item label="机构:">
             <el-input style="width:140px;" size="small" v-model="formOrganization.orgName" placeholder="请输入机构名称"></el-input>
          </el-form-item>
           <el-form-item label="姓名:">
            <el-input style="width:140px;" size="small" v-model="formOrganization.name" placeholder="请输入人员姓名"></el-input>
          </el-form-item>
          <el-form-item label="来源:">
            <el-select size="small" v-model="formOrganization.source" placeholder="请选择来源">
              <el-option v-for="(item, index) in sourceList" :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
         
          <el-form-item>
            <el-button size="small" type="primary" @click="serch()">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button size="small"  @click="reset()">取消搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-table class="rim" ref="multipleOrganization" :data="peoplesDtoList" tooltip-effect="dark" style="width: 100%">
      <el-table-column prop="name" label="人员" min-width="100" align="center"></el-table-column>
      <el-table-column prop="department" label="所属科室" align="center"></el-table-column>
      <el-table-column prop="hospital" label="所属医院" align="center"></el-table-column>
      <el-table-column prop="provinceName" label="所属省份" align="center"></el-table-column>
      <el-table-column prop="cityName" label="所属城市" align="center"></el-table-column>
      <el-table-column prop="countyName" label="所属区县" align="center"></el-table-column>
      <el-table-column prop="townName" label="所属街道" align="center"></el-table-column>
      <el-table-column prop="sourceName" label="来源" align="center"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="140">
        <template slot-scope="scope">
          <el-button @click="detel(scope.row)" type="text" size="small" style="color:red" v-if="scope.row.source != 1">删除</el-button>
        </template>
      </el-table-column>
      <div slot="empty">
        <div class="table-empty">
          <img src="../../../assets/image/no-content1.png">
          <p>没有人员信息</p>
        </div>
      </div>
    </el-table>
    <div class="pagination">
      <el-pagination background @size-change="handleSizeOrganization" @current-change="handleCurrentOrganization"
        :current-page="formOrganization.pageNum" :page-sizes="[10, 20, 40]" :page-size="formOrganization.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="totalPeople"></el-pagination>
    </div>
    <el-dialog
      title="添加人员"
      :visible.sync="dialogOrg"
      width="80%"
      top="5vh"
      :before-close="handleClose">
        <add-member
          v-if="dialogOrg"
          :circleId='circleId'
          :roleType='roleType'
          @addPeople="addPeople"
          @handleClose="handleClose">
        </add-member>
    </el-dialog>
    <!-- 删除 弹窗 -->
    <el-dialog
      title="确认删除"
      :show-close="false"
      :visible.sync="deleteVisible"
      :close-on-click-modal="false"
      width="600px"
      center
    >
      <p style="text-align: center;">{{detelMessage}}</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="confirmDelete">删 除</el-button>
        <el-button type="primary" @click="hideDeleteFrom">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as operationData from "@/utils/operation";
import { openLoading, closeLoading } from "@/utils/utils";
import { doUpload, getFilePath } from "@/utils/qiniu-util";
import { mpRelSearch, mprDeleteOrg } from '@/utils/yqrange/memberApi';
import addMember from './addMember.vue'
let vm = null;
export default {
   components: {
    addMember
  },
  props: {
    circleId: {
      type: Number | String,
      default: 1
    },
    roleType: {
      type: Number | String,
      default: 1
    },
  },
  data() {
    return {
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          if (node.level == 1) {
            let req = {
              provinceId: node.data.value
            };
            vm.GET("basic-data/position/cities", req).then(res => {
              let newData = vm.setMoreOption(res.data.cityList, "cities");
              resolve(newData);
            });
          } else if (node.level == 2) {
            let req = {
              cityId: node.data.value
            };
            vm.GET("basic-data/position/counties", req).then(res => {
              let newData = vm.setMoreOption(res.data.countyList, "counties");
              resolve(newData);
            });
          } else if (node.level == 3) {
            let req = {
              countyId: node.data.value
            };
            vm.GET("basic-data/position/towns", req).then(res => {
              let newData = vm.setMoreOption(res.data.townList, "towns");
              resolve(newData);
            });
          }
        }
      },
      administrativeValue: "",
      optionsRegion: [],
      sourceList: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '可访问本圈的机构',
          value: 1
        },
        {
          label: '手动添加',
          value: 2
        },
      ],
      peoplesDtoList: [],
      totalPeople: 10,
      selectionList:[],
      formOrganization: {
        name: '',
        orgName: '',
        source: 0,
        pageNum: 1,
        pageSize: 10
      },
      dialogOrg:false,
      deleteVisible: false,
      detelMessage: ''
    }
  },
  created() {
    vm = this;
    //Idtype:1,内部 2.外部
    vm.idType = localStorage.getItem("storageIdType");
    this.initRange();
  },
  mounted() {
    
   
  },
  methods: {
    initRange() {
      this.formOrganization.pageNum = 1;
      this.formOrganization.name = "";
      this.formOrganization.orgName = "";
      this.formOrganization.source = 0
      this.getList()
    },
    setMoreOption(data, type) {
      let option = [];
      for (let i = 0; i < data.length; i++) {
        let obj = data[i];
        if (type == "cities") {
          obj.label = data[i].cityName;
          obj.value = data[i].cityId;
        } else if (type == "counties") {
          obj.label = data[i].countyName;
          obj.value = data[i].countyId;
        } else if (type == "towns") {
          obj.label = data[i].townName;
          obj.value = data[i].townId;
          obj.leaf = true;
        }
        option.push(obj);
      }
      return option;
    },
    serch() {
       this.formOrganization.pageNum = 1;
       this.getList()
    },
    getList() {
      let idType = localStorage.getItem("storageIdType");
      let req = {
        circleId: this.circleId,
        countryFlag: true,
        name:  this.formOrganization.name.trim(),
        orgName: this.formOrganization.orgName.trim(),
        pageNo: this.formOrganization.pageNum,
        pageSize: this.formOrganization.pageSize,
        source: this.formOrganization.source,
      };
      openLoading(vm);
      mpRelSearch(req).then(res => {
        closeLoading(vm);
        if (res.code == "000000") {
          this.peoplesDtoList = res.data.peoplesDtoList;
          this.totalPeople = res.data.total;
          this.setPleTotal()
        } else {
          this.$message(res.message);
        }
      }).catch(err => {
        this.$message.error('请求失败');
      });
    },

    //重置机构、人员查询
    reset() {
      this.initRange()
    },
    //机构table修改
    handleSizeOrganization(val) {
      console.log(`每页 ${val} 条`);
      this.formOrganization.pageSize = val;
      this.getList();
    },
    handleCurrentOrganization(val) {
      //console.log(`当前页: ${val}`);
      this.formOrganization.pageNum = val;
      this.getList();
    },
    detel(item) {
      this.detelMessage = `删除后 "【${item.name}】" 将无法访问本圈, 确定删除么?`
      this.deleteVisible = true
      this.clickItem = item
    },
    confirmDelete() {
      let params = {
          circleId: this.circleId,
          docIds: [this.clickItem.id]
        }
        mprDeleteOrg(params).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.deleteVisible = false
          this.formOrganization.pageNum = 1;
          this.getList()
        }).catch(() => {
          this.$message.error('删除失败，请稍后重试');
        })
    },
    hideDeleteFrom() {
      this.deleteVisible = false
      this.detelMessage = ''
      this.clickItem = null
    },
    setPleTotal() {
      this.$emit('setPleTotal',this.totalPeople)
    },
    dialogMemberFn() {
      this.dialogOrg = true
    },
    handleClose() {
      this.dialogOrg = false
    },
    addPeople() {
     this.dialogOrg = false
     this.initRange()
    }
  }
}
</script>

<style lang="scss" scoped>
.organization {
  .add-organization {
    margin-left: 20px;
    line-height: 20px;
    width: 120px;
    cursor: pointer;
    padding-top: 10px;
    i {
      font-size: 18px;
      margin-right: 2px;
    }
    
  }
  .organization-search {
    flex: 1;
    margin-right: 10px;
    text-align: right;
  }
}
</style>
