<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="平台" prop="platform">
        <el-select v-model="form.platform" clearable size="small">
          <el-option
              v-for="dict in versionPlatformList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="安卓内部版本号" prop="versionCode">
        <el-input v-model="form.versionCode" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="旧版本号" prop="oldVersion">
        <el-input v-model="form.oldVersion" placeholder="请输入旧版本号" />
      </el-form-item>
      <el-form-item label="新版本号" prop="newVersion">
        <el-input v-model="form.newVersion" placeholder="请输入新版本号" />
      </el-form-item>
      <el-form-item label="包大小" prop="packageSize">
        <el-input v-model="form.packageSize" placeholder="请输入包大小" />
      </el-form-item>
      <el-form-item label="升级内容" prop="content">
        <el-input v-model="form.content" type="textarea" />
<!--        <editor v-model="form.content" :min-height="192"/>-->
      </el-form-item>
      <el-form-item label="下载地址" prop="downloadUrl">
        <el-input v-model="form.downloadUrl" placeholder="请输入下载地址" />
      </el-form-item>
      <el-form-item label="强制更新" prop="enforce">
        <el-select v-model="form.enforce" clearable size="small">
          <el-option
              v-for="dict in yesOrNoList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enableStatus">
        <el-select v-model="form.enableStatus" clearable size="small">
          <el-option
              v-for="dict in enableStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addVersion, getVersion, updateVersion } from '@/api/cai/version'
import { enableStatusList, versionPlatformList, yesOrNoList } from '@/constant/statusMap'

export default {
  components: {
  },
  data () {
    return {
      versionPlatformList,yesOrNoList,enableStatusList,
      open: false,
      title: '',
      form:{
        id: undefined,
        platform: undefined,
        versionCode: undefined,
        oldVersion: undefined,
        newVersion: undefined,
        packageSize: undefined,
        content: undefined,
        downloadUrl: undefined,
        enforce: 0,
        enableStatus: 1
      },
      // 表单校验
      rules: {
        platform: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        oldVersion: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        newVersion: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        packageSize: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        downloadUrl: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        enforce: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        enableStatus: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ]
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "版本管理";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getVersion(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateVersion(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addVersion(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
