<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="lineId">
        <j-list-select-tag
          v-model="model.lineId"
          dict-code="line"
        />
      </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' }"
        />
      </a-form-model-item>
      <a-form-model-item label="站场区间" prop="stationId">
        <j-list-select-tag
          v-model="model.stationId"
          dict-code="station"
        />
      </a-form-model-item>
      <a-form-model-item label="行别" prop="xingbie">
        <j-dict-select-tag
          v-model="model.xingbie"
          type="radioButton"
          dict-code="xingbie"
        />
      </a-form-model-item>
      <a-form-model-item label="附加类型" prop="fjlx">
        <j-dict-select-tag
          v-model="model.fjlx"
          dict-code="fjlx"
        />
      </a-form-model-item>
      <a-form-model-item label="起始杆号" prop="qsgh">
        <a-input v-model="model.qsgh" />
      </a-form-model-item>
      <a-form-model-item label="起始公里标" prop="qsglb">
        <a-input v-model="model.qsglb" />
      </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: {
        lineId: [{ required: true, message: '请选择' }],
        deptId: [{ required: true, message: '请选择' }],
        stationId: [{ required: true, message: '请选择' }],
        xingbie: [{ required: true, message: '请选择' }],
        fjlx: [{ required: true, message: '请选择' }],
        qsgh: [{ required: true, message: '请输入' }],
        qsglb: [{ required: true, message: '请输入' }]

      },
      url: {
        add: '/business/catenary/bus/fjxgmd/add',
        edit: '/business/catenary/bus/fjxgmd/update',
        tree: '/system/dept/treeSelect'
      }
    }
  },
  created() {
    this.loadTree()
  },
  methods: {
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    add() {
      this.model = {}
      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.id) {
        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>
