<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="平台">
        <el-radio-group v-model="form.platform" prop="platform">
          <el-radio label="Android">Android</el-radio>
          <el-radio label="iOS">iOS</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="版本编码" prop="versionCode">
        <el-input v-model="form.versionCode"></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="下载地址">
        <el-input v-model="form.downloadUrl"></el-input>
      </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 type="textarea" :rows="3" placeholder="请输入内容" 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: '幼享乐',
        versionCode: '',
        versionNumber: '',
        downloadUrl: '',
        isShow: false,
        isForceUpgrade: false,
        platform: 'Android',
        upgradeDesc: '1. 优化产品体验，修复若干问题'
      },
      rules: {
        productName: [
          { required: true, message: '请输入产品名称', trigger: 'change' }
        ],
        versionCode: [
          { required: true, message: '请输入版本编码', trigger: 'change' }
        ],
        versionNumber: [
          { 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) {
          return false;
        }

        if (this.form.platform === 'Android' && !this.form.downloadUrl) {
          this.$error('Android端，下载地址为必填！~');
          return false;
        }

        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>
