<template>
	<view class="personnel-page">
		<scroll-view class="scroll-y" scroll-y="true">
			<tm-radio-group
        type="select"
			  :list="empDeptList"
				:defaultValue='desPersopn.empId'
				:setting="{
          pName: 'deptName',
          child: 'responseList',
				  value: 'empId',
				  name: 'empName'
			  }"
        :openkeys="[0]"
        @change="changeDesPersopn"
			/>
		</scroll-view>
		<view class="fixed-buttom-btn" @click="sure">
			<text class="btn-text">确定</text>
		</view>
	</view>
</template>

<script>
	// 人员架构
	export default {
    props: {
      // 按钮信息 (包过请求的key)
      btnInfo: {
        type: Object,
        default: () => {
          return {}
        }
      },
      // 任务详情
      missionDetails: {
        type: Object,
        default: () => {
          return {}
        }
      }
    },
		data(){
			return {
        // 人员列表
        empDeptList: [],
        // 接收人信息
        desPersopn: {}
			}
		},
		created() {
			uni.setNavigationBarTitle({
				title: '人员架构'
      });
      this.getEmpDeptTree();
    },
    methods: {
      // 更改接收人
      changeDesPersopn(selectVal, selectData, i, pSelect) {
        this.desPersopn = {
          ...selectData,
          ...pSelect
        };
      },
      // 确定
      sure() {
        const { params } = this.btnInfo;
        let requestParams = {};
        this.btnInfo.params.map(item => {
          if(item.valueKey){
            requestParams[item.paramsKey] = (
              item.isOutvalueKey
              ? this.missionDetails
              : this.btnInfo
            )[item.valueKey];
          }else if(item.value){
            requestParams[item.paramsKey] = item.value;
          }else {
            switch(item.paramsKey){
              case 'receiveEmpId':
                requestParams[item.paramsKey] = this.desPersopn.empId;
                break;
              case 'receiveEmpName':
                requestParams[item.paramsKey] = this.desPersopn.empName;
                break;
              case 'deptId':
                requestParams[item.paramsKey] = this.desPersopn.deptId;
                break;
              case 'deptName':
                requestParams[item.paramsKey] = this.desPersopn.deptName;
                break;
              default:
                requestParams[item.paramsKey] = '';
                break;
            }
          }
        });
        this.$emit('comRequest', requestParams);
      },
      // 查询部门人员树
			getEmpDeptTree() {
				this.$store.dispatch({
					type: 'mission/commActions',
					payload: {
						key: "getEmpDeptTree"
					}
				}).then(data => {
					if(data) {
            let _empDeptList = data.map(item1 => {
              return {
                ...item1,
                responseList: item1.responseList ?  item1.responseList.filter(item2 => item2.deptManage == 1) : []
              }
            }) || [];
            _empDeptList = _empDeptList.filter(item => item.responseList.length > 0);
            this.empDeptList = _empDeptList || [];
          }
				});
			}
    },
	}
</script>

<style lang="less">
	.personnel-page {
		height: 100%;

		.scroll-y {
			height: calc(100% - 87.5rpx);
			padding-top: 15rpx;
		}
	}
</style>
