<template>
  <el-dialog
    class="sign-download-data"
    :visible.sync="modalIsShow"
    title="标记用途"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="used" label="是否使用：">
        <el-radio-group v-model="modalForm.used">
          <el-radio
            v-for="(val, key) in BOOLEAN_TYPE"
            :key="key"
            :label="key * 1"
            >{{ val }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="useScene" label="下载用途：">
        <el-select
          v-model="modalForm.useScene"
          placeholder="下载用途"
          clearable
          multiple
          class="width-400"
        >
          <el-option
            v-for="item in useScenes"
            :key="item.name"
            :value="item.name"
            :label="item.desc"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确认</el-button
      >
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { BOOLEAN_TYPE } from "../../../constants/enumerate";
import { signPurpose } from "../api";

const initModalForm = {
  used: 1,
  useScene: [],
};

export default {
  name: "sign-download-data",
  props: {
    ids: {
      type: Array,
      default() {
        return [];
      },
    },
    useScenes: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      BOOLEAN_TYPE,
      modalForm: {},
      rules: {
        used: [
          {
            required: true,
            message: "请选择是否使用",
            trigger: "change",
          },
        ],
        useScene: [
          {
            required: true,
            message: "请选择下载用途",
            validator: (rule, value, callback) => {
              if (!value || !value.length) {
                return callback("请选择下载用途");
              }
              return callback();
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { ...initModalForm };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;

      const datas = { ...this.modalForm, id: this.ids.join() };
      datas.used = !!datas.used;
      datas.useScene = datas.useScene.join();
      const data = await signPurpose(datas).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success("操作成功！");
      this.cancel();
      this.$emit("modified");
    },
  },
};
</script>
