export default {
  computed: {
    // 任务类型
    unitTypeList() {
      return this.$store.getters['dataArr/getUnitTypeList'];
    },
    // 检查问题类型
    askList(){
      return this.$store.getters['dataArr/getAskList'];
    },
    // 获取人员
    userData(){
      return this.$store.getters['dataArr/getUserList'];
    },
    // 获取部门
    depList(){
      return this.$store.getters['dataArr/getDepList'];
    }
  },
  data() {
    return {
      // 人员数组
      mix_unitTypeList:[],
      userdatabefore:[],
      userdata:[],
      departmentList:[],
    }
  },
  created(){
    this.getUnitList()
    // console.log("this.unitTypeList",this.unitTypeList)
  },
  methods: {
    // 获取任务类型
    getUnitList() {
      this.$store.dispatch('dataArr/getUnitTypeListApi').then(()=>{
        // patrol中的检查的任务
         // this.getMissonList()
         this.mix_unitTypeList = this.$store.getters['dataArr/getUnitTypeList']
         console.log("this.mix_unitTypeList",this.mix_unitTypeList)
      }).catch(res=>{
        console.log(res)
      })
    },
    // 获取检查问题类型
    getAsk() {
      this.$store.dispatch('dataArr/getAskListApi')
    },
    // 获取部门
    getDep() {
      this.$store.dispatch('dataArr/getDepListApi')
    },
    // 获取人员 处理为按部门排序划分的 树形结构
    getUser() {
      this.$store.dispatch('dataArr/getUserListApi').then(()=>{
        this.userdatabefore = this.userData;
        let contactData = this.contactUser(this.userData)
        this.userdata = this.sortDepartment(contactData,'department_id')
      }).catch(res=>{
        console.log(res)
      })
    },
    contactUser(arr) {
      let tempArr = [],
        newArr = []
      for (let i = 0; i < arr.length; i++) {
        if (tempArr.indexOf(arr[i].department_id) === -1) {
          newArr.push({
            department_id: arr[i].department_id,
            name: arr[i]['department']['name'],
            children: [arr[i]]
          })
          tempArr.push(arr[i].department_id);
        } else {
          for (let j = 0; j < newArr.length; j++) {
            if (newArr[j].department_id == arr[i].department_id) {
              newArr[j].children.push(arr[i])
            }
          }
        }
      }
      return newArr
    },
    sortDepartment(arr,key){
      return arr.sort(function(a,b){
        var x = a[key]
        var y = b[key]
        return ((x<y)?-1:(x>y)?1:0)
      })
    },

  },
};
