<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="lineSjbh">-->
<!--        <a-input-number v-model="model.lineSjbh" style="width: 100%" />-->
<!--      </a-form-model-item>-->
      <a-form-model-item label="线路名称" prop="lineName">
        <a-input v-model="model.lineName" />
      </a-form-model-item>
      <a-form-model-item label="所属车间" prop="deptLines">
        <a-tree-select
          v-model="model.deptLines"
          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:'label', key:'id', value: 'id' }"
        />
      </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="lineYylc">-->
<!--        <a-input v-model="model.lineYylc" style="width: 100%" />-->
<!--      </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: 5 },
      wrapperCol: { span: 18 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        // lineSjbh: [{ required: true, message: '请输入' }],
        lineName: [{ required: true, message: '请输入' }],
        deptLines: [{ required: true, message: '请选择' }],
        // xingbie: [{ required: true, message: '请选择' }],
        // lineYylc: [{ required: true, message: '请输入' }],
        startMarker: [{ required: true, message: '请输入' }],
        endMarker: [{ required: true, message: '请输入' }]

      },
      url: {
        add: '/business/catenary/bus/line/add',
        edit: '/business/catenary/bus/line/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)
      if (this.model.deptLines) {
        this.model.deptLines = this.setDeptsToArr(this.model.deptLines)
      } else {
        this.model.deptLines = []
      }
      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.lineId) {
        url = this.url.add
        type = 'post'
      } else {
        url = this.url.edit
        type = 'put'
      }
      if (this.model.deptLines) {
        this.model.deptLines = this.setDeptsArrObj(this.model.deptLines)
      } else {
        this.model.deptLines = []
      }
      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>
