<template>
    <div>
        <pro-dialog :visible="$attrs.visible" @close="$emit('close:modal')" :title="title" width="70%" :closable="true"
            :destroyOnClose="true" :maskClosable="true">
            <template v-slot:footer="">
                <pro-button id="f3dd65a2-c52b-11ea-87c3-ff36a904ecfe" @click="$emit('close:modal')"
                    :icon="h(CloseOutlined)">关闭</pro-button>
                <pro-button id="f3dd65a4-c52b-11ea-87c3-ff36a904ecfe" @click="saveHandler_d7fae64c"
                    :icon="h(SaveOutlined)">保存</pro-button>
            </template>
            <pro-form ref="formRef_a6c2da04" :model="formData_a6c2da04" :rules="formRules_a6c2da04" label-width="120px">
                <pro-form-grid :header="{ title: '币种信息' }" colNum="4">
                    <pro-input v-show="false" :ref="(el) => formRefs_a6c2da04.no = el"
                        :formItemProps="{ label: 'no', prop: 'no' }" v-model:value="formData_a6c2da04.no"
                        placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-input :ref="(el) => formRefs_a6c2da04.key = el" :formItemProps="{ label: '币种编号', prop: 'key' }"
                        v-model:value="formData_a6c2da04.key" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-input :ref="(el) => formRefs_a6c2da04.name = el" :formItemProps="{ label: '名称', prop: 'name' }"
                        v-model:value="formData_a6c2da04.name" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-input :ref="(el) => formRefs_a6c2da04.epn = el" :formItemProps="{ label: '国际编码', prop: 'epn' }"
                        v-model:value="formData_a6c2da04.epn" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-input :ref="(el) => formRefs_a6c2da04.sbl = el" :formItemProps="{ label: '币种符号', prop: 'sbl' }"
                        v-model:value="formData_a6c2da04.sbl" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-input :ref="(el) => formRefs_a6c2da04.show_seqn = el"
                        :formItemProps="{ label: '展示序号', prop: 'show_seqn' }"
                        v-model:value="formData_a6c2da04.show_seqn" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-select :ref="(el) => formRefs_a6c2da04.correct_yn = el"
                        :formItemProps="{ label: '是否是折算币种', prop: 'correct_yn' }"
                        v-model:value="formData_a6c2da04.correct_yn" :editable="false" placeholder="请选择"
                        :allowClear="true" tableName="com_usertype_info" colName="correct_yn"
                        :optionData="yesOrNo"></pro-select>
                    <pro-select :ref="(el) => formRefs_a6c2da04.use_yn = el"
                        :formItemProps="{ label: '启用标志', prop: 'use_yn' }" v-model:value="formData_a6c2da04.use_yn"
                        :editable="false" placeholder="请选择" :allowClear="true" tableName="com_usertype_info"
                        colName="use_yn"></pro-select>
                    <pro-input :ref="(el) => formRefs_a6c2da04.brf = el" :formItemProps="{ label: '备注', prop: 'brf' }"
                        v-model:value="formData_a6c2da04.brf" placeholder="请输入" :allowClear="true"></pro-input>
                    <pro-select :ref="(el) => formRefs_a6c2da04.default_yn = el"
                        :formItemProps="{ label: '是否是本位币', prop: 'default_yn' }"
                        v-model:value="formData_a6c2da04.default_yn" :editable="false" placeholder="请选择"
                        :allowClear="true" tableName="com_usertype_info" colName="default_yn"
                        :optionData="yesOrNo"></pro-select>
                    <pro-input v-show="false" :ref="(el) => formRefs_a6c2da04.formtype = el"
                        :formItemProps="{ label: 'formtype', prop: 'formtype' }"
                        v-model:value="formData_a6c2da04.formtype"></pro-input>
                    <pro-input v-show="false" :ref="(el) => formRefs_a6c2da04.oldKey = el"
                        :formItemProps="{ label: 'oldKey', prop: 'oldKey' }"
                        v-model:value="formData_a6c2da04.oldKey"></pro-input>

                </pro-form-grid>
                <pro-form-grid :header="{ title: '折算币种' }" colNum="3">
                    <div>
                        <pro-table ref="tableRef_28fa7a23" :columns="columns_28fa7a23" :showPagination="false"
                            :dataSource="conversion">
                            <template v-slot:header="">
                                <pro-button id="4b7facf0-94a4-11ee-9af3-e137392c498c" text="增加一行"
                                    :icon="h(PlusOutlined)" icon="plus" type="primary"
                                    @click="addRows">增加一行</pro-button>
                            </template></pro-table>
                    </div>
                </pro-form-grid>
            </pro-form>
        </pro-dialog>
    </div>
