<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" size="small">
      <el-row>
        <form-col :span="span" label="区域名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入区域名称" />
        </form-col>
        <form-col :span="span" label="类型" prop="type">
          <el-radio-group v-model="form.type" placeholder="请选择类型">
            <el-radio
              v-for="dict in dict.type.area_sub_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </form-col>
        <form-col :span="span" label="还车误差" prop="error">
          <el-input v-model="form.error" placeholder="请输入还车误差">
            <template slot="append">米</template>
          </el-input>
        </form-col>
        <form-col :span="span" label="照片" prop="picture">
          <image-upload v-model="form.picture"/>
        </form-col>
        <form-col :span="span" label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </form-col>
        <form-col :span="span" label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.area_sub_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </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 { getAreaSub, addAreaSub, updateAreaSub } from "@/api/bst/areaSub";
import FormCol from "@/components/FormCol/index.vue";
import { AreaSubStatus, AreaSubType } from '@/utils/enums';

export default {
  name: 'AreaSubEditDialog',
  components: { FormCol },
  dicts: ['area_sub_type', 'area_sub_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: null
    },
    areaId: {
      type: [String, Number],
      required: true
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      span: 24,
      title: '',
      form: {},
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },
  methods: {
    handleOpen() {
      if (this.id == null) {
        this.reset();
        this.title = "添加子区域";
      } else {
        this.getDetail();
        this.title = "修改子区域";
      }
    },
    reset() {
      this.form = {
        id: null,
        name: null,
        type: AreaSubType.PARKING,
        areaId: this.areaId,
        boundary: null,
        boundaryStr: null,
        error: null,
        longitude: null,
        latitude: null,
        createTime: null,
        picture: null,
        remark: null,
        status: AreaSubStatus.NORMAL,
        ...this.initData
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
    },
    getDetail() {
      getAreaSub(this.id).then(response => {
        this.form = response.data;
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const promise = this.form.id != null ? updateAreaSub(this.form) : addAreaSub(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>