<template>
  <fvSearchForm :searchConfig="researchFundSearchConfig" @search="searchResearchFund"
                style="margin-left: 16px"></fvSearchForm>
  <fvTable ref="tableIns" :tableConfig="researchFundTableConfig" @headBtnClick="headBtnClick">
    <template #empty>
      <el-empty description="暂无数据"/>
    </template>
  </fvTable>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {deleteResearchFund} from "@/api/research-fund";
import { getSubCompOpt } from '@/api/user/user.js';
import {toThousands} from '@/utils/changePrice.js'

const router = useRouter();
const researchFundSearchConfig = ref([
  {
    label: '研发公司名称',
    prop: 'rdCompanyId',
    component: 'el-tree-select',
    props: {
      placeholder: '请输入研发公司名称查询',
      clearable: true,
      data: [],
      filterable: true,
      checkStrictly: true,
      remote: true
    }
  },
  {
    label: '研发资金金额',
    prop: 'rdAmount',
    component: 'el-input',
    props: {
      placeholder: '请输入研发资金金额查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  },
  {
    label: '研发资金日期',
    prop: 'rdDate',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择研发资金日期',
      clearable: true,
      type: 'month',
      format: 'YYYY-MM',
      valueFormat: "YYYY-MM"
    },
    colProps: {}
  },
])
const tableIns = ref()
const researchFundTableConfig = reactive({
  columns: [
    {
      prop: 'index',
      type: 'index',
      label: '序号',
      align: 'center',
      width: 85,
      index: index => {
        return (tableIns.value.getQuery().pageNum - 1) * tableIns.value.getQuery().pageSize + index + 1
      }
    },
    {
      prop: 'rdCompany',
      label: '研发公司名称',
      align: 'center'
    },
    {
      prop: 'rdAmount',
      label: '研发资金金额',
      align: 'center',
      currentRender:({row})=>{
        return <span>{toThousands(row.rdAmount)}</span>
      }
    },
    {
      prop: 'rdDate',
      label: '研发资金日期',
      align: 'center'
    },
    {
      prop: 'remarks',
      label: '备注',
      align: 'center'
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      fixed: 'right',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        btn.push({label: '编辑',  prem: ['rd:fund:update'], func: () => handleEdit(row), type: 'primary'})
        return (
            <div style={{width: '100%'}}>
              {
                btn.map(item => (
                    <el-button
                        type={item.type}
                        v-perm={item.prem}
                        onClick={() => item.func()}
                        link
                    >
                      {item.label}
                    </el-button>
                ))
              }
              {
                <popover-delete name={row.rdCompany} type={'研发投入资金'} btnType={'danger'} perm={['rd:fund:remove']}
                                onDelete={() => handleDelete(row)}/>

              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/rd/list',
  btns: [
    {name: '新增', key: 'add', color: '#DED0B2', auth: ['rd:fund:add']}
  ],
  params: {}
})
const searchResearchFund = (val) => {
  let obj = {...val}
  if (obj.dateValue) {
    obj.startGenerationTime = obj.dateValue[0]
    obj.endGenerationTime = obj.dateValue[1]
    delete obj.dateValue
  }
  researchFundTableConfig.params = obj
  tableIns.value.refresh()
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
  }
}
const handleAdd = () => {
  router.push({
    name: 'Devfund/add',
    query: {}
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Devfund/edit',
    query: {
      id: row.rdFundId
    }
  })
}
const handleDelete = (row) => {
  deleteResearchFund(row.rdFundId).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tableIns.value.refresh()
    }
  });
}
const init = async () => {
  const res = await getSubCompOpt()
  researchFundSearchConfig.value.find(item=>item.prop == 'rdCompanyId').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>
