<template>
  <div>
    <ListTemplate ref="listTemplateRefs">
      <template #button>
        <m-button type="primary" icon="el-icon-plus" @click="onAdd">新增</m-button>
      </template>
      <template #actions="{ row }">
        <m-button text type="primary" size="small" @click="onEdit(row)">编辑</m-button>
        <m-button text type="primary" size="small" @click="onEditRules(row)">规则配置</m-button>
        <m-button text type="danger" size="small" @click="onDelete(row)">删除</m-button>
        <m-button text type="primary" size="small" @click="onHistory(row)">历史记录</m-button>
      </template>
    </ListTemplate>
    <WelfareEdit ref="welfareEditRefs" @refresh="onInit"></WelfareEdit>
    <WelfareRules ref="welfareRulesRefs" @refresh="onInit"></WelfareRules>
    <HistoryDialog ref="historyDialogRefs">
      <template #actions="{ row }">
        <m-button type="primary" text @click="$emit('history', row)" size="small">查看配置规则</m-button>
      </template>
    </HistoryDialog>
  </div>
</template>

<script>
import ListTemplate from '../components/ListTemplate.vue'
import {
  deleteWelfare
} from '@/api/welfare'
import WelfareEdit from './welfareEdit.vue'
import WelfareRules from './welfareRules'
import HistoryDialog from './historyDialog'
export default {
  name: 'human-resources-welfare-list',
  components: {
    ListTemplate,
    WelfareEdit,
    WelfareRules,
    HistoryDialog
  },
  mounted () {
    this.$emit('mounted')
  },
  methods: {
    onInit () {
      this.$refs.listTemplateRefs.onInit()
    },
    onEdit (item) {
      this.$refs.welfareEditRefs.open(item)
    },
    onAdd () {
      this.$refs.welfareEditRefs.open()
    },
    onEditRules (item) {
      this.$refs.welfareRulesRefs.open(item)
    },
    onDelete (item) {
      this.$confirm(`确定删除${item.subsidyName}吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await deleteWelfare({ subsidyId: item.subsidyId })
          this.$message.success('删除成功')
          this.onInit()
        } catch (error) {
          this.$message.error(error)
        }
      }).catch(_ => {})
    },
    onHistory (row) {
      this.$refs.historyDialogRefs.open(row.uuid, row.subsidyName)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
