<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    :close-on-click-modal="false"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <form-col :span="span" label="MAC" prop="mac">
          <el-input v-model="form.mac" placeholder="请输入设备Mac号" :disabled="form.id != null"/>
        </form-col>
        <form-col :span="span" label="SN" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入设备SN号" :disabled="form.id != null" />
        </form-col>
        <form-col :span="span" label="车牌号" prop="vehicleNum">
          <el-input v-model="form.vehicleNum" placeholder="请输入车牌号" />
        </form-col>
        <form-col :span="span" label="车型" prop="modelId">
          <model-remote-select v-model="form.modelId" style="width: 100%;" v-if="!loading" :init-options="initModelOptions" />
        </form-col>
        <form-col :span="span" label="所属用户" prop="mchId">
          <user-input v-model="form.mchId" :text.sync="form.mchName" :disabled="!checkPermi(['system:user:list'])"/>
        </form-col>
        <form-col :span="span" label="运营区" prop="areaId">
          <area-remote-select v-model="form.areaId" style="width: 100%;" :init-options="initAreaOptions" v-if="!loading"/>
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDevice, addDevice, updateDevice } from "@/api/bst/device";
import FormCol from "@/components/FormCol/index.vue";
import UserInput from '@/components/Business/User/UserInput.vue';
import AreaRemoteSelect from '@/components/Business/Area/AreaRemoteSelect.vue';
import {RoleKeys} from '@/utils/enums';
import { mapGetters } from 'vuex';
import ModelRemoteSelect from '@/components/Business/Model/ModelRemoteSelect.vue';

export default {
  name: 'DeviceEditDialog',
  components: { FormCol, UserInput, AreaRemoteSelect, ModelRemoteSelect },
  dicts: ['device_status', 'device_lock_status', 'device_iot_status', 'device_online_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      RoleKeys,
      loading: false,
      span: 24,
      title: '',
      form: {},
      rules: {
        mac: [
          { required: true, message: "设备Mac号不能为空", trigger: "blur" }
        ],
        sn: [
          { required: true, message: "设备SN号不能为空", trigger: "blur" }
        ],
      }
    }
  },
  computed: {
    ...mapGetters(['userId', 'nickName']),
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    },
    initAreaOptions() {
      return [{id: this.form.areaId, name: this.form.areaName}]
    },
    initModelOptions() {
      return [{id: this.form.modelId, name: this.form.modelName}]
    }
  },
  methods: {
    handleOpen() {
      if (this.id == null) {
        this.reset();
        this.title = "添加设备";
      } else {
        this.getDetail();
        this.title = "修改设备";
      }
    },
    reset() {
      this.form = {
        id: null,
        modelId: null,
        picture: null,
        deviceName: null,
        mac: null,
        sn: null,
        vehicleNum: null,
        areaId: null,
        activationTime: null,
        onlineStatus: null,
        createTime: null,
        updateTime: null,
        remark: null,
        status: null,
        lockStatus: null,
        location: null,
        remainingPower: null,
        voltage: null,
        longitude: null,
        latitude: null,
        lastTime: null,
        version: null,
        signalStrength: null,
        quality: null,
        satellites: null,
        gps: null,
        lastLocationTime: null,
        hardwareVersionId: null,
        mchId: null,
        iotStatus: null,
        isSound: null,
        ...this.initData
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
    },
    getDetail() {
      this.loading = true;
      getDevice(this.id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const promise = this.form.id != null ? updateDevice(this.form) : addDevice(this.form);
          promise.then(response => {
            this.$modal.msgSuccess(this.form.id != null ? "修改成功" : "新增成功");
            this.dialogVisible = false;
            this.$emit('success');
          });
        }
      });
    },
    cancel() {
      this.dialogVisible = false;
      this.reset();
    }
  }
}
</script>

<style lang="scss" scoped>

</style> 