// 添加/修改商户
<template>
  <el-dialog
    :title="isEdit ? '编辑区域' : '添加区域'"
    width="766px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="130px"
    >
      <el-form-item prop="id" style="margin-bottom: 0">&nbsp;</el-form-item>
      <el-form-item label="区域名称" prop="name" required>
        <el-input v-model="form.name" placeholder="请输入区域名称" />
      </el-form-item>
      <el-form-item label="绑定设备" prop="snJson" required>
        <el-select
          v-model="form.snJson"
          class="filter-item"
          placeholder="请选择设备进行绑定"
          clearable
          multiple
          filterable
        >
          <el-option
            v-for="(item, index) in machineList"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="行星云推送消息" prop="spiPushMsg">
        <el-input v-model="form.spiPushMsg" placeholder="请输入行星云推送消息" />
      </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 { addOrUpdate } from "@/api/area";

export default {
  props: {
    machineList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      form: {
        id: null,
        name: null,
        snJson: null,
        spiPushMsg: null,
      },
      rules: {
        name: [
          {
            required: true,
            whitespace: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        snJson: [
          {
            required: true,
            type: "array",
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
    };
  },
  methods: {
    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        if (obj && Object.keys(obj).length > 0) {
          this.form.id = obj.id;
          this.form.name = obj.name;
          this.form.spiPushMsg = obj.spiPushMsg;
          if (obj.snJson) {
            this.form.snJson = obj.snJson.split(",");
          } else {
            this.form.snJson = null;
          }
          this.isEdit = true;
        } else {
          this.isEdit = false;
        }
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        if (this.form.snJson && this.form.snJson.length > 0) {
          this.form.snJson = this.form.snJson.join(",");
        } else {
          this.form.snJson = null;
        }
        this.submitLoading = true;
        try {
          await addOrUpdate({
            ...this.form,
            adminId: this.$store.getters.adminId,
          });
          this.visible = false;
          this.$emit("success", this.isEdit);
        } catch (e) {
          this.$refs.form.resetFields();
          console.info("add", e);
        } finally {
          this.submitLoading = false;
        }
      });
    },
  },
};
</script>

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