<template>
  <fvSearchForm :searchConfig="searchConfig" @search="search" style="margin-left: 16px"></fvSearchForm>
<!--  <el-button color="#DED0B2" style="float: left;margin: 0 10px 10px 0" @click="exportTable">导出</el-button>-->

  <fvTable ref="tableIns" :tableConfig="tableConfig" @headBtnClick="headBtnClick" style="margin-top: 15px"  @selectionChange="selectionChange">
    <template #empty>
      <el-empty description="暂无数据"/>
    </template>
  </fvTable>
</template>

<script setup lang="jsx">
import fvSelect from '@/fvcomponents/fvSelect/index.vue'

import { getSubCompOpt } from '@/api/user/user.js';
import {reactive, ref} from "vue";
import {shareDetailExport, shareExportExcel} from "@/api/expense-manage";
import {ElMessage} from "element-plus";
const router = useRouter()
const route = useRoute()
const searchConfig = reactive([
  {
    label: '子项目',
    prop: 'subProjectName',
    component: 'el-input',
    props: {
      placeholder: '请输入子项目查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  // {
  //   label: '支付月份',
  //   prop: 'apportionmentMonth',
  //   component: 'el-date-picker',
  //   props: {
  //     placeholder: '请选择支付月份',
  //     clearable: true,
  //     type:'month',
  //     format: 'YYYY-MM',
  //     valueFormat:"YYYY-MM"
  //   },
  //   colProps: {}
  // },
  // {
  //   label: '人员性质',
  //   prop: 'personnelNature',
  //   component: shallowRef(fvSelect),
  //   props: {
  //     placeholder: '请选择人员性质',
  //     clearable: true,
  //     filterable: true,
  //     cacheKey: 'nature_of_personnel'
  //   }
  // },
])
const tableIns = ref()
const selectData = ref([])
const tableConfig = reactive({
  columns: [
    {
      type: 'selection',
      prop: 'selection',
    },
    // {
    //   prop: 'name',
    //   type: 'index',
    //   label: '序号',
    //   align: 'center',
    //   width:85,
    //   index: index => {
    //     return (tableIns.value.getQuery().pageNum - 1) * tableIns.value.getQuery().pageSize + index + 1
    //   }
    // },
    {
      prop: 'paymentYear',
      label: '支付年份',
      align: 'center'
    },
    {
      prop: 'paymentMonth',
      label: '支付月份',
      align: 'center'
    },

    {
      prop: 'masterProjectName',
      label: '主项目',
      align: 'center',
      width: 120,
    },

    {
      prop: 'subProjectName',
      label: '子项目',
      align: 'center',
      width: 120,
    },
    {
      prop: 'researchPersonnel',
      label: '研发人员',
      align: 'center',
      width: 120,
      showOverflowTooltip: false,
    },
    {
      prop: 'personnelNature',
      label: '人员性质',
      align: 'center',
      width: 120,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.personnelNature&&row.personnelNature !== null&&row.personnelNature!==undefined) {
          return (<Tag dictType={'nature_of_personnel'} value={row.personnelNature}/>)
        } else {
          return '--'
        }
      }
    },
    {
      prop: 'researchDuration',
      label: '当月研发工时(天)',
      align: 'center',
      // currentRender:({row})=>{
      //   return <span>{toThousands(row.afterTax)}</span>
      // }
    },
    {
      prop: 'workday',
      label: '当月总工时(天)',
      align: 'center',
      showOverflowTooltip: false,
    },
    {
      prop: 'subtotal',
      label: '人工成本分摊(元)',
      align: 'center',
    },

  ],
  api: '/workflow/mosr/cost/share',
  params: {},
  btns: [
    {name: '添加分摊', key: 'add', color: '#DED0B2'},
    {name: '导出', key: 'export', color: '#DED0B2'}
  ],
  export:{
    open :false,
  }
})


const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
    case 'export':
      exportTable()
      break;
  }
}

const selectionChange = (data) => {
  console.log('data', data)
  selectData.value=data.map(item=>item.allocationId)
}
const exportTable = () => {
  console.log('selectData',selectData.value)
  if (selectData.value.length === 0) {
    ElMessage.warning('请选择要导出的费用分摊')
    return
  }
  shareDetailExport(selectData.value).then(res => {
    console.log(res)
    let fileName = `科技研发项目工时及成本分摊汇总表.xlsx`
    const blob = new Blob([res.data])
    let a = document.createElement('a')
    a.href = URL.createObjectURL(blob)
    a.download = fileName
    a.click()
  })
}
const handleAdd = () => {
  router.push({
    name: 'Sharedetail/add',
    query: {}
  })
}
const init = async () => {
  const res = await getSubCompOpt()
  searchConfig.value.find(item=>item.prop == 'affiliatedCompanyIds').props.data = res.data
}

// init()

</script>
<style scoped lang="scss">
:deep(.el-table__header) {
  .is-leaf:first-child {
    .cell {
      //margin-left: -25px !important;
    }
  }
}

:deep(.el-table__body) {
  .el-table__cell:first-child {
    .cell {
      //margin-left: -13px !important;
    }
  }
}
:deep(.el-date-editor--month){
  width: 100%;
}
</style>