</template>
<script setup lang="jsx">
import { ref, onMounted, computed, h, useAttrs, watch } from 'vue';
import settings from '@/settings';
import request from '@/utils/request';
import { useStore } from 'vuex';
import { CloseOutlined, SaveOutlined, DeleteOutlined, PlusOutlined } from "@ant-design/icons-vue";
import { Modal } from "ant-design-vue";
const store = useStore();
const attrs = useAttrs();
const emit = defineEmits(['close:modal'])
const yesOrNo = ref([{
    item_name: "是",
    item_no: "Y"
}, {
    item_name: "否",
    item_no: "N"
}]);
const systemPrefix = settings.systemPrefix;
const addRows = () => {
    store.dispatch("CurrencyManager/addRows", {
        name: formData_a6c2da04.value.name
    });
};
const deleteRows = e => {
    store.dispatch("CurrencyManager/deleteRows", {
        e
    });
};
const formData_a6c2da04 = ref({
    key: "",
    name: "",
    epn: "",
    sbl: "",
    show_seqn: "",
    correct_yn: "N",
    use_yn: "Y",
    brf: "",
    default_yn: "N",
    formtype: "1",
    oldKey: ""
});
const formRules_a6c2da04 = ref({
    key: [{
        required: true,
        trigger: "change"
    }, {
        validator: addPageOnChange,
        trigger: "change"
    }],
    name: [{
        required: true,
        trigger: "change"
    }]
});
const formRef_a6c2da04 = ref(null);
const formRefs_a6c2da04 = ref({
    key: "",
    name: "",
    epn: "",
    sbl: "",
    show_seqn: "",
    correct_yn: "",
    use_yn: "",
    brf: "",
    default_yn: "",
    formtype: "",
    oldKey: ""
});
const tableRef_28fa7a23 = ref(null);
const columns_28fa7a23 = ref([{
    prop: "ActionColumn",
    label: "操作",
    render: (scope) => {
        return <div>
            <pro-button id="userDelete" onClick={() => confirmHandler_0c5b805d(scope)} icon={h(DeleteOutlined)}>删除</pro-button>
        </div>
    }
}, {
    prop: "key",
    label: "折算币种编号",
    align: "center",
    width: "120px",
    editConfig: {
        el: "input"
    }
}, {
    prop: "name",
    label: "折算币种名称",
    align: "center",
    width: "120px",
    editConfig: {
        el: "input"
    }
}, {
    prop: "from_cur_no",
    label: "原币种",
    align: "center"
}, {
    prop: "to_cur_no",
    label: "折算后币种",
    align: "center",
    width: "120px"
}, {
    prop: "use_yn",
    label: "启用标志",
    align: "center",
    render: scop => useYn(scop.record.use_yn, scop.record)
}, {
    prop: "show_seqn",
    label: "展示顺序",
    align: "center",
    editConfig: {
        el: "input"
    }
}, {
    prop: "brf",
    label: "备注",
    align: "center",
    editConfig: {
        el: "input"
    }
}]);
function saveHandler_d7fae64c () {
    //校验表单
    formRef_a6c2da04.value.getOrignRef().validate().then(values => {
        //表单校验成功
        //执行保存回调
        store.dispatch('CurrencyManager/save', {
            values,
            closeMethod: () => { emit("close:modal") }
        });
    });
}
watch(() => formData_a6c2da04.use_yn, (value, oldValue) => {
    const no = actions.getFieldState('no', state => state.value);
    // if ('N' == value && no != undefined) { // 停用
    //     const action = {
    //         type: 'UserTypeInfoManager/changeValidate',
    //         payload: { no },
    //     };
    //     DvaDispatch(action);
    // }
});
onMounted(() => {
    if (formData_a6c2da04.value.name) {
        //修改
        formData_a6c2da04.value.formtype = '2';
        let oldKey = formData_a6c2da04.value.key;
        formData_a6c2da04.value.oldKey = oldKey;
    }
    formData_a6c2da04.value = store.state.CurrencyManager.formData
});
watch(() => store.state.CurrencyManager.formData, (value, oldValue) => {
    formData_a6c2da04.value = value;
});
var addPageOnChange = async (_rule, key) => {
    if (key == null || key == '' || key == undefined) { } else {
        let ss = '';
        ss = await util.validateRecord("1", 'currency', "key", key);
        let formtype = actions.getFieldState('formtype', s => s.value);
        if ('2' == formtype) {
            //修改页面校验重复
            let oldKey = actions.getFieldState('oldKey', s => s.value);
            if (key == oldKey) {
                ss = '2';
            }
        }
        return new Promise(resolve => {
            if (ss == '0001') {
                resolve('该币种编号已存在!');
            } else {
                resolve('');
            }
        });
    }
};
function confirmHandler_0c5b805d (scope) {
    Modal.confirm({
        title: '删除确认',
        content: '确认删除选中的记录',
        cancelText: '删除',
        onOk () {
            store.dispatch('CurrencyManager/deleteRows', { ...scope.record });
        }
    });
}
const conversion = computed(() => store.state.CurrencyManager.conversion);
const title = computed(() => store.state.CurrencyManager.title);
var showUseYn = (text, record) => {
    let value = false;
    if (record.use_yn == 'Y') {
        value = true;
    }
    return <OptColumn type="Switch" checkedChildren="启用" unCheckedChildren="未启用" Coldata={record} defaultChecked={value} checked={value} onChange={(value, record) => changeUseYn(value, record)} />;
};
var renderStard = (e, ww, userNo, data, conversion, insertOrModify) => {
    let resData = [...data];
    if (insertOrModify == 1) {
        const toCurNo = conversion.records.filter(item => item.to_cur_no != e);
        if (toCurNo.length == 0) {
            resData = data;
        } else {
            toCurNo.forEach(item => {
                data.forEach((it, index) => {
                    if (it.item_no == item.to_cur_no) {
                        resData.splice(index, 1);
                    }
                });
            });
        }
    } else {
        const toCurNo = conversion.records.filter(item => item.to_cur_no != e);
        toCurNo.forEach(item => {
            resData.forEach((it, index) => {
                if (it.item_no == item.to_cur_no) {
                    resData.splice(index, 1);
                }
            });
        });
    }
    return <Select placeholder="请选择" onSelect={(value, record) => setStardList(value, record, ww.rowsId, ww.no)} defaultValue={e}>
        {resData.map(it => <Select.Option key={it.item_name} value={it.item_no}>
            {it.item_name}
        </Select.Option>)}
    </Select>;
};
var useYn = (text, record) => {
    if (record.use_yn == 'Y') {
        return "启用";
    } else {
        return "未启用";
    }
}
</script>
