// 添加/修改授权
<template>
  <el-dialog
    title="添加安装任务"
    width="800px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="100px"
    >
      <el-form-item label="设备sn" prop="sns" required>
        <el-select
          style="width: 100%"
          filterable
          clearable
          multiple
          v-model="form.sns"
          placeholder="请选择设备sn"
        >
          <el-option
            v-for="item in snList"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="安装包" prop="appApkId" required>
        <el-select
          style="width: 100%"
          clearable
          filterable
          v-model="form.appApkId"
          placeholder="请选择安装包"
        >
          <el-option
            v-for="item in installationPackageList"
            :key="item.id"
            :label="item.apkTitle"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { taskAdd } from "@/api/install_task";

export default {
  data() {
    return {
      form: {
        sns: [],
        appApkId: null,
      },
      rules: {
        sns: [
          {
            required: true,
            message: "不能为空!",
            trigger: "blur",
          },
        ],
        appApkId: [
          {
            required: true,
            message: "不能为空!",
            trigger: "blur",
          },
        ],
      },
      visible: false,
      submitLoading: false,
    };
  },
  props: {
    snList: {
      type: Array,
      default: function () {
        return [];
      },
    },
    installationPackageList: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  created() {},
  methods: {
    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        this.submitLoading = true;
        try {
          await taskAdd(this.form);
          this.visible = false;
          this.$emit("success", false);
        } catch (e) {
          console.info("add", e);
        } finally {
          this.submitLoading = false;
        }
      });
    },
  },
};
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
