<template>
  <div>
    <el-dialog
      title="日志设置"
      :visible.sync="visit"
      width="30%"
      :before-close="cancel"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="是否开启">
          <el-switch
            v-model="form.open"
            @change="cachechange"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item label="日志天数" prop="day">
          <el-select
            v-model="form.day"
            placeholder="请选择日志天数"
            size="mini"
            :disabled="form.open == 0"
          >
            <el-option
              v-for="item in days"
              :label="item"
              :value="item"
              :key="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { cache } from "@/api/system/tenant";
export default {
  props: ["visit", "id", "cacheDays", "cacheEn"],
  data() {
    return {
      form: {
        open: null,
        day: null,
      },
      rules: {
        day: [{ required: true, message: "请选择日志天数", trigger: "change" }],
      },
      days: [1, 2, 3, 4, 5, 6, 7],
    };
  },
  watch: {
    cacheDays(newV, oldV) {
      this.form.day = newV;
    },
    cacheEn(newV, oldV) {
      this.form.open = newV;
    },
  },
  methods: {
    //确认
    sure() {
      this.$refs.form.validate((valid) => {
        let obj = {
          enabled: this.form.open,
          id: this.id,
          cacheDays: this.form.day,
        };
        if (valid) {
          cache(obj).then((res) => {
            if (res.data.code == 200) {
              this.$refs.form.resetFields();
              this.$emit("sures");
            }
          });
        }
      });
    },
    //取消
    cancel() {
      this.$refs.form.resetFields();
      this.form.open = 0;
      this.$emit("cancelca");
    },
    cachechange(e) {
      this.form.open = e;
    },
  },
};
</script>
