<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="500px"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="版本名" prop="productName">
        <el-input v-model="form.productName"></el-input>
      </el-form-item>

      <el-form-item label="版本号" prop="versionNumber">
        <el-input v-model="form.versionNumber"></el-input>
      </el-form-item>

      <el-form-item label="下载地址" prop="downloadUrl">
        <el-input v-model="form.downloadUrl"></el-input>
      </el-form-item>

      <el-form-item label="客户端">
        <el-radio-group v-model="form.platform" prop="platform">
          <el-radio label="Android">Android</el-radio>
          <!-- // todo 前端判断更改成大写IOS -->
          <el-radio label="iOS">iOS</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="强制更新">
        <el-radio-group v-model="form.isForceUpgrade" prop="isForceUpgrade">
          <el-radio :label="true">开启</el-radio>
          <el-radio :label="false">关闭</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.isShow" prop="isShow">
          <el-radio :label="true">运行</el-radio>
          <el-radio :label="false">停止</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="升级备注">
        <el-input v-model="form.upgradeDesc"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveItem } from '@/api/app';

export default {
  name: 'UpdateAppItemModal',

  props: {
    id: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      modal: true,

      form: {
        productName: '',
        versionNumber: '',
        downloadUrl: '',
        isShow: false,
        isForceUpgrade: false,
        platform: 'Android',
        upgradeDesc: null
      },
      rules: {
        productName: [
          { required: true, message: '请输入版本名', trigger: 'change' }
        ],
        versionNumber: [
          { required: true, message: '请输入版本号', trigger: 'change' }
        ],
        downloadUrl: [
          { required: true, message: '请输入下载地址', trigger: 'change' }
        ]
      }
    };
  },

  computed: {
    title() {
      if (this.id) {
        return '编辑App自更新';
      } else {
        return '新增App自更新';
      }
    }
  },

  mounted() {},

  methods: {
    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const params = Object.assign({}, this.form);
          params.cover = '';
          if (this.id) params.id = this.id;
          const { success, msg } = await saveItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('update_app_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
