<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 {deleteAllocation} from "@/api/expense-manage";

const router = useRouter();
const shortcuts = [
  {
    text: '上周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: '上月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: '三月前',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
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,
      type:'month',
      format: 'YYYY-MM',
      valueFormat:"YYYY-MM"
    },
    colProps: {}
  },
  {
    label: '生成时间',
    prop: 'dateValue',
    component: 'el-date-picker',
    props: {
      clearable: true,
      type: 'daterange',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      shortcuts: shortcuts
    },
    colProps: {}
  },
  // {
  //   label: '状态',
  //   prop: 'state',
  //   component: shallowRef(fvSelect),
  //   props: {
  //     placeholder: '请选择状态',
  //     clearable: true,
  //     cacheKey: 'special_fund'
  //   }
  // },
])
const tableIns = ref()
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: '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'}
                                    onDelete={() => handleDelete(row)}/>
                    : ''
              }
            </div>
        )
      }
    }
  ],
  api: '/workflow/mosr/cost/allocation',
  btns: [
    {name: '添加分摊', key: 'add', color: '#DED0B2'}
  ],
  params: {}
})

const search = (val) => {
  let obj = {...val}
  if (obj.dateValue) {
    obj.startGenerationTime = obj.dateValue[0]
    obj.endGenerationTime = obj.dateValue[1]
    delete obj.dateValue
  }
  tableConfig.params = obj
  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 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>
