<template>
  <div>
    <el-form :model="searchForm" inline @submit.prevent>
      <el-form-item>
        <el-input
          v-model="searchForm.percentageName"
          placeholder="方案名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="searchForm.status"
          placeholder="启用状态"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option label="启用" :value="0" />
          <el-option label="停用" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" v-hasPermi="['finance:commission:search']">搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['finance:commission:add']"
        >
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableList" border stripe>
      <el-table-column type="index" width="50" />
      <el-table-column label="方案名称" prop="percentageName" width="240" />
      <el-table-column label="结算方式" prop="percentageType" width="150" />
      <el-table-column label="结算规则">
        <template #default="{ row }">
          <div v-dompurify-html="row.ruleParam"></div>
        </template>
      </el-table-column>
      <el-table-column label="启用状态" prop="status" width="150">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            v-hasPermi="['finance:commission:update']"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="openForm('update', scope.row)"
            v-hasPermi="['finance:commission:update']"
          >
            修改
          </el-button>
          <el-button
            type="primary"
            link
            @click="handleDelete(scope.row.percentageId)"
            v-hasPermi="['finance:commission:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="searchForm.pageNo"
      v-model:limit="searchForm.pageSize"
      @pagination="getList"
    />

    <DialogPlan ref="planDialogRef" @success="getList" />
  </div>
</template>

<script name="CommissionPlan" setup>
import DialogPlan from './Comp/DialogPlan.vue'
import * as PlanApi from '@/api/finance/plan'
import { removeNullField } from '@/utils'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const searchForm = ref({
  percentageName: undefined,
  status: undefined,
  pageNo: 1,
  pageSize: 20
})

onMounted(() => {
  handleQuery()
})

/** 搜索按钮操作 */
const handleQuery = () => {
  searchForm.value.pageNo = 1
  getList()
}

const loading = ref(false)
const tableList = ref([])
const total = ref(0)
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PlanApi.getPlanPage(removeNullField(searchForm.value))
    tableList.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const planDialogRef = ref()
function openForm(type, row = undefined) {
  planDialogRef.value.open(type, row)
}

const handleStatusChange = async (row) => {
  try {
    // 修改状态的二次确认
    const text = row.status === 0 ? '启用' : '停用'
    await message.confirm('确认要"' + text + '""' + row.percentageName + '"方案吗?')
    // 发起修改状态
    await PlanApi.updatePlanStatus({ percentageId: row.percentageId, status: row.status })
    // 刷新列表
    await getList()
  } catch {
    // 取消后，进行恢复按钮
    row.status = row.status === 0 ? 1 : 0
  }
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PlanApi.deletePlan(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>

<style lang="scss" scoped></style>
