<template>
  <el-dialog
    :title="!dataForm.carId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车型名称" prop="carName">
      <el-input v-model="dataForm.carName" placeholder="车型名称"></el-input>
    </el-form-item>
    <el-form-item label="导航类型" prop="navigationType">
      <el-select v-model="dataForm.navigationType" placeholder="导航类型">
        <el-option
          v-for="item in navigationTypeOpt"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input v-model="dataForm.remarks" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="是否启用" prop="isOn">
      <el-switch
        v-model="dataForm.isOn"
        active-color="#409EFF"
        inactive-color="#F56C6C"
        :active-value="1"
        :inactive-value="0"
      />
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        navigationTypeOpt: [],
        dataForm: {
          carId: 0,
          carName: '',
          navigationType: '',
          remarks: '',
          isOn: 0
        },
        dataRule: {
          carName: [
            { required: true, message: '车型名称不能为空', trigger: 'blur' }
          ],
          navigationType: [
            { required: true, message: '导航类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getDictDetail()
    },
    methods: {
      getDictDetail () {
        this.$http({
          url: this.$http.adornUrl('/api/dict/dictDetail?code=navigation_type&page=0&size=9999'),
          method: 'get'
        }).then(({data}) => {
          this.navigationTypeOpt = [...data.content]
        })
      },
      init (id) {
        this.dataForm.carId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.carId) {
            this.$http({
              url: this.$http.adornUrl(`/car/car/info/${this.dataForm.carId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.carName = data.car.carName
                this.dataForm.navigationType = data.car.navigationType
                this.dataForm.remarks = data.car.remarks
                this.dataForm.isOn = data.car.isOn
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/car/car/${!this.dataForm.carId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'carId': this.dataForm.carId || undefined,
                'carName': this.dataForm.carName,
                'navigationType': this.dataForm.navigationType,
                'remarks': this.dataForm.remarks,
                'isOn': this.dataForm.isOn
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
