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


<script setup lang="jsx">
import fvSelect from '@/fvcomponents/fvSelect/index.vue'
import { ElNotification} from "element-plus";
import {deleteFund} from "@/api/special-fund";
import {toThousands} from '@/utils/changePrice.js'
const router = useRouter()
const searchConfig = reactive([
  {
    label: '专项资金名称',
    prop: 'name',
    component: 'el-input',
    props: {
      clearable: true,
      placeholder: '请输入专项资金名称查询'
    }
  },

  {
    label: '资金金额(元)',
    prop: 'fundAmount',
    component: 'el-input',
    props: {
      clearable: true,
      placeholder: '请输入资金金额查询'
    }
  },
  {
    label: '剩余金额(元)',
    prop: 'residualAmount',
    component: 'el-input',
    props: {
      clearable: true,
      placeholder: '请输入剩余金额查询'
    }
  },
  {
    label: '状态',
    prop: 'state',
    component: shallowRef(fvSelect),
    props: {
      placeholder: '请选择状态',
      clearable: true,
      filterable:true,
      cacheKey: 'special_fund',
      remote: true
    }
  },
  {
    label: '项目数量',
    prop: 'projectNumber',
    component: 'el-input',
    props: {
      clearable: true,
      placeholder: '请输入项目数量查询'
    }
  }
])
const tableConfig = 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: 'name',
      label: '专项资金名称',
      align: 'center'
    },
    // {
    //   prop: 'approveName',
    //   label: '审批人',
    //   align: 'center'
    // },
    {
      prop: 'fundAmount',
      label: '资金金额(元)',
      align: 'center',
      currentRender:({row})=>{
        return <span>{toThousands(row.fundAmount)}</span>
      }
    },
    {
      prop: 'residualAmount',
      label: '剩余金额(元)',
      align: 'center',
      currentRender:({row})=>{
        return <span>{toThousands(row.residualAmount)}</span>
      }
    },
    {
      prop: 'projectNumber',
      label: '项目数量',
      align: 'center',
      width: 100,
    },
    {
      prop: 'approveName',
      label: '当前审批节点',
      align: 'center',
      width: 150,
      currentRender: ({row, index}) => {
        if(row.state=='1'){
          return <span>{row.approveName}</span>
        }else {
          return <span>{row.taskNode}</span>
        }
      }
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      width: 150,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        if (row.state == undefined||row.state == 0) {
          return '--'
        } else {
          return (<Tag dictType={'special_fund'} value={row.state}/>)
        }
      }
    },
    {
      prop: 'oper',
      label: '操作',
      align: 'center',
      fixed:'right',
      width: 150,
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons = new Set(Array.from(row.buttons))
        if (buttons.has("details")) {
        btn.push({label: '详情', prem: ['special:fund:info'], func: () => handleDetail(row), type: 'primary'})
        }
        if (buttons.has("edit")) {
        btn.push({label: '编辑', prem: ['special:fund:edit'], 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>
                ))
              }
              {
                buttons.has("delete") ?
                <popover-delete name={row.name} type={'专项资金'} btnType={'danger'}
                                onDelete={() => handleDelete(row)}/>
                : ''
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/special/fund',
  params: {},
  btns: [
    {name: '新增', key: 'add', color: '#DED0B2', auth: ['special:fund:add']},
    // {name: '导出', key: '_export', color: '#DED0B2', auth: ''},
  ]
})
const tableIns = ref()

const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}
const handleDetail = (row) => {
  router.push({
    name: 'Fund/detail',
    query: {
      id: row.specialFundId
    }
  })
}
const handleAdd = () => {
  router.push({
    name: 'Fund/add',
    query: {}
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Fund/edit',
    query: {
      id: row.specialFundId
    }
  })
}
const handleDelete = async (row) => {
  deleteFund(row.specialFundId).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tableIns.value.refresh()
    }
  })
}
const headBtnClick = (key) => {
  switch (key) {
    case 'add':
      handleAdd()
      break;
    case 'export':
      handleExport()
      break;
  }
}
</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-col-5:nth-child(4).el-col-offset-1){
  margin-left: 10px !important;
}
</style>
