<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="8vh"
    width="1040px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="类型" prop="sys_type">
                  <el-select
                    v-model="ruleForm.sys_type"
                    style="width: 100%"
                    :disabled="id == '007'"
                    placeholder="请选择类型"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select> </el-form-item></el-col>
              <el-col :span="12">
                <el-form-item label="版本号" prop="version">
                  <el-input
                    v-model="ruleForm.version"
                    :disabled="id == '007'"
                    placeholder="版本号"
                    style="width: 100%"
                    maxlength="50"
                  /> </el-form-item></el-col>
              <el-col :span="12">
                <el-form-item label="标题" prop="module">
                  <el-input
                    v-model="ruleForm.module"
                    :disabled="id == '007'"
                    placeholder="标题"
                    style="width: 100%"
                    maxlength="50"
                  /> </el-form-item></el-col>
              <el-col :span="12">
                <el-form-item label="时间" prop="addtime">
                  <el-date-picker
                    v-model="ruleForm.addtime"
                    type="datetime"
                    style="width: 100%"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    placeholder="选择日期时间"
                    :picker-options="pickerOptions"
                  /> </el-form-item></el-col>
              <el-col :span="24">
                <el-form-item label="内容" prop="system">
                  <vue-editor v-model="ruleForm.system" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="id !== '007'"
            type="primary"
            @click="submitForm"
          >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            id == "007" ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from '@/apis/service/system/updates'
import resToken from '@/mixins/resToken'

import { VueEditor } from 'vue2-editor'
export default {
  name: 'Terrace',
  components: {
    VueEditor
  },
  mixins: [resToken],
  props: ['showModel'],
  data() {
    return {
      loading: false,
      title: '添加版本信息',
      showModelThis: this.showModel,
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < new Date().valueOf() - 1000 * 60 * 60 * 24
        }
      },
      options: [
        { value: 'VER', label: '版本信息' },
        { value: 'MSG', label: '维护通知' }
      ],

      ruleForm: {
        sys_type: 'VER',
        version: '',
        module: '',
        system: '',
        addtime: ''
      },
      rulesThis: this.rules,
      rules: {
        sys_type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        version: [{ required: true, message: '版本号不能为空', trigger: 'blur' }],
        module: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        system: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        addtime: [{ required: true, message: '请选择时间', trigger: 'change' }]
      }
    }
  },
  watch: {
    showModel: function(val) {
      this.showModelThis = val
      if (val) {
        this.initForm()
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit('cancel')
      }
    }
  },
  methods: {
    async initForm() {
      this.loading = true
      this.rulesThis = this.rules
      await this.resetForm()
      this.loading = false
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields()
          this.$refs.ruleForm.clearValidate()
          this.ruleForm = {
            sys_type: 'VER',
            version: '',
            module: '',
            system: '',
            addtime: ''
          }
        }
      })
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.loading) {
            return
          }
          this.loading = true
          const model = JSON.parse(JSON.stringify(this.ruleForm))
          const { code } = await asyncRequest.add(model)

          if (code === 0) {
            this.$notify.success({
              title: '添加成功!',
              message: ''
            })
            this.showModelThis = false
            // 刷新
            this.$emit('refresh')
          } 
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.terrace {
}
</style>
