<template>
  <j-modal
    :title="title"
    :width="700"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <a-form-model ref="form" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="validatorRules" :model="model">
      <a-form-model-item label="所亭名称" prop="substationName">
        <a-input v-model="model.substationName" />
      </a-form-model-item>
      <a-form-model-item label="所属车间" prop="deptId">
        <a-tree-select
          v-model="model.deptId"
          :show-search="true"
          allow-clear
          tree-default-expand-all
          style="width: 100%"
          :tree-data="treeData"
          tree-node-filter-prop="label"
          :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
          @change="deptChange"
        />
      </a-form-model-item>
      <a-form-model-item label="线路" prop="lineId">
        <j-list-select-tag
          v-if="lineShow"
          v-model="model.lineId"
          :dept-id="model.deptId"
          dict-code="line"
        />
      </a-form-model-item>
      <a-form-model-item label="公里标" prop="marker">
        <a-input v-model="model.marker" style="width: 100%" />
      </a-form-model-item>
      <a-form-model-item label="经度" prop="longitude">
        <a-input v-model="model.longitude" style="width: 100%" />
      </a-form-model-item>
      <a-form-model-item label="纬度" prop="latitude">
        <a-input v-model="model.latitude" style="width: 100%" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { httpAction, getAction } from '@/api/request'
import JModal from '@/components/JModal'

export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      lineShow: false,
      treeData: [],
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        substationName: [{ required: true, message: '请输入' }],
        lineId: [{ required: true, message: '请选择' }],
        deptId: [{ required: true, message: '请选择' }],
        marker: [{ required: true, message: '请输入' }],
        longitude: [{ required: true, message: '请输入' }],
        latitude: [{ required: true, message: '请输入' }]

      },
      url: {
        add: '/business/catenary/bus/substation/add',
        edit: '/business/catenary/bus/substation/update',
        tree: '/system/dept/treeSelect'
      }
    }
  },
  watch: {
    'model.deptId': {
      immediate: true,
      handler: function(newV, oldV) {
        this.lineShow = false
        this.$nextTick(() => {
          this.lineShow = true
        })
      }
    }
  },
  created() {
    this.loadTree()
  },
  methods: {
    deptChange() {
      this.model.lineId = undefined
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    add() {
      this.model = { lineId: null }
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close(isSubmit) {
      if (isSubmit) {
        this.checkData()
      } else {
        this.visible = false
      }
    },
    checkData() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveData()
        } else {
          return false
        }
      })
    },
    saveData() {
      let url, type
      if (!this.model.substationId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      httpAction(url, this.model, type).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.$emit('ok')
          this.visible = false
        } else {
          console.log(res)
        }
      })
    }
  }
}
</script>
