<template>
  <fvSearchForm :searchConfig="searchConfig" @search="search"></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 {deleteAllocation} from "@/api/expense-manage";

const router = useRouter();
const searchConfig = reactive([
  {
    label: '分摊名称',
    prop: 'shareName',
    component: 'el-input',
    props: {
      placeholder: '请输入分摊名称查询',
      clearable: true,
      filterable: true,
      checkStrictly: true
    }
  }, {
    label: '分摊月份',
    prop: 'apportionmentMonth',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择分摊月份',
      clearable: true,
    },
    colProps: {}
  },
  {
    label: '生成时间',
    prop: 'generationTime',
    component: 'el-date-picker',
    props: {
      placeholder: '请选择生成时间',
      clearable: true,
      valueFormat: 'YYYY-MM-DD',
    },
    colProps: {}
  },
  // {
  //   label: '状态',
  //   prop: 'state',
  //   component: shallowRef(fvSelect),
  //   props: {
  //     placeholder: '请选择状态',
  //     clearable: true,
  //     cacheKey: 'special_fund'
  //   }
  // },
])
const tableIns = ref()
const tableConfig = reactive({
  columns: [
    {
      prop: 'shareName',
      label: '分摊名称',
      align: 'center'
    },
    {
      prop: 'apportionmentMonth',
      label: '分摊月份',
      align: 'center'
    },
    {
      prop: 'generationTime',
      label: '生成时间',
      align: 'center'
    },
    {
      prop: 'state',
      label: '状态',
      align: 'center',
      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',
      showOverflowTooltip: false,
      currentRender: ({row, index}) => {
        let btn = []
        let buttons
        if (row.buttons) {
          buttons = new Set(Array.from(row.buttons))
        }
        if (buttons.has("details")) {
          btn.push({label: '详情', prem: ['mosr:requirement:info'], func: () => handleDetail(row), type: 'primary'})
        }
        if (buttons.has("edit")) {
          btn.push({label: '编辑', prem: ['mosr:requirement:resubmit'], func: () => handleEdit(row), type: 'primary'})
        }
        if (buttons.has("report")) {
          btn.push({label: '明细导出', prem: ['mosr:requirement:info'], func: () => handleReport(row), type: 'primary'})
        }
        if (buttons.has("report")) {
          btn.push({label: '汇总导出', prem: ['mosr:requirement:info'], func: () => handleReport(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.shareName} type={'费用分摊'} btnType={'danger'}
                                    perm={['mosr:requirement:del']}
                                    onDelete={() => handleDelete(row)}/>
                    : ''
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/cost/allocation',
  btns: [
    {name: '添加分摊', key: 'add', color: '#DED0B2'}
  ],
  params: {}
})

const search = (val) => {
  tableConfig.params = {...val}
  tableIns.value.refresh()
}
const handleAdd = () => {
  router.push({
    name: 'Share/add',
    query: {}
  })
}
const handleDetail = (row) => {
  router.push({
    name: 'Share/detail',
    query: {
      id: row.allocationId
    }
  })
}
const handleEdit = (row) => {
  router.push({
    name: 'Share/edit',
    query: {
      id: row.allocationId
    }
  })
}
const handleDelete = (row) => {
  deleteAllocation(row.allocationId).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;
  }
}
</script>

<style scoped>

</style>
