<template>
  <div>
    <ListTemplate ref="listTemplateRefs" :card-title="$attrs.label" v-loading="loading">
      <template #tool>
        <slot name="tool"></slot>
      </template>
      <template #actions="{ row }">
        <m-button text type="primary" size="small" @click="onOpen('salarySolutionDetailsRefs', row)">查看</m-button>
        <slot name="actions" :row="row"></slot>
        <m-button text type="primary" size="small" @click="onOpen('salarySolutionHistory', row)">版本记录</m-button>
        <m-button text type="danger" size="small" @click="onDelete(row)">删除</m-button>
      </template>
    </ListTemplate>
    <SalarySolutionDetails ref="salarySolutionDetailsRefs"></SalarySolutionDetails>
    <SalarySolutionHistory ref="salarySolutionHistory">
      <template #actions="{ row }">
        <m-button type="primary" text @click="$emit('history', row)" size="small">查看规则配置</m-button>
      </template>
    </SalarySolutionHistory>
  </div>
</template>

<script>
import {
  deleteSolution,
  sendSalaryRelease
} from '@/api/salary'
import ListTemplate from '../components/ListTemplate.vue'
import SalarySolutionHistory from './salarySolutionHistory.vue'
import SalarySolutionDetails from './salarySolutionDetails.vue'
export default {
  name: 'salary-solution-list',
  components: {
    ListTemplate,
    SalarySolutionHistory,
    SalarySolutionDetails
  },
  data () {
    return {
      loading: false
    }
  },
  mounted () {
    this.$emit('mounted')
  },
  methods: {
    onInit () {
      this.$refs.listTemplateRefs.onInit()
    },
    async onSend (row) {
      this.loading = true
      try {
        await sendSalaryRelease({ performanceSolutionId: row.performanceSolutionId })
        this.$message.success('发布成功')
        this.$refs.listTemplateRefs.onInit()
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onOpen (ref, item) {
      this.$refs[ref]?.open && this.$refs[ref].open(item)
    },
    onDelete (item) {
      this.$confirm(`确定删除${item.title}吗？`, '提示').then(async () => {
        try {
          await deleteSolution({ performanceSolutionId: item.performanceSolutionId })
          this.$message.success('删除成功')
          this.onInit()
        } catch (error) {
          this.$message.error(error)
        }
      }).catch(_ => {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
