<!--
@ 作者: 鲁智强
@ 时间: 2023-08-15 11:34:38
@ 备注: 部门指标添加
-->
<template>
	<el-dialog :model-value="true" :title="title" @close="handleClose">
	<el-form ref="ruleFormRef" :model="formData" label-width="120px" class="demo-ruleForm">
      <el-form-item label="指标名称:" prop="name">
		<el-input v-model="formData.name" placeholder="请输入指标名称"></el-input> 
	</el-form-item>
	<el-form-item label="关联部门:" prop="sort">
		<el-tree-select ref="orgTreeRef" v-model="formData.acceptdepartmentid" clearable multiple node-key="id" :data="tableData" :props="orgTreeProps" :render-after-expand="false"/>
	</el-form-item>
	<el-form-item label="指标维度：" prop="sex">
        <el-tree-select ref="orgTreeRef" v-model="formData.dimension" clearable multiple node-key="id" :data="tables" :props="orgTreeProp" :render-after-expand="false"/>
	</el-form-item>
	<el-form-item label="指标性质：" prop="state">
        <el-select v-model="formData.nature">
          <el-option label="定性指标" :value="1" />
          <el-option label="定量指标" :value="2" />
		</el-select>
	</el-form-item>
	<el-form-item label="计量单位：" prop="unites">
		<el-input v-model="formData.unit" placeholder="请输入计量单位"/>
	</el-form-item>
	<el-form-item label="数据提交:" prop="sorts">
		<el-tree-select ref="orgTreeRef" v-model="formData.userlist" node-key="key" :data="tablea" :props="orgTreeProps" :render-after-expand="false"/>
	</el-form-item>
	<el-form-item label="计分方式：" prop="iop">
        <el-select v-model="formData.cycleattr">
          <el-option label="自动计分" :value="1" />
          <el-option label="手动计分" :value="2" />
		</el-select>
	</el-form-item><el-form-item label="考核周期：" prop="uio">
        <el-select v-model="formData.scoringmethod">
          <el-option label="月" :value="1" />
          <el-option label="季度" :value="2" />
		  <el-option label="年" :value="3" />
		</el-select>
	</el-form-item>
	<el-form-item label="辅助计数：" prop="state">
		<el-input-number v-model="num" :min="1"/>
	</el-form-item>
	  <el-form-item>
        <el-button type="primary" @click="submitForm()">确定</el-button>
    </el-form-item>
	</el-form>	
	</el-dialog>
  </template>
  <script lang="ts" setup>
  import { reactive } from "vue";
  import {tarlist,addPostCont,addtarget,getgroupuser} from '@/api/opk/api'
  import {editPostCont} from '@/api/opk/pulic/api'
  
  interface FormInfo {
	id: string;
	age: string;
	name: string;
	sex: string;
	ccc: string;
	ade: string;
	sort: number;
	title: string;
	type: number;
  }
const props= defineProps( {
	  title: {
		type: String,
		default: "",
	  },
	  adddPostBox: {
		type: Boolean,
		default: false,
	  },
	  addrowInfo: {
		type: Object as () => FormInfo,
		default() {
		  return {};
		},
	  },
	  arrayNum: {
		type: Number,
		default: 0,
	  },
	});
const num =ref(1); 
const orgTreeLoadings = ref([]);	//加载行政组织树
const orgTreeProps ={
   label: 'name',
   children:'child',
}
const orgTreeProp = {
	label:'title',
	children:'children'
}
const emit = defineEmits(["update:adddPostBox","editRow","addRow"])
const formData = reactive({
	"name": "",           // 1
	"share": 0,				//1
    "acceptdepartmentid": [] as String[],  //1
    "userlist": [] as String[],
    "dimension":"",		//1
    "nature": 1,
    "unit": "",          // 1
    "cycle": 4,
    "cycleattr": "",
    "scoringmethod": 1,
	"visiblerangegroup": [],	//1
    "visiblerangedepart": []	//1
})
const tableData = ref<any>([])
function jjjs(){
	addPostCont("/hrapi/org/govthree","post")
	    .then(data => {
        	tableData.value = data.data[3].child[14].child;
        })
        .catch(error => {
          console.error('Error fetching data:', error);
        });
}
jjjs()
const tables = ref([])
function editPost(){
	editPostCont()
		.then(data => {
			tables.value = data.data.list;
		})
}
editPost()
const tablea = ref([])
function getgrou(){
	getgroupuser({id:309,level:4})
		.then(data => {
			tablea.value = data.data;
			console.log(tablea.value)
		})
}
getgrou()
  // 关闭弹窗
 const handleClose = ()=> {
    emit("update:adddPostBox", false);
  }
 const submitForm=()=> {
    handleClose()
	const add = formData.acceptdepartmentid.toString()
    formData.acceptdepartmentid = add.split(',')
	const aee = formData.dimension.toString()
	formData.dimension = aee
	const edd = formData.userlist.toString()
	formData.userlist = edd.split(',')
	if (props.addrowInfo.name) {
      // 修改
      const id = props.addrowInfo; // 假设你有一个唯一的ID来识别要修改的项
      emit("editRow", formData);
    } else {
      // 新增
      addtarget(formData)
        .then((response:any) => {
          // 处理新增成功的情况
          if (response.code === 200) {
            ElMessage.success("新增成功");
            emit("addRow", formData);
          } else {
            // 处理新增失败的情况
            ElMessage.error("新增成功");
          }
        })
    }
  }
  </script>
  
  <style scoped>
  
  
  .avatar-uploader .avatar {
	display: block;
	width: 178px;
	height: 178px;
  }
  </style>
  
  <style lang="scss">
  .avatar-uploader .el-upload {
	position: relative;
	overflow: hidden;
	cursor: pointer;
	border: 1px dashed var(--el-border-color);
	border-radius: 6px;
	transition: var(--el-transition-duration-fast);
  }
  
  .avatar-uploader .el-upload:hover {
	border-color: var(--el-color-primary);
  }
  
  .el-icon.avatar-uploader-icon {
	width: 178px;
	height: 178px;
	font-size: 28px;
	color: #8c939d;
	text-align: center;
  }
  .el-dialog{
	  width:1200px;
  }
  </style>
  