<template>
  <div>
    <el-row>
      <el-col>
        <el-card id="box-card-form-1">
          <el-form
            ref="form"
            label-width="300px"
            :model="form"
            :rules="rules"
            size="medium"
          >
            <el-form-item
              label="Access-Control-Allow-Origin"

              prop="Access-Control-Allow-Origin"
            >
              <el-input
                v-model="form['Access-Control-Allow-Origin']"
                clearable
                :placeholder="__('请输入 Access-Control-Allow-Origin')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item
              label="Access-Control-Allow-Methods"

              prop="Access-Control-Allow-Methods"
            >
              <el-input
                v-model="form['Access-Control-Allow-Methods']"
                clearable
                :placeholder="__('请输入 Access-Control-Allow-Methods')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item
              label="Access-Control-Allow-Headers"

              prop="Access-Control-Allow-Headers"
            >
              <el-input
                v-model="form['Access-Control-Allow-Headers']"
                clearable
                :placeholder="__('请输入 Access-Control-Allow-Headers')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item
              label="Content-Security-Policy"

              prop="Content-Security-Policy"
            >
              <el-input
                v-model="form['Content-Security-Policy']"
                clearable
                :placeholder="__('请输入 Content-Security-Policy')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item
              label="Strict-Transport-Security"

              prop="Strict-Transport-Security"
            >
              <el-input
                v-model="form['Strict-Transport-Security']"
                clearable
                :placeholder="__('请输入 Strict-Transport-Security')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item
              label="X-Content-Type-Options"

              prop="X-Content-Type-Options"
            >
              <el-input
                v-model="form['X-Content-Type-Options']"
                clearable
                :placeholder="__('请输入 X-Content-Type-Options')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item label="X-Frame-Options"   prop="X-Frame-Options">
              <el-input
                v-model="form['X-Frame-Options']"
                clearable
                :placeholder="__('请输入 X-Frame-Options')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item label="X-XSS-Protection"  prop="X-XSS-Protection">
              <el-input
                v-model="form['X-XSS-Protection']"
                clearable
                :placeholder="__('请输入X-XSS-Protection')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item label="CSRF（Cross-site request forgery）"   prop="csrf">
              <el-input
                v-model="form.csrf"
                clearable
                :placeholder="__('请输入 CSRF（Cross-site request forgery）')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-form-item label="minify html"   prop="minify">
              <el-input
                v-model="form.minify"
                clearable
                :placeholder="__('请输入 minify html')"
                :style="{ width: '100%' }"
              />
            </el-form-item>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              style="margin-left: 300px"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit } from '@/api/base/config'

  export default {
    name: 'Security',
    components: {},
    data() {
      return {
        form: {
          'Access-Control-Allow-Origin': '',
          'Access-Control-Allow-Methods': '',
          'Access-Control-Allow-Headers': '',
          'Content-Security-Policy': '',
          'Strict-Transport-Security': '',
          'X-Content-Type-Options': '',
          'X-Frame-Options': '',
          'X-XSS-Protection': '',
          csrf: '',
          minify: '',
        },
        rules: {
          'Access-Control-Allow-Origin': [
            {
              required: true,
              message: this.__('请输入 Access-Control-Allow-Origin'),
              trigger: 'blur',
            },
          ],
          'Access-Control-Allow-Methods': [
            {
              required: true,
              message: this.__('请输入 Access-Control-Allow-Methods'),
              trigger: 'blur',
            },
          ],
          'Access-Control-Allow-Headers': [
            {
              required: true,
              message: this.__('请输入 Access-Control-Allow-Headers'),
              trigger: 'blur',
            },
          ],
          'Content-Security-Policy': [
            {
              required: true,
              message: this.__('请输入 Content-Security-Policy'),
              trigger: 'blur',
            },
          ],
          'Strict-Transport-Security': [
            {
              required: true,
              message: this.__('请输入 Strict-Transport-Security'),
              trigger: 'blur',
            },
          ],
          'X-Content-Type-Options': [
            {
              required: true,
              message: this.__('请输入 X-Content-Type-Options'),
              trigger: 'blur',
            },
          ],
          'X-Frame-Options': [
            {
              required: true,
              message: this.__('请输入 X-Frame-Options'),
              trigger: 'blur',
            },
          ],
          'X-XSS-Protection': [
            {
              required: true,
              message: this.__('请输入X-XSS-Protection'),
              trigger: 'blur',
            },
          ],
          csrf: [
            {
              required: true,
              message: this.__('请输入 CSRF（Cross-site request forgery）'),
              trigger: 'blur',
            },
          ],
          minify: [
            {
              required: true,
              message: this.__('请输入 minify html'),
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form['Access-Control-Allow-Origin'] =
          data['Access-Control-Allow-Origin']
        this.form['Access-Control-Allow-Methods'] =
          data['Access-Control-Allow-Methods']
        this.form['Access-Control-Allow-Headers'] =
          data['Access-Control-Allow-Headers']
        this.form['Content-Security-Policy'] = data['Content-Security-Policy']
        this.form['Strict-Transport-Security'] =
          data['Strict-Transport-Security']
        this.form['X-Content-Type-Options'] = data['X-Content-Type-Options']
        this.form['X-Frame-Options'] = data['X-Frame-Options']
        this.form['X-XSS-Protection'] = data['X-XSS-Protection']
        this.form['csrf'] = data['csrf']
        this.form['minify'] = data['minify']
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
<style>
#box-card-form-1 {
  overflow: unset;
}
.image_font {
  color: #979898;
  font-size: 14px;
}
</style>
