<template>
  <div>
    <el-dialog :close-on-click-modal="false" title="修改备份策略" :visible.sync="addData.dialog" width="60%" append-to-body>
      <el-form label-width="120px" :model="addData.data" ref="data">
        <cb-form-item label="自动备份：">
          <el-switch disabled v-model="back" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </cb-form-item>
        <cb-form-item label="保留天数：" prop="cloudRds.keepDays" validate="required" required-message="请输入名称">
          <el-input-number v-model="addData.data.cloudRds.keepDays" class="w" :min="1" :max="732"></el-input-number>
          <div class="mini-txt">设置备份保留天数，可设置时间范围为1-732天</div>
        </cb-form-item>
        <cb-form-item label="备份时间：" prop="cloudRds.startTime" validate="required">
          <el-select v-model="addData.data.cloudRds.startTime" filterable>
            <el-option v-for="(item, index) in list" :key="index" :label="item" :value="item"></el-option>
          </el-select>
          <div class="mini-txt">备份时间段以UTC时区保存。如果碰到夏令时/冬令时切换，备份时间段会因时区变化而改变</div>
        </cb-form-item>
        <cb-form-item label="备份周期：" prop="cloudRds.period" validate="required">
          <el-checkbox-group v-model="addData.data.cloudRds.period">
            <el-checkbox :label="item.value" v-for="(item, index) in week" :key="index">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
          <div class="mini-txt">备份周期至少选择一天</div>
        </cb-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="ghost" @click="addData.dialog = false">取 消</el-button>
        <el-button type="primary" @click="ok" :loading="loading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/* global $ */
import { modifyRdsBackUpPolicy } from 'views/hcso/services/hcso.js'
import { forIn } from 'lodash'

export default {
  props: {
    addData: {
      type: Object,
      default: function () {
        return {
          data: {
            labelIds: []
          },
          dialog: false
        }
      }
    }
  },
  data() {
    return {
      loading: false,
      back: true,
      week: [
        { name: '星期一', value: '1' },
        { name: '星期二', value: '2' },
        { name: '星期三', value: '3' },
        { name: '星期四', value: '4' },
        { name: '星期五', value: '5' },
        { name: '星期六', value: '6' },
        { name: '星期日', value: '7' }
      ],
      list: [
        '00:00-01:00',
        '01:00-02:00',
        '02:00-03:00',
        '03:00-04:00',
        '04:00-05:00',
        '05:00-06:00',
        '06:00-07:00',
        '07:00-08:00',
        '08:00-09:00',
        '09:00-010:00',
        '010:00-011:00',
        '11:00-12:00',
        '12:00-13:00',
        '13:00-14:00',
        '14:00-15:00',
        '15:00-16:00',
        '16:00-17:00',
        '17:00-18:00',
        '18:00-19:00',
        '19:00-20:00',
        '20:00-21:00',
        '21:00-22:00',
        '22:00-23:00',
        '23:00-00:00'
      ]
    }
  },
  methods: {
    ok() {
      this.$refs.data.validate(valid => {
        if (valid) {
          console.log(this.addData)
          modifyRdsBackUpPolicy({
            action: 'setBackupPolicy',
            id: this.addData.data.id,
            cloudRds: {
              backupPolicy: {
                keepDays: this.addData.data.cloudRds.keepDays,
                startTime: this.addData.data.cloudRds.startTime,
                period: this.addData.data.cloudRds.period.join(',')
              }
            }
          }).then(data => {
            if (data.success) {
              this.$message({
                type: 'success',
                message: data.message
              })
              this.addData.dialog = false
              this.$emit('back')
            }
            this.loading = false
          })
        }
      })
    }
  },
  created() {}
}
</script>

<style scoped>
.mini-txt {
  font-size: 12px;
}
</style>
