<template>
 <div class="sta-content ">
   <diy-el-form v-if="$route.name!=='year-use-sta'" :span="4" :forms="formConfig" v-model:model-form="forms">
     <template #searchBtn>
       <el-button type="primary" :icon="Search" @click="initSearch">查询</el-button>
     </template>
     <template #addBtn v-if="+useRootStore().userInfo.ptjk_role ===0">
       <el-button type="primary" :icon="Plus" @click="$emit('addOp')" >新增</el-button>
     </template>
   </diy-el-form>
   <div class="my-line"></div>
   <my-table
     border

     :headers="tableHeader"
    :is-height-equal-max-height="true"
     :page-size="10"
    :table-data="tableData"
     v-bind="tableAttrs"

   >
     <template v-for="item in tableHeader.filter(i => i.type!=='text')" #[item.prop]="{data}">
       <span v-if="!(data[item.prop] > 0)">{{data[item.prop] || 0}}</span>
       <el-link @click="openModel(data,item)" type="primary" v-else>{{data[item.prop]}}</el-link>
     </template>
     <template v-for="item in tableHeader.filter(i => i.type==='text')" #[item.prop]="{data}">
       <slot :name="item.prop" :data="data" >{{data[item.prop]}}</slot>
     </template>
       <template #operation="{data}">
         <slot name="operation" :data="data"></slot>
       </template>
   </my-table>
   <ele-pagination  ref="refEl"

                    @sizeOrPageChange="handleSizeChange"
                    class="page"
                    :default-page-size="pageSize"


                    :total-count="totalCount">

     <template #default>
       <div class="m-s">
         显示第 {{pageSize * (pageIndex -1) + 1 }} 到第 {{pageSize * pageIndex}} 条记录，总共 {{totalCount}} 条记录 共显示

       </div>
     </template>
   </ele-pagination>
   <template v-if="isDetailDialogIsShow">
     <sta-tab-content-detail-dialog :type="type" :current-dialog-obj="currentDialogObj" :dialog-headers="currentObj.dialogHeaders" v-model:dialog-visible="isDetailDialogIsShow"></sta-tab-content-detail-dialog>
   </template>
 </div>
</template>

<script>
import DiyElForm from "@/components/common/DiyElForm.vue";
import {computed, onMounted, ref} from "vue";
import {shareExchangeHomeFilterForm} from "@/data/all-forms";
import MyTable from "@/components/common/MyTable.vue";
import {usePagination} from "@/unit/use-method/usePagination";

import {shareExchangeArr, staTabReflectArr} from "@/data/all-reflect";
import ElePagination from "@/components/common/ElePagination.vue";
import {useRoute} from "vue-router";
import {Plus, Search} from "@element-plus/icons-vue";
import StaTabContentDetailDialog from "@/components/common/StaTabContentDetailDialog.vue";
import {useRootStore} from "@/stores/useStore";
import {UserInfo} from "@/unit/UserInfo";
import {getDetailUnitListSpace, getDetailUnitListZw} from "@/service/system-manage/unit-manage/unit-maneg";
import {userManageHomeTableZwHeader2} from "@/data/all-table-header";

