<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="stationName">
        <a-input v-model="model.stationName" />
      </a-form-model-item>
      <a-form-model-item label="线路" prop="lineId">
        <j-list-select-tag
          v-model="model.lineId"
          dict-code="line"
          @change="deptChange"
        />
      </a-form-model-item>
      <a-form-model-item label="所属车间" prop="deptStations">
        <a-tree-select
          v-model="model.deptStations"
          tree-checkable
          :max-tag-count="5"
          :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:'deptName', key:'deptId', value: 'deptId' }"
        />
      </a-form-model-item>
      <a-form-model-item label="区间/站场" prop="stationType">
        <j-dict-select-tag
          v-model="model.stationType"
          type="radioButton"
          dict-code="station_type"
        />
      </a-form-model-item>
      <a-form-model-item label="起始公里标" prop="startMarker">
        <a-input v-model="model.startMarker" style="width: 100%" />
      </a-form-model-item>
      <a-form-model-item label=" 结束公里标" prop="endMarker">
        <a-input v-model="model.endMarker" 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 {
      treeData: [],
      labelCol: { span: 4 },
      wrapperCol: { span: 19 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        stationName: [{ required: true, message: '请输入' }],
        lineId: [{ required: true, message: '请选择' }],
        deptStations: [{ required: true, message: '请选择' }],
        stationId: [{ required: true, message: '请选择' }],
        startMarker: [{ required: true, message: '请输入' }],
        endMarker: [{ required: true, message: '请输入' }]

      },
      url: {
        add: '/business/catenary/bus/station/add',
        edit: '/business/catenary/bus/station/update',
        tree: '/business/catenary/bus/line/getDeptByLineId/'
      }
    }
  },
  watch: {
    'model.lineId': {
      handler: function(newV, oldV) {
        if (this.model.lineId) {
          this.loadTree(this.model.lineId)
        }
      }
    }
  },
  created() {
  },
  methods: {
    deptChange() {
      this.model.deptStations = []
    },
    loadTree(lineId) {
      this.treeData = []
      getAction(this.url.tree + lineId).then((res) => {
        if (res.code === 200) {
          this.treeData = res.rows
        }
      })
    },
    add() {
      this.model = { deptStations: [] }
      this.visible = true
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (this.model.deptStations) {
        this.model.deptStations = this.setDeptsToArr(this.model.deptStations)
      } else {
        this.model.deptStations = []
      }
      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.stationId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      if (this.model.deptStations) {
        this.model.deptStations = this.setDeptsArrObj(this.model.deptStations)
      } else {
        this.model.deptStations = []
      }
      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)
        }
      })
    },
    setDeptsArrObj(depts) {
      const obj = []
      for (const item of depts) {
        obj.push({ deptId: item })
      }
      return obj
    },
    setDeptsToArr(depts) {
      const Arr = []
      for (const item of depts) {
        Arr.push(item.deptId)
      }
      return Arr
    }
  }
}
</script>
