<template>
  <el-form :model="formData" label-width="120px" ref="ruleForm" :rules="rules" v-loading="loading" class="pr-4">
    <div class="flex flex-row justify-between">
      <div class="w-full">
        <el-form-item label="收费模式" prop="chargeType">
          <el-radio-group v-model="formData.chargeType">
            <el-radio :label="1">单集固定收费</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="起始集" prop="chargeSequence">
          <el-input-number style="width:200px;" v-model.number="formData.chargeSequence" :step="1" step-strictly
            placeholder="请输入起始集" />
          <span class="ml-3 text-gray-400">范围:1-30</span>
        </el-form-item>
        <el-form-item label="单集定价" prop="chargeCoin">
          <div class="flex items-center w-full">
            <el-input-number style="width:200px;" v-model.number="formData.chargeCoin" :step="1" step-strictly
              placeholder="请输入单集定价" />
            <span class="ml-3 text-gray-400">范围:50-300</span>
          </div>
        </el-form-item>
      </div>
    </div>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(ruleForm)">{{ $t('system.confirm') }}</el-button>
    </div>
  </el-form>
</template>

<script lang="ts" setup>
import { InfoFilled } from '@element-plus/icons-vue';
import { videoStockVideoSetChargeConfig } from '@/api/video/index'
import type { FormInstance, FormRules } from 'element-plus'
const ruleForm = ref<FormInstance>()
import { onMounted, ref } from 'vue';
const props = defineProps({
  primary: String | Number,
});
const formData = ref({ status: 2, miniProgramIds: [] })
const validateNumber = (rule, value, callback) => {
  const min = 1;
  const max = 30;

  if (value < min || value > max) {
    callback(new Error(`范围必须在 ${min} 到 ${max} 之间`));
  } else {
    callback();
  }
};

const validateNumberCoin = (rule, value, callback) => {
  const min = 50;
  const max = 300;

  if (value < min || value > max) {
    callback(new Error(`范围必须在 ${min} 到 ${max} 之间`));
  } else {
    callback();
  }
};
const rules = reactive({
  chargeType: [{ required: true, message: '请选择收费模式', trigger: 'change' }],
  chargeSequence: [
    {
      required: true,
      message: '请输入起始集',
      trigger: 'blur'
    },
    { validator: validateNumber, trigger: 'blur' },
  ],
  chargeCoin: [
    {
      required: true,
      message: '请输入单集定价',
      trigger: 'blur'
    },
    { validator: validateNumberCoin, trigger: 'blur' },
  ]

});
const loading = ref(false)

const submitForm = (formEl: FormInstance | undefined) => {
  console.log(formData.value, 'formData.valueformData.value');
  if (!formEl) return;
  formEl
    .validate(valid => {
      if (valid) {
        videoStockVideoSetChargeConfig({ ...formData.value }).then(res => {
          console.log(res);
          ElMessage.success(res.message)
          emit('close')
        })
      } else {
        loading.value = false;
      }
    })
    .then(() => { });
}

if (props.primary) {
  formData.value.id = props.primary.id
  formData.value.chargeCoin = props.primary.charge_coin
  formData.value.chargeType = props.primary.d_charge_type
  formData.value.chargeSequence = props.primary.charge_sequence
}
const emit = defineEmits(['close']);
onMounted(() => {

});
</script>