export default {
  name: "StaTabContent",
  computed: {
    Plus() {
      return Plus
    },
    Search() {
      return Search
    }
  },
  components: {StaTabContentDetailDialog, ElePagination, MyTable, DiyElForm},
  props:{
    type:{

    },
    tableAttrs:{

    },
    getTableList:{

    },
    myHeader:{

    }
  },
  setup(props) {

    const route = useRoute()
    const currentObj = computed(() =>{
      return staTabReflectArr.find(i=>i.id===route.name).tabArr.find(i=>i.tabName === props.type)
    })
    const tableHeader = ref([...props.myHeader])
    const formConfig = ref({...currentObj.value.filterForm})
    const isDetailDialogIsShow = ref(false)
    const currentDialogObj = ref({})

    onMounted(() => {
      let findAdBtn = formConfig.value.formItemsArr.find(i => i.slotName === 'addBtn')
      if (findAdBtn){
        findAdBtn.isHide = +useRootStore().userInfo.ptjk_role!==0
      }

      console.log('共享创建')
    })
    function openModel(data,item){
      isDetailDialogIsShow.value = true
      currentDialogObj.value = {
        ...data,
        ...forms.value,
        prop:item
      }
    }
    function getTableListData() {


      let params = {}
      console.log('route.params.name',route.name,route)
      if (route.name === 'share-exchange-sta'){

        if (currentObj.value.tabName === 'kj'){
          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            start_time:forms.value.start_time,
            end_time:forms.value.end_time,
          }
        }else {
          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            start_time:forms.value.start_time,
            end_time:forms.value.end_time,
            a_code:forms.value.a_code,
            code:''
          }
        }
      }
      else if (route.name === 'share-use-sta'){

        if (currentObj.value.tabName === 'kj'){

          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            a_start_time:forms.value.start_time,
            a_end_time:forms.value.end_time,
            "a_manager":true,
            "a_unitcode":42,
          }
        }else {
          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            START_DATE:forms.value.start_time,
            END_DATE:forms.value.end_time,
          }
        }
      }
      else if (route.name === 'service-use-sta'){

        if (currentObj.value.tabName === 'kj'){

          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            START_DATE:forms.value.start_time,
            END_DATE:forms.value.end_time,
            appkey:new UserInfo().GetUserKeyCode()


          }
        }else {
          params = {
            pageIndex:pageIndex.value,
            pageSize:pageSize.value,
            START_DATE:forms.value.start_time,
            END_DATE:forms.value.end_time,
            appkey:new UserInfo().GetUserKeyCode()
          }
        }
      }
      else if (route.name === 'year-use-sta'){
        let startTime = new Date().getFullYear() + "-01-01";
        let endTime = new Date().getFullYear() - 1 + "-01-01";
        if (currentObj.value.tabName === 'kj'){

          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_start_time":startTime,
            "a_end_time":endTime,

          }
        }else {
          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_start_time":startTime,
            "a_end_time":endTime,

          }
        }
      }
      else if (route.name === 'tool-use-sta'){

        if (currentObj.value.tabName === 'kj'){

          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_start_time":forms.value.start_time,
            "a_end_time":forms.value.end_time,
            RESOURCE_TYPE:'1'
          }
        }else {
          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_start_time":forms.value.start_time,
            "a_end_time":forms.value.end_time,
            RESOURCE_TYPE:'3'
          }
        }
      }
      else if (route.name === 'unit-manage'){

        if (currentObj.value.tabName === 'kj'){

          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_name":forms.value.a_name,


          }
        }else {
          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_name":forms.value.a_name,


          }
        }
      }
      else if (route.name === 'root-manage'){
        params = {
          "pageIndex": pageIndex.value,
          "pageSize": pageSize.value,
          "ROLE_NAME":forms.value.ROLE_NAME,
          "ROLE_TYPE":forms.value.ROLE_TYPE,


        }
      }
      else if (route.name === 'user-manage'){
       if (currentObj.value.tabName === 'kj'){
         params = {
           "pageIndex": pageIndex.value,
           "pageSize": pageSize.value,
           "a_name":forms.value.a_name,
           "a_unit_code":forms.value.a_unit_code || '',
           a_user_unit:'42',
           a_manager:'true',
           appkey:new UserInfo().GetUserKeyCode()

         }
         let findUnit = formConfig.value.formItemsArr.find(i => i.modelValue === 'a_unit_code')
         getDetailUnitListSpace({
           appkey:useRootStore().userInfo.ptjk_userkeycode
         }).then(res => {
           let data = res.msg[0].Rows
           findUnit.options = [{value:'',label:'请选择'}].concat(data.map(i => ({
             label:i.NAME,
             value:i.CODE,
           })))
         })
       }
       else if (currentObj.value.tabName === 'zw'){
         let findUnit = formConfig.value.formItemsArr.find(i => i.modelValue === 'a_unit_code')
          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_name":forms.value.a_name,
            "a_unit_code":forms.value.a_unit_code,
            a_user_unit:'AH0026',
            a_manager:'true',
            appkey:new UserInfo().GetUserKeyCode()

          }

         getDetailUnitListZw({
           appkey:useRootStore().userInfo.ptjk_userkeycode
         }).then(res => {
           let data = res.msg[0].Rows
           findUnit.options = [{value:'',label:'请选择'}].concat(data.map(i => ({
             label:i.NAME,
             value:i.CODE,
           })))
         })
        }
       else if (currentObj.value.tabName === 'lm'){
         let findUnit = formConfig.value.formItemsArr.find(i => i.modelValue === 'a_unit_code')
         params = {
           "pageIndex": pageIndex.value,
           "pageSize": pageSize.value,
           "a_name":forms.value.a_name,
           "a_unit_code":forms.value.a_unit_code,

           appkey:new UserInfo().GetUserKeyCode()

         }
         getDetailUnitListSpace({
           appkey:useRootStore().userInfo.ptjk_userkeycode
         }).then(res => {
           let data = res.msg[0].Rows
           findUnit.options = [{value:'',label:'请选择'}].concat(data.map(i => ({
             label:i.NAME,
             value:i.CODE,
           })))
         })
       }
      }
      else if (route.name === 'log-manage'){

        if (currentObj.value.tabName === 'kj'){

          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_user_unitid":'42',
            a_manager:'true',
            a_startTime:forms.value.a_startTime,
            a_endTime:forms.value.a_endTime,
            a_key_world:forms.value.a_key_world,
            appkey:new UserInfo().GetUserKeyCode()

          }
        }else {
          params = {
            "pageIndex": pageIndex.value,
            "pageSize": pageSize.value,
            "a_user_unitid":'AH0026',
            a_manager:'true',
            a_startTime:forms.value.a_startTime,
            a_endTime:forms.value.a_endTime,
            a_key_world:forms.value.a_key_world,
            appkey:new UserInfo().GetUserKeyCode()


          }
        }
      }
      currentObj.value.serviceUrl(params).then(res => {
        tableData.value = res.msg[0].Rows||[]
        totalCount.value = res.msg[0].TotalRowCount
      })

    }
    const {pageSize,pageIndex,totalCount,handleSizeChange,initSearch,refEl,tableData} = usePagination(getTableListData)
    const forms=  ref({})


    return {
      forms,
      shareExchangeHomeFilterForm,
      pageSize,pageIndex,totalCount,handleSizeChange,initSearch,refEl,tableData,

      currentObj,
      getTableListData,
      isDetailDialogIsShow,
      openModel,
      currentDialogObj,
      useRootStore,
      formConfig,
      tableHeader
    }
  }
}
</script>

<style scoped lang="scss">
.sta-content{
  padding-top: 10px;
}
</style>
