<template>
  <div>
    <el-dialog :title="!editMode ? '新建配置项' : '编辑配置项' " :visible.sync="isShow" @open="dialogOpen" class="site-config-item-dialog">
      <el-form ref="editSiteConfigForm" :model="tmpSiteConfig" :rules="siteConfigFormRules" label-width="130px">
          <el-form-item label="配置项代码：" prop="code">
            <el-input v-model="tmpSiteConfig.code" :disabled="editMode" class="form-input"></el-input>
          </el-form-item>
          <el-form-item label="配置项名称：" prop="name">
            <el-input v-model="tmpSiteConfig.name" class="form-input"></el-input>
            <!-- <el-button @click="langClick" type="text"><i class="fa fa-file-word-o fa-fw"></i></el-button> -->
          </el-form-item>
          <el-form-item label="配置项值：" prop="value">
            <el-input v-model="tmpSiteConfig.value" class="form-input"></el-input>
          </el-form-item>
          <el-form-item label="备注：" prop="memo">
            <el-input v-model="tmpSiteConfig.memo" class="form-input"></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import util from '@src/common/util.js'
export default {
  data() {
    var validateCodeOfSiteConfig = (rule, value, callback) => {
      if (!value || value.trim() === '') {
        callback(new Error('请输入配置项代码'))
      } else {
        if (value.length >= 40) {
          callback(new Error('长度必须小于40个字符'))
        }
        callback()
      }
    }
    // 验证配置项名称
    var validateNameOfSiteConfig = (rule, value, callback) => {
      if (!value || value.trim() === '') {
        callback(new Error('请输入配置项名称'))
      } else {
        if (value.length >= 120) {
          callback(new Error('长度必须小于120个字符'))
        }
        callback()
      }
    }
    // 验证配置项值
    var validateValueOfSiteConfig = (rule, value, callback) => {
      if (!value || value.trim() === '') {
        callback(new Error('请输入配置项值'))
      } else {
        if (value.length >= 1000) {
          callback(new Error('长度必须小于1000个字符'))
        }
        callback()
      }
    }
    // 验证备注
    // function validateMemoOfSiteConfig (rule, value, callback) {
    //   if (!value || value.trim() === '') {
    //     callback(new Error('请输入配置项代码'))
    //   } else {
    //     if (value.length >= 120) {
    //       callback(new Error('长度必须小于120个字符'))
    //     }
    //     callback()
    //   }
    // }
    return {
      tmpSiteConfig: {},
      siteConfigFormRules: {
        code: [
          {
            required: true,
            validator: validateCodeOfSiteConfig,
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            validator: validateNameOfSiteConfig,
            trigger: 'blur'
          }
        ],
        value: [
          {
            required: true,
            validator: validateValueOfSiteConfig,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    isShow: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    },
    editMode() {
      return !!this.code
    }
  },
  methods: {
    async dialogOpen() {
      let res = await axios.get(
        `/ui/sites/${this.siteID}/configs/initdialog`,
        {
          params: {
            code: this.code
          }
        }
      )
      this.tmpSiteConfig = res.data.data
    },
    langClick() {
    // console.log('国际化弹框')
    },
    async confirm() {
      try {
        await util.validateForm(this.$refs['editSiteConfigForm'])
      } catch (e) {
        util.showErrorMessageBox(e)
        return
      }
      if (this.code) {
        let res = await axios.put(
          `/ui/sites/${this.tmpSiteConfig.siteID}/configs`,
          this.tmpSiteConfig
        )
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.$emit('callback')
          this.isShow = false
        }
      } else {
        let res = await axios.post(
          `/ui/sites/${this.tmpSiteConfig.siteID}/configs`,
          this.tmpSiteConfig
        )
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          this.$emit('callback')
          this.isShow = false
        }
      }
    }
  },
  props: {
    siteID: {
      type: Number,
      default() {
        return 0
      }
    },
    code: {
      type: String
    },
    show: {
      type: Boolean,
      default: false,
      required: true
    }
  }
}
</script>
<style>
/* 在一般情况下dialog宽为550像素，在逻辑分辨率480像素以下dialog宽为96% */
.site-config-item-dialog .el-dialog {
  width: 550px;
}
@media (max-width: 480px) {
  .site-config-item-dialog .el-dialog {
    width: 96%;
  }
}
</style>
<style scoped>
.form-input {
  width: 70%;
}
</style>
