<template>
  <el-form :model="user" label-width="80px">
    <div v-for="item in user">
      <el-form-item :label="item.dictLabel" prop="dictLabel">
        <el-input v-model="item.dictLabel" disabled />
      </el-form-item>
      <el-form-item label="版本号" prop="dictValue">
        <el-input v-model="item.dictValue" placeholder="请输入版本号" clearable />
      </el-form-item>
      <el-form-item label="下载地址" prop="remark">
        <el-input v-model="item.remark" placeholder="请输入wgt下载地址" clearable />
      </el-form-item>
    </div>

    <el-form-item>
      <el-button type="primary" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import request from '@/utils/request';
const user = ref([
  {
    'dictValue': '',
    'dictLabel': 'ios',
    'remark': ''
  },
  {
    'dictValue': '',
    'dictLabel': 'android',
    'remark': ''
  }
]);

function getCfg() {
  request({
    url: '/app/auth/version',
    method: 'get'
  }).then((res) => {
    if (res && res.code === 200) {
      user.value.forEach((v) => {
        let f = res.data.find((item) => {
          return item.dictLabel === v.dictLabel;
        });
        if (f) {
          v.dictValue = f.dictValue;
          v.remark = f.remark;
        }
      });
    }
  });
}

function submit() {
  let flag = [];
  user.value.forEach((v) => {
    let keys = Object.keys(v);
    keys.forEach((k) => {
      if (!v[k]) {
        flag.push(false);
      }
    });
  });
  if (flag.length > 0) {
    ElMessageBox.alert('所有项都是必填');
    return;
  }
  request({
    url: '/api/xinghan/device/UpVersion',
    method: 'post',
    data: user.value
  }).then((res) => {
    if (res && res.code === 200) {
      ElMessageBox.alert('操作成功');
    }
  });
}

onMounted(() => {
  getCfg();
});
</script>
