<template>
  <div class="flex flex-col h-full p-2">
    <pageheader></pageheader>
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" @submit.prevent="handleQuery">
            <el-form-item label="项目名称" prop="itemName">
              <el-input v-model="queryParams.itemName" placeholder="请输入项目名称" clearable @keyup.enter.prevent="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-edit="checkPermi(['chargeItem:chargeItem:edit'])"
            :show-add="checkPermi(['chargeItem:chargeItem:add'])"
            :show-delete="checkPermi(['chargeItem:chargeItem:remove'])"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="chargeItemList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目名称" align="center" prop="itemName" />
        <el-table-column label="计费方式" align="center" prop="billingType">
          <template #default="scope">
            <dict-tag :options="com_charge_item_method" :value="scope.row.billingType"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="计算公式" align="center" prop="formula">
          <template #default="scope">
            <dict-tag :options="com_charge_item_formula" :value="scope.row.formula"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" prop="fixedPrice">
          <template #default="scope">
            {{ scope.row.price }}
            <dict-tag :options="com_charge_item_unit" :value="scope.row.fixedPrice"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="计量单位" align="center" prop="unit">
          <template #default="scope">
            <dict-tag :options="com_charge_item_unit" :value="scope.row.unit"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="计算精度" align="center" prop="accuracy">
          <template #default="scope">
            <dict-tag :options="com_charge_item_precision" :value="scope.row.accuracy"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="进位方式" align="center" prop="carryMethod">
          <template #default="scope">
            <dict-tag :options="com_charge_item_carry_method" :value="scope.row.carryMethod"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-switch
              @change="(val: string) => handleSwitch(val, scope.row)"
              inactive-value="1"
              active-value="0"
              v-model="scope.row.status"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['chargeItem:chargeItem:edit']">编辑</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['chargeItem:chargeItem:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="ChargeItem" lang="ts">
import { listChargeItem, getChargeItem, delChargeItem, addChargeItem, updateChargeItem, changChargeItem } from '@/api/system/SdbChargeItem';
import { ChargeItemVO, ChargeItemQuery, ChargeItemForm } from '@/api/system/SdbChargeItem/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_charge_item_method } = toRefs<any>(proxy?.useDict('com_charge_item_method'));
const { com_charge_item_unit } = toRefs<any>(proxy?.useDict('com_charge_item_unit'));
const { com_charge_item_precision } = toRefs<any>(proxy?.useDict('com_charge_item_precision'));
const { com_charge_item_carry_method } = toRefs<any>(proxy?.useDict('com_charge_item_carry_method'));
const { com_charge_item_formula } = toRefs<any>(proxy?.useDict('com_charge_item_formula'));

const chargeItemList = ref<ChargeItemVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const chargeItemFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ChargeItemForm = {
  id: undefined,
  itemName: undefined
};
const data = reactive<PageData<ChargeItemForm, ChargeItemQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    itemName: undefined
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询收费项目管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listChargeItem(queryParams.value);
  chargeItemList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  chargeItemFormRef.value?.resetFields();
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: ChargeItemVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const router = useRouter();
/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/sdb/addchargeItem'
  });
};

/** 修改按钮操作 */
const handleUpdate = async (row?: ChargeItemVO) => {
  router.push({
    path: '/sdb/editchargeItem',
    query: {
      id: row.id
    }
  });
};

const handleSwitch = (val: string, row: ChargeItemVO) => {
  // 1停用 0 启用
  updateChargeItem({
    id: row.id,
    status: val
  }).then(() => {
    ElMessage.success('修改成功');
  });
};
/** 删除按钮操作 */
const handleDelete = async (row?: ChargeItemVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除该收费项目？').finally(() => (loading.value = false));
  await delChargeItem(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'chargeItem/chargeItem/export',
    {
      ...queryParams.value
    },
    `chargeItem_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
</style>
