<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="750px"
        draggable
        destroy-on-close
        :close-on-click-modal="false"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="state.rules"
          label-position="top"
          ref="formRef"
          size="default"
          label-width="100px"
          class="mt5">
        <el-form-item label="费率名称" prop="name" class="wd300">
          <el-input
              v-model="state.ruleForm.name"
              placeholder="费率名称"
              clearable
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="平台费率（0.1代表10%）" prop="feeRate" class="wd300">
          <el-input-number
              v-model="state.ruleForm.feeRate"
              placeholder="平台费率（0.1代表10%）"
              clearable
              step="0.1"
              class="w100">
          </el-input-number>
        </el-form-item>
        <el-form-item label="提现手续费率（0.006代表6‰）" prop="withdrawalFeeRate" class="wd300">
          <el-input-number
              v-model="state.ruleForm.withdrawalFeeRate"
              placeholder="提现手续费率（0.006代表6‰）"
              clearable
              step="0.001"
              class="w100">
          </el-input-number>
        </el-form-item>
      </el-form>

      <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="PlatformFeeRateDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'


// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const formRef = ref();
//定义初始变量，重置使用
const initState = () => ({
  ruleForm: {
    id: null
  },
  btnLoading: false,
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  rules: {
    name:[u.validator.required]
  },
})

// 定义变量内容
const state = reactive(initState());


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title = u.dialog.actions[action].title + "『站点平台费率』"
  state.dialog.submitTxt = u.dialog.actions[action].btn + "『站点平台费率』"
  state.dialog.isShowDialog = true;
  if (action !== 'add') {
    state.ruleForm = Object.assign(state.ruleForm, row);
  } else {
    state.ruleForm = Object.assign(state.ruleForm, row);
  }
};
// 关闭弹窗
const onClose = () => {
  state.dialog.isShowDialog = false;
  Object.assign(state, initState())
};
// 取消
const onCancel = () => {
  onClose();
};
// 提交
const onSubmit = () => {
  formRef.value.validate((v: boolean) => {
    if (v) {
      state.btnLoading = true;
      const url = !!state.ruleForm.id ? "platform-fee-rate/modify" : "platform-fee-rate/add"
      $body(url, state.ruleForm).then(() => {
        state.btnLoading = false;
        Msg.message('操作成功');
        console.log('submit!')
        onClose();
        emit('refresh');
      })
    } else {
      state.btnLoading = false;
      Msg.message('请先完整填写表单', 'error');
    }
  })
};

const handleFormChange = (formData: any) => {
  console.log(formData)
}

// 初始化数据
const loadData = (id: number) => {
  $get(`platform-fee-rate/detail/${id}`).then((res: any) => {
    state.ruleForm = res;
  })
}

// 暴露变量
defineExpose({
  open
});


</script>