<template>
    <el-card shadow="never" class="list-card">
        <Table ref="currencySettingTable"
               :tableParams="tableParams"
               :btnRole="leftBtn"
               @handleAdd="handleAdd"
               @handleEdit="handleUpdate"
               @selectionChange="handleSelectionChange"
        >
        </Table>
    </el-card>
    <!-- 添加或修改币种设置对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
        <el-form ref="currencySettingFormRef" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="币种ISO代码" prop="iso">
                <el-input v-model="form.iso" placeholder="请输入币种ISO代码"/>
            </el-form-item>
            <el-form-item label="币种符号" prop="currencySymbol">
                <el-input v-model="form.currencySymbol" placeholder="请输入币种符号"/>
            </el-form-item>
            <el-form-item label="中文名称" prop="chineseName">
                <el-input v-model="form.chineseName" placeholder="请输入中文名称"/>
            </el-form-item>
            <el-form-item label="英文名称" prop="englishName">
                <el-input v-model="form.englishName" placeholder="请输入英文名称"/>
            </el-form-item>
            <el-form-item label="汇率" prop="rate">
                <el-input type="number" v-model="form.rate" placeholder="请输入汇率"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">提交</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup name="CurrencySetting" lang="ts">
import {
    listCurrencySetting,
    getCurrencySetting,
    delCurrencySetting,
    addCurrencySetting,
    updateCurrencySetting
} from '@/api/system/currencySetting';
import {CurrencySettingVO, CurrencySettingQuery, CurrencySettingForm} from '@/api/system/currencySetting/types';
import auth from '@/plugins/auth'
import Table from '@/components/Table/index.vue'
import {tableTypes} from '@/components/Table/types'

const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const currencySettingList = ref<CurrencySettingVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);

const leftBtn = ref(
    [
        {type: 'remove', hasPermi: 'system:currencySetting:remove'},
        {type: 'add', hasPermi: 'system:currencySetting:add'},
        {type: 'edit', hasPermi: 'system:currencySetting:edit'},
        {type: 'import', hasPermi: 'system:currencySetting:import'},
        {type: 'export', hasPermi: 'system:currencySetting:export'},
    ]);

const single = ref(true);
const multiple = ref(true);
const total = ref(0);

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

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


const tableParams = reactive<tableTypes<CurrencySettingForm, CurrencySettingVO>>({
    tableColumn: [
        {show: true, label: '币种ISO代码', prop: 'iso', sortable: false},
        {show: true, label: '币种符号', prop: 'currencySymbol', sortable: false},
        {show: true, label: '中文名称', prop: 'chineseName', sortable: false},
        {show: true, label: '英文名称', prop: 'englishName', sortable: false},
        {show: true, label: '汇率', prop: 'rate', sortable: false},

        {
            show: true,
            label: '操作',
            width: 200,
            prop: 'operation',
            disabledMove: true,
            render: ({column}: { column: CurrencySettingVO }) => {
                return [
                    auth.hasPermi('system:user:demo') ? h(ElButton, {link: true, type: 'primary'}, () => 'demo') : '',
                ]
            }
        },
    ],
    keyId: 'id',
    queryParams: {
        iso: undefined,
        currencySymbol: undefined,
        chineseName: undefined,
        englishName: undefined,
        rate: undefined,
        hasTop: undefined,
        topTime: undefined,
        isDelete: undefined,
        enabled: undefined,
        ownerBy: undefined,
        sort: undefined,
    },
    listFn: listCurrencySetting,
    delFn: delCurrencySetting,
    exportCode: 'EXPORT_CURRENCY_EXCEL'
})


const initFormData: CurrencySettingForm = {
    id: undefined,
    iso: undefined,
    currencySymbol: undefined,
    chineseName: undefined,
    englishName: undefined,
    rate: undefined,
    hasTop: undefined,
    topTime: undefined,
    isDelete: undefined,
    enabled: undefined,
    ownerBy: undefined,
    sort: undefined
}
const data = reactive<PageData<CurrencySettingForm, CurrencySettingQuery>>({
    form: {...initFormData},
    rules: {
        id: [
            {required: true, message: "币种设置主键不能为空", trigger: "blur"}
        ],
        iso: [
            {required: true, message: "币种ISO代码不能为空", trigger: "blur"}
        ],
        currencySymbol: [
            {required: true, message: "币种符号不能为空", trigger: "blur"}
        ],
        chineseName: [
            {required: true, message: "中文名称不能为空", trigger: "blur"}
        ],
        englishName: [
            {required: true, message: "英文名称不能为空", trigger: "blur"}
        ],
        rate: [
            {required: true, message: "汇率不能为空", trigger: "blur"}
        ],
        hasTop: [
            {required: true, message: "是否置顶不能为空", trigger: "blur"}
        ],
        topTime: [
            {required: true, message: "置顶时间不能为空", trigger: "blur"}
        ],
        isDelete: [
            {required: true, message: "是否删除：0未删除，1已删除不能为空", trigger: "blur"}
        ],
        enabled: [
            {required: true, message: "是否启用:1:启用,0:不启用不能为空", trigger: "blur"}
        ],
        ownerBy: [
            {required: true, message: "拥有者不能为空", trigger: "blur"}
        ],
        sort: [
            {required: true, message: "显示顺序不能为空", trigger: "blur"}
        ]
    }
});

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

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

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

const currencySettingTable = ref()
/** 搜索按钮操作 */
const handleQuery = () => {
    currencySettingTable.value.getData()
}

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

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

/** 新增按钮操作 */
const handleAdd = () => {
    reset();
    dialog.visible = true;
    dialog.title = "添加币种设置";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: CurrencySettingVO) => {
    reset();
    // const _id = row?.id || ids.value[0]
    // const res = await getCurrencySetting(_id);
    Object.assign(form.value, row);
    dialog.visible = true;
    dialog.title = "修改币种设置";
}
const getList = () => {
    currencySettingTable.value.getData()
}
/** 提交按钮 */
const submitForm = () => {
    currencySettingFormRef.value?.validate(async (valid: boolean) => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.id) {
                await updateCurrencySetting(form.value).finally(() => buttonLoading.value = false);
            } else {
                await addCurrencySetting(form.value).finally(() => buttonLoading.value = false);
            }
            proxy?.$modal.msgSuccess("修改成功");
            dialog.visible = false;
            await getList();
        }
    });
}


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