<style scoped lang="scss">

</style>
<template>
  <div class="system-dialog-container">
    <el-dialog
        :title="state.dialog.title"
        v-model="state.dialog.isShowDialog"
        width="820px"
        draggable
        destroy-on-close
        :close-on-click-modal="false"
        @close="onClose"
        align-center>
      <el-form
          inline
          :model="state.ruleForm"
          :rules="state.rules"
          ref="formRef"
          size="default"
          label-width="125px"
          class="mt5">
<!--        <el-form-item label="主活动" prop="activityId">
          <ext-select
              v-model="state.ruleForm.activityId"
              placeholder="关联活动"
              url="activity"
              url-method="get"
              clearable
              class="wd200">
          </ext-select>
        </el-form-item>-->
        <el-form-item label="banner图片" prop="bannerUrl">
          <ext-upload v-model="state.ruleForm.bannerUrl"></ext-upload>
<!--          <el-input-->
<!--              v-model.trim="state.ruleForm.bannerUrl"-->
<!--              placeholder="banner图片地址"-->
<!--              clearable-->
<!--              class="wd200">-->
<!--          </el-input>-->
        </el-form-item>
        <el-form-item label="描述" prop="bannerDesc"   class="w100">
          <el-input
              v-model.trim="state.ruleForm.bannerDesc"
              placeholder="描述"
              maxlength="200"
              show-word-limit
              type="textarea"
              :rows="2"
              clearable
              class="w100">
          </el-input>
        </el-form-item>

        <el-form-item label="关联跳转地址" prop="linkUrl"  class="w100">
          <el-input
              v-model.trim="state.ruleForm.linkUrl"
              placeholder="关联跳转地址"
              clearable
              class="w100">
          </el-input>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <ext-date-picker
              v-model.trim="state.ruleForm.startTime"
              placeholder="开始时间"
              type="datetime"
              clearable
              class="wd200">
          </ext-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <ext-date-picker
              v-model.trim="state.ruleForm.endTime"
              placeholder="结束时间"
              type="datetime"
              clearable
              class="wd200">
          </ext-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <ext-d-select
              v-model.trim="state.ruleForm.status"
              type="Banner.status"
              placeholder="状态"
              clearable
              class="wd200">
          </ext-d-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" class="w100">
          <el-input
              maxlength="500"
              show-word-limit
              type="textarea"
              :rows="3"
              v-model.trim="state.ruleForm.remark"
              placeholder="备注信息"
              clearable
              class="w100">
          </el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <div 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>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="BannerDialog">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {Msg} from "/@/utils/message";
import {$body, $get} from "/@/utils/request";
import u from '/@/utils/u'
import ExtSelect from "/@/components/form/ExtSelect.vue";
import ExtUpload from "/@/components/form/ExtUpload.vue";
import ExtDatePicker from "/@/components/form/ExtDatePicker.vue";
import ExtDSelect from "/@/components/form/ExtDSelect.vue";


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

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


// 打开弹窗
const open = (action: string = 'add', row: any) => {
  state.dialog.title = u.dialog.actions[action].title + "『banner配置』"
  state.dialog.submitTxt = u.dialog.actions[action].btn + "『banner配置』"
  state.dialog.isShowDialog = true;
  if (action !== 'add') {
    loadData(row.id);
  } 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((valid:boolean) => {
    if(valid){
      if(new Date(state.ruleForm.startTime).getTime()>new Date(state.ruleForm.endTime).getTime()){
        Msg.message("开始时间不能晚于结束时间","error")
        return false;
      }
      state.btnLoading = true;
      const url = state.ruleForm.id > 0 ? "banner/modify" : "banner/add"
      $body(url, state.ruleForm).then(() => {
        state.btnLoading = false;
        Msg.message('操作成功');
        console.log('submit!')
        onClose();
        emit('refresh');
      })
    }
  }).catch(() => {
    state.btnLoading = false;
    Msg.message('请先完整填写表单', 'error');
  })
};

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

// 初始化表格数据
const loadData = (id: number) => {
  $get(`banner/${id}`).then((res: any) => {
    state.ruleForm = res;
  })
}

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


</script>