<template>
  <h3>
    策略配置
  </h3>
  <hr>
  <div class="fade-in">
    <div class="alert alert-info">
      <h4>
        自动备份数据到阿里云OSS
      </h4>
      <hr>
      <p>Bucket: emblem-archive</p>
      <p>阿里云账号: 1797148457430235</p>
      <p>范围：所有本地数据和特征</p>
      <p>OSS区域：深圳</p>
      间隔
      <input class="form-control w-50" type="number" v-model="config.auto_backup_to_oss_interval_days" />
      天
      (0 = 禁用)
      <hr>
      任务开始时刻：
      <input class="form-control w-50" type="number" v-model="config.auto_backup_start_hour"/>
      (-1 = 不限制。0-23 = 按照整点开始)
    </div>

    <div class="alert alert-info">
      <h4>
        自动归档批次
      </h4>
      <hr>
      大于
      <input class="form-control w-50" type="number" v-model="config.auto_archive_age"/>
      天
      之前的数据
      (0 = 禁用)

      <div class="my-2">
        <input type="checkbox" v-model="config.auto_archive_keep_features"/>
        <label class="ms-2">保留特征数据</label>
      </div>

      <div>
        <input type="checkbox" v-model="config.auto_archive_keep_covnerted"/>
        <label class="ms-2">保留格式转换结果</label>
      </div>
    </div>

    <div class="alert alert-info">
      <h4>
        自动磁盘擦洗
      </h4>
      <hr>
      <p>磁盘擦洗会对磁盘上的所有数据进行读取验证。此功能可以防止数据静默丢失。在有多副本的情况下，错误数据可以自动纠正。</p>
      运行频率：每
      <input class="form-control w-50" type="number" v-model="config.auto_scrub_interval_days"/>
      天
      (0 = 禁用)
    </div>

    <div v-if="msg" class="alert alert-success">
      {{ msg }}
    </div>
    <div v-if="error" class="alert alert-danger">
      {{ error }}
    </div>
    <div class="my-2">
      <button class="btn btn-primary" @click="save">保存</button>
      <button class="btn btn-secondary ms-2" @click="reload">重新加载</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'EstorConfig',
  props: [],
  components: {
  },
  data: function () {
    return {
      config: {
        auto_backup_to_oss_interval_days: 0,
        auto_backup_start_hour: -1,
        auto_archive_age: 0,
        auto_archive_keep_features: false,
        auto_archive_keep_covnerted: false,
        auto_scrub_interval_days: 0,
      },
      msg: null,
      error: null,
    };
  },
  computed: {
    config_path() {
      return this.$root.emblem_dir + "/config.json";
    }
  },
  methods: {
    async reload() {
      this.error = null;
      this.msg = null;
      var r = await this.$root.estor_get(this.config_path);
      this.config = r;
    },
    async save() {
      try {
        await this.$root.estor_put(this.config_path, this.config);
        this.error = null;
        this.msg = '保存成功';
        setTimeout(() => { this.msg = null; }, 5000);
      } catch(e) {
        this.msg = null;
        this.err = e;
      }
    },
  },
  mounted() {
    this.reload();
  },
}
</script>

<style scoped>
</style>
