<script setup>

import {auditReport} from "@/api/report/flow";
import feedback from "@/utils/feedback";
import {listPolice} from "@/api/system/police";
import {getList,save,delCommon} from '@/api/commonOpinions';
import useCatchStore from "@/stores/modules/catch";
import useUserStore from "@/stores/modules/user";
const userStore = useUserStore();
const props = defineProps(['dialog','reportId','node','isAudit'])
const emits = defineEmits(['submitFeedback',"closeFun"])
const loading =ref(false)

const catchSotre = useCatchStore();

const departs = catchSotre.getDeparts() ;
const formData = ref({
  isInform:'1'
})
const auditForm= ref()
const dialog = ref(false);
const flowNodes = ref([
  {
    label:'一审流程',
    value:'first'
  },
  {
    label:'二审流程',
    value:'second'
  },
  {
    label:'三审流程',
    value:'third'
  },
  {
    label:'领导审核',
    value:'Lead'
  },
  {
    label:'定案表审核',
    value:'original'
  },
  {
    label:'审计报告',
    value:'audit'
  }
])

const getNodesFun = (val)=>{
  let data = [];
  switch (val){
    case "review":
      data = [
          {
          label:'一审流程',
          value:'first'
        },
          {
            label:'二审流程',
            value:'second'
          },
          {
            label:'三审流程',
            value:'third'
          },
          {
            label:'领导审核',
            value:'Lead'
          }
        ]
      break;
    case "first":
      data=[
        {
          label:'二审流程',
          value:'second'
        },
        {
          label:'三审流程',
          value:'third'
        },
        {
          label:'领导审核',
          value:'Lead'
        },
      ]
          break;
    case "second":
      data=[
        {
          label:'三审流程',
          value:'third'
        },
        {
          label:'领导审核',
          value:'Lead'
        },
      ]
          break;
    case "third":
      data=[
        {
          label:'领导审核',
          value:'Lead'
        }
      ]
          break;
    case "Lead":
      data=[
        {
          label:'定案表审核',
          value:'original'
        }
      ]
          break;
    case "original":
      data=[
        {
          label:'审计报告',
          value:'audit'
        }
      ]
          break;
  }
  return data;
}



//审核人
const query = ref({
  current: 1,
  departBranch: true,
  size: 100
});
let polices = ref([])
function getPoliceList() {
  loading.value=true
  listPolice(query.value).then((data) => {
    polices.value = data.records;
  });
  loading.value=false
}
getPoliceList()

// 获取常用意见
const commonList =ref([])
async function getCommonList(){
  const res =  await  getList();
  commonList.value=res
  if(commonList.value && commonList.value.length > 0 ){
    formData.value.message = commonList.value[0].content
  }
}
getCommonList()
//存为常用
async function saveCommon(){
  if(formData.value.message){
    const res = await  save({"content":formData.value.message})
    feedback.msgSuccess("保存成功");
    await getCommonList();
  }
}
//删除常用
async function delCommonFun(){
  if(formData.value.message){
    const res = await delCommon({"content":formData.value.message})
    feedback.msgSuccess("保存成功");
    await getCommonList();
  }
}
//获取单位为简称
function getDepartShortName(departData,id,rowName){
  if(departData){
    for(let i =0;i<departData.length;i++){
      let s = departData[i];
      if(s.id === id){
        formData.value[rowName]= s.shortName;
        break;
      }
      if(s.children){
        getDepartShortName(s.children,id,rowName)
      }
    }
  }
}

//根据输入值获取信息
const inputGetList= (val)=>{
  query.value.name = val;
  getPoliceList()
}

//关闭添加界面
const closeAdd=()=>{
  formData.value={}
  emits("closeFun",false)
}
//提交
const submitFun = async ()=>{
  debugger
  await auditForm.value.validate();
  await feedback.confirm("是否确认通过？");
  formData.value.reportId = props.reportId;
  formData.value.isAudit = props.isAudit;
  //nextNode

  const res = await auditReport(formData.value);
  feedback.msgSuccess("操作成功");
  emits('submitFeedback',true)
  closeAdd()
}


watch(()=>props.dialog,(val)=>{
  debugger
  if(val){
    if(props.node){
      // let index =flowNodes.value.findIndex(s=>s.value===props.node);
      // flowNodes.value = flowNodes.value.splice(index + 1,flowNodes.value.length)
      flowNodes.value=getNodesFun(props.node)
    }
    if( userStore?.user?.departId){

      formData.value.approverUnit = userStore.user.departName;
      formData.value.approverUnitId= userStore.user.departId;
      query.value.departId =formData.value.approverUnitId;
      getPoliceList()
      delete  query.value.departId;
    }

    formData.value.isAudit = props.isAudit;
  }
})
//获取单位简称
watch(()=>formData.value.approverUnitId,(val)=>{
  if(val){
    query.value.departId =val;
    getDepartShortName(departs,val,'approverUnit')
  }else{
    delete query.value.departId;
  }
  getPoliceList()
})
//获用户名称
watch(()=>formData.value.approverId,(val)=>{
  if(val){
    let policesData = polices.value.filter(s=>s.idCode === formData.value.approverId);
    if(policesData.length > 0){
      formData.value.approver = policesData[0].name
    }
  }else{
    delete  formData.value.approver
  }
})
</script>

<template>
  <el-dialog title="提交审核" v-model="props.dialog" @close="closeAdd">
    <el-form ref="auditForm" :model="formData" :label-width="120">
      <el-form-item label="通知报审人" prop="isInform">
        <el-radio-group v-model="formData.isInform">
          <el-radio value="1"  >是</el-radio>
          <el-radio value="0"  >否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="办理单位" prop="approverUnitId" :rules="{ required: true, message: '请选择办理单位', trigger: 'blur' }">
        <depart-tree-select :auth="false" v-model="formData.approverUnitId" :size="'default'" placeholder="" clearable/>
      </el-form-item>
      <el-form-item label="办理流程" prop="nextNode" :rules="{ required: true, message: '请选择办理流程', trigger: 'blur' }">
        <el-select v-model="formData.nextNode">
          <el-option v-for="(item,index) in flowNodes" :value="item.value" :label="item.label" :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="审核人" prop="approverId" :rules="{ required: true, message: '请选择审核人', trigger: 'blur' }">
        <el-select :loading="loading" v-model="formData.approverId" :remote-method="inputGetList" filterable>
          <el-option v-for="(item,index) in polices"  :key="index" :value="item.idCode" :label="item.name"></el-option>
          <template #loading>
            <svg class="circular" viewBox="0 0 50 50">
              <circle class="path" cx="25" cy="25" r="20" fill="none" />
            </svg>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="办理意见" prop="message" :rules="{ required: true, message: '请输入办理意见', trigger: 'blur' }">
        <el-input type="textarea" :rows="10"  v-model="formData.message" ></el-input>
      </el-form-item>
      <el-form-item label="常用意见">
        <el-row :gutter="20" style="width:100%">
          <el-col :span="16">
            <el-select  allow-create filterable default-first-option v-model="formData.message">
              <el-option v-for="(item,index) in commonList" :key="index"
                         :value="item.content" :label="item.content"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="saveCommon" >存为常用</el-button>
            <el-button  @click="delCommonFun">删除常用</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div class="flex end">
      <el-button  @click="closeAdd">关闭</el-button>
      <el-button type="primary" @click="submitFun">提交</el-button>
    </div>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
