<!--
@ 作者: 鲁智强
@ 时间: 2023-08-15 11:34:38
@ 备注: 岗位指标添加
-->
<template>
	<el-dialog :model-value="true" :title="title" @close="handleClose">{{formData.yrb}}
		<el-form ref="ruleFormRef" :model="formData" label-width="120px" class="demo-ruleForm">
      <el-form-item label="指标名称:" prop="name">
		<el-input v-model="formData.title" placeholder="请输入指标名称"></el-input> 
	</el-form-item>
	<el-form-item v-model="formData.otherposttarget" label="岗位/人员:" prop="name">
		<div>
		{{formData.gov}}
		<el-tree-select ref="orgTreeRef" v-model="iop[0].orgid" clearable show-checkbox check-strictly=true node-key="id" :data="lzq" :props="orgTreeProps" :render-after-expand="false" @check="position"/>	
		<el-tree-select ref="orgTreeRef" v-model="iop[0].postid" clearable multiple show-checkbox node-key="id" :data="zjr" :props="orgTreeProps" :render-after-expand="false" @check="chl"/>	
		<el-tree-select ref="orgTreeRef" v-model="iop[0].operator" clearable multiple show-checkbox node-key="key" :data="tablea" :props="orgTreeProps" :render-after-expand="false" @check="lll"/>	
		</div>
	</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.cycleattr">
          <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="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,addposttargetcont,search_orgpost,positionlist} from '@/api/opk/api'
  import {editPostCont} from '@/api/opk/pulic/api'
  import {govthree} from '@/api/opk/opk/api'
  
  interface FormInfo {
	id: string;
	age: string;
	name: string;
	sex: string;
	ccc: string;
	ade: string;
	sort: number;
	title: string;
	type: number;
  }
const isshow = ref(false)
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({
	"title": "",           // 1
	"acceptdepartmentid": [] as String[],  //1
    "userlist": [] as String[],
    "dimension":"",		//1
    "unit": "",          // 1
    "cycle": 4,
    "cycleattr": 1,
    "otherposttarget":[] as String[],
	"type":1,
	"scoringmethod": 1,
	"name":"",
	"gov":"",
	"lzq":"",
	"zjr":"",
	"yrb":"",
})
const iop = reactive([{
	orgid:"",
	postid:"",
	operator:[

	]
}])
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 tablee = ref([])
function orgpost(){
	search_orgpost({name:"1",orgid:"309"})
		.then(data =>{
			tablee.value = data.data;
	})
	.finally(()=>{
		isshow.value = false
	})
}
orgpost()
const chl = ()=>{
	formData.departmentPostOther = zjr
}
const tablea = ref([])
function getgrou(){
	getgroupuser({id:309,level:4})
		.then(data => {
			tablea.value = data.data;
			console.log(tablea.value)
		})
}
getgrou()
const lzq = ref([])
function govthrees(){
	govthree({id:309,all:1})
		.then(data=>{
			lzq.value = data.data
		})
}
govthrees()
const zjr = ref([])
const position = ()=>{
	const add = iop[0].orgid.toString()
	positionlist({page:1,pagesize:100000,organization:add})
		.then(data=>{
			zjr.value = data.data.list;
		})
}
const lll = ()=>{
	formData.otherposttarget = tablea[this.tablea.findIndex(item => item.key===formData.yrb)]
}
// 关闭弹窗
 const handleClose = ()=> {
    emit("update:adddPostBox", false);
  }
const handleDel = (val:any) => {
	const dictTypeIds = [val.outID || postIds.value].join(",");
    let dictTypeIdAry = dictTypeIds.split(",")
  		ElMessageBox.confirm("你确定删除这条信息吗?", "提示", {
    		confirmButtonText: "确认",
    		cancelButtonText: "取消",
    		type: "warning",
  		})
			handleSure(val)
			console.log(val.id)	
};
const handleSure = (val:any)=> {
    const index = tableData.value.findIndex((item:any) => item.id === val.id);
    if(index!=-1){
		tableData.value.splice(index, 1);
		}
}
 const submitForm=()=> {
    handleClose()
	const add = formData.acceptdepartmentid.toString()
    formData.acceptdepartmentid = add.split(',')
	formData.dimension = formData.dimension.toString()
	const edd = formData.userlist.toString()
	formData.userlist = edd.split(',')
	const www = Number(iop[0].postid)
	iop[0].postid = www
	if (props.addrowInfo.name) {
      // 修改
      const id = props.addrowInfo; // 假设你有一个唯一的ID来识别要修改的项
      emit("editRow", formData);
    } else {
      // 新增
      addposttargetcont({title:formData.title,type:1,unit:formData.unit,scoringmethod:1,dimension:formData.dimension,cycle:4,cycleattr:1,
		departmentPostOther:formData.departmentPostOther,otherposttarget:iop})
        .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>
  