<template>
  <div class="dislogTipBox">
    <BaseDialog
      width="900px"
      :isShow.sync="isShow"
      :title="(orderSnList ? '批量' : '') + '催款提醒'"
      @close="close"
      @submit="submitForm"
      @open="init"
    >
      <el-form
        inline
        hide-required-asterisk
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
      >
        <div>
          <el-form-item label="最新提醒：" v-if="!orderSnList">
            {{ $methodsTools.onlyForma(orderInfo.lastTime) || "--" }}
          </el-form-item>
        </div>
        <div>
          <el-form-item prop="exceedType" label="预付超期：">
            <el-select v-model="form.exceedType" placeholder="请选择预付超期">
              <el-option label="7天" :value="1"></el-option>
              <el-option label="30天" :value="2"></el-option>
              <el-option label="半年" :value="3"></el-option>
              <el-option label="1年" :value="4"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <div>
          <el-form-item prop="noteType" label="提醒设置：">
            <el-select v-model="form.noteType" placeholder="请选择提醒类型">
              <el-option
                label="取消提醒"
                v-if="orderInfo.noteId"
                :value="0"
              ></el-option>
              <el-option label="立即提醒" :value="1"></el-option>
              <el-option label="每天提醒" :value="2"></el-option>
              <el-option label="每周提醒" :value="3"></el-option>
              <el-option label="每月提醒" :value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="weekTime" label="" v-if="form.noteType == 3">
            <el-select
              v-model="form.weekTime"
              multiple
              placeholder="请选择星期"
              key="3"
            >
              <el-option
                v-for="week in weekList"
                :key="week.value"
                :label="week.label"
                :value="week.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="monthTime" label="" v-if="form.noteType == 4">
            <el-select
              v-model="form.monthTime"
              multiple
              placeholder="请选择日期"
              key="4"
            >
              <el-option
                v-for="day in 31"
                :key="day"
                :label="day"
                :value="day"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="dayTime" label="" v-if="form.noteType > 1"
            ><el-time-picker
              format="HH:mm"
              value-format="HH:mm"
              v-model="form.dayTime"
              placeholder="选择时间"
            >
            </el-time-picker>
          </el-form-item>
        </div>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
import {
  orderNote,
  orderNoteEdit,
  editBatchReminder,
} from "@/api/financed/index";
export default {
  name: "DislogTip",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    orderInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      form: {},
      rules: {
        dayTime: [{ required: true, message: "请选择时间", trigger: "change" }],
        weekTime: [
          { required: true, message: "请选择星期", trigger: "change" },
        ],
        monthTime: [{ required: true, message: "请选择日期", trigger: "blur" }],
      },
      weekList: [
        { label: "星期一", value: "1" },
        { label: "星期二", value: "2" },
        { label: "星期三", value: "3" },
        { label: "星期四", value: "4" },
        { label: "星期五", value: "5" },
        { label: "星期六", value: "6" },
        { label: "星期日", value: "7" },
      ],
      dayList: [],
      value: [],
    };
  },

  mounted() {},

  methods: {
    init() {
      this.resetForm();
    },
    close() {
      this.$refs["form"].resetFields();
    },
    submit() {},
    resetForm() {
      let {
        noteId,
        orderSn,
        tenantId,
        dayTime,
        weekTime,
        monthTime,
        noteType,
        exceedType,
      } = this.orderInfo;
      this.form = {
        noteType: noteType || 1,
        id: noteId,
        dayTime: dayTime,
        weekTime: weekTime ? weekTime.split(",") : [],
        monthTime: monthTime ? monthTime.split(",") : [],
        orderSn,
        tenantId,
        exceedType,
      };
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let form = JSON.parse(JSON.stringify(this.form));
          let { noteType, weekTime, monthTime } = form;
          delete form.weekTime;
          delete form.monthTime;
          if (noteType == 3) {
            form.weekTime = weekTime.toString();
          }
          if (noteType == 4) {
            form.monthTime = monthTime.toString();
          }
          if (noteType == 0) {
            form.status = -1;
            form.noteType = undefined;
          }
          if (this.orderSnList) {
            form = this.orderSnList.map((e) => {
              return { ...form, ...e };
            });
            editBatchReminder(form).then((res) => {
              this.$message.success("批量催款提醒设置成功");
              this.isShow = false;
              this.$emit("search");
            });
            return;
          }
          if (form.id) {
            orderNoteEdit(form).then((res) => {
              this.$message.success("催款提醒修改成功");
              this.isShow = false;
              this.$emit("search");
            });
          } else {
            orderNote(form).then((res) => {
              this.$message.success("催款提醒设置成功");
              this.isShow = false;
              this.$emit("search");
            });
          }
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
    orderSnList() {
      return this.orderInfo.orderSnList;
    },
  },
};
</script>
