<template>
  <common-layout  ref="layoutRef" :table-attrs="{
     'has-operation':true
   }" :form-attrs="{
     'span-attrs':{
       span:4
     }
   }" :immediate-search=false :table-header="headerConfig" :get-table-list="getTableData" :filter-form="formConfig">
    <template #operation="{data}">
      <el-button  type="info" @click="$emit('changeOperation','detail',detailData)">查看</el-button>
    </template>
  </common-layout>
</template>

<script>
import {onBeforeMount, ref} from "vue";
import {cityCreditInformationPlatformSearchHome} from "@/data/all-form";
import {cityCreditInformationPlatformSearchHomeHeaderFr,cityCreditInformationPlatformSearchHomeHeaderZrr} from "@/data/all-table-header";
import commonLayout from "@/components/CommonLayout.vue";
import {
  getQueryDataCxbh,
  getQueryDataCxbhZrr,
  getYysxs
} from "@/service/credit-information/city-credit-information-platform-search/city-credit-information-platform-search";
import {handleMes} from "@/units/element-ui/tip";
import {useRootStore} from "@/pinia/useStore";
import {InsertSyslog} from "@/units/use-method/user-log";
export default {
  name: "CityCreditInformationPlatformSearchHome",
  components:{commonLayout},
  setup(){
    const layoutRef = ref(null);
    const formConfig = ref({...cityCreditInformationPlatformSearchHome});
    const headerConfig = ref(null);
    const YYSXList = ref(null);
    const detailData = ref(null);
    const store = useRootStore();
    function getCurrentHeader(val){
      debugger
      if(val == '01'){
        headerConfig.value = cityCreditInformationPlatformSearchHomeHeaderFr
      }else{
        headerConfig.value = cityCreditInformationPlatformSearchHomeHeaderZrr
      }
    }
    function getTableData(){  //获取表体数据
      debugger
      layoutRef.value.isValidate(queryData)
    }
    function queryData(){
      detailData.value = null;
      if(layoutRef.value.form.TYPE == "01"){
        getQueryDataCxbh({
          "ztmc":layoutRef.value.form.ZTNAME,
          "ztdm":layoutRef.value.form.CODE,
          "qdid":layoutRef.value.form.YYSX,
          "ispdf":"0",
          "isphoto":"0"
        }).then(res =>{
          let originData = [];
          InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"),"信息查询", "市信用信息查询成功查询成功", "市信用信息查询", layoutRef.value.form.ZTNAME)
          if(res.data.length > 0){
            detailData.value = res.data;
            originData.push(res.data[0]["xydaBasicData"][0]["tabulardata"][0]);
            layoutRef.value.getResData(originData,originData.length)
          }
        })
      }else if(layoutRef.value.form.TYPE == "02"){
        getQueryDataCxbhZrr({
          "ztmc":layoutRef.value.form.ZTNAME,
          "ztdm":layoutRef.value.form.CODE,
          "qdid":layoutRef.value.form.YYSX,
          "ispdf":"1",
          "isphoto":"1"
        }).then(res =>{
          let originData = [];
          InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"),"信息查询", "市信用信息查询成功查询成功", "市信用信息查询", layoutRef.value.form.ZTNAME)
          if(res.data.length > 0){
            detailData.value = res.data;
            originData.push(res.data[0]["xydaBasicData"][0]["tabulardata"][0]);
            layoutRef.value.getResData(originData,originData.length)
          }
        })
      }
    }
    function getYYSX(){  //获取全部应用事项（法人和自然人）
      getYysxs().then(res =>{
        debugger
        YYSXList.value = res;
        splitXSLX("01")
      })
    }
    function splitXSLX(val){  //获取事项类型
      debugger
      let returnOption = [];
      let tempSXLX = [];
      for(let i=0;i<YYSXList.value.length;i++){
       if(val=="01"){
         if(YYSXList.value[i].ztlx=="法人"){
           tempSXLX = YYSXList.value[i].datas
         }
       }else if(val=="02"){
         if(YYSXList.value[i].ztlx=="自然人"){
           tempSXLX = YYSXList.value[i].datas
         }
       }
      }
      returnOption =  tempSXLX.map(i => ({
        ...i,
        label:i.yylb,
        value:i.yylb,
      }))
      let option = [
        {
          value:'',
          label:'请选择'
        }
      ].concat(returnOption)

      const sxlxItem = formConfig.value.formItemsArr.find(i => i.modelValue === "SXLX")

      sxlxItem.options = [...option]
    }
    function splitYYSX(val){   //获取当前类型的应用事项
      let tempYYSY = [];
      let returnOption = [];
      for(let i=0;i<YYSXList.value.length;i++){
        for(let j = 0;j<YYSXList.value[i].datas.length;j++){
          if(YYSXList.value[i].datas[j].yylb == val){
            tempYYSY =YYSXList.value[i].datas[j].datas;
          }
        }
      }
      returnOption =  tempYYSY.map(i => ({
        ...i,
        label:i.yysx,
        value:i.yybm,
      }))
      let option = [
        {
          value:'',
          label:'请选择'
        }
      ].concat(returnOption);
      const yysxItem = formConfig.value.formItemsArr.find(i => i.modelValue === "YYSX");
      yysxItem.options = [...option]
    }
    onBeforeMount(()=>{
      getCurrentHeader("01");
      getYYSX();
      let typeItem = formConfig.value.formItemsArr.find(i => i.modelValue ==="TYPE");
      typeItem.events = {
        'change'(val){
          getCurrentHeader(val);
          splitXSLX(val);
        }
      }
      let sxlxItem = formConfig.value.formItemsArr.find(i => i.modelValue ==="SXLX");
      sxlxItem.events = {
        'change'(val){
          splitYYSX(val);
        }
      }
    })
    return {
      layoutRef,
      getTableData,
      headerConfig,
      detailData,
      formConfig
    }
  }
}
</script>

<style scoped>

</style>