<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="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"
          @selectChange="lineChange"
        />
      </a-form-model-item>
      <a-form-model-item label="站场区间" prop="stationId">
        <j-list-select-tag
          v-if="stationShow"
          v-model="model.stationId"
          :line-id="model.lineId"
          :dept-id="model.deptId"
          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="pillarCode">
        <a-input v-model="model.pillarCode" />
      </a-form-model-item>
      <a-form-model-item label="开始公里标" prop="startMarker">
        <a-input-number style="width: 100%" v-model="model.startMarker" />
      </a-form-model-item>
      <a-form-model-item label="结束公里标" prop="endMarker">
        <a-input-number style="width: 100%" v-model="model.endMarker" />
      </a-form-model-item>
      <a-form-model-item label="数量（课）" prop="count">
        <a-input v-model="model.count" />
      </a-form-model-item>
      <a-form-model-item label="距导线水平距离（米）" prop="distanceHorizontal">
        <a-input v-model="model.distanceHorizontal" />
      </a-form-model-item>
      <a-form-model-item label="距导线垂直距离（米）" prop="distanceVertical">
        <a-input v-model="model.distanceVertical" />
      </a-form-model-item>
      <a-form-model-item label="树种" prop="kind">
        <a-input v-model="model.kind" />
      </a-form-model-item>
      <a-form-model-item label="严重程度" prop="severity">
        <a-input v-model="model.severity" />
      </a-form-model-item>
      <a-form-model-item label=" 产权（林场、个人）" prop="propertyRights">
        <a-input v-model="model.propertyRights" />
      </a-form-model-item>
      <a-form-model-item label="调查人" prop="inquirer">
        <a-input v-model="model.inquirer" />
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-textarea :maxLength="300" :rows="4"  v-model="model.remark" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import { getAction, httpAction } from '@/api/request'
import JModal from '@/components/JModal'
export default {
  name: 'CheckAndEditModel',
  components: {
    JModal
  },
  data() {
    return {
      stationShow: false,
      lineShow: false,
      treeData: [],
      labelCol: { span: 7 },
      wrapperCol: { span: 16 },
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      validatorRules: {
        deptId: [{ required: true, message: '请选择' }],
        lineId: [{ required: true, message: '请选择' }],
        stationId: [{ required: true, message: '请选择' }],
        xingbie: [{ required: true, message: '请选择' }],
        pillarCode: [{ required: true, message: '请输入' }],
        startMarker: [{ required: true, message: '请输入' }],
        endMarker: [{ required: true, message: '请输入' }],
        count: [{ required: true, message: '请输入' }],
        displayNum: [{ required: true, message: '请输入' }],
        distanceHorizontal: [{ required: true, message: '请输入' }],
        distanceVertical: [{ required: true, message: '请输入' }],
        kind: [{ required: true, message: '请输入' }],
        severity: [{ required: true, message: '请输入' }],
        propertyRights: [{ required: true, message: '请输入' }],
        inquirer: [{ required: true, message: '请输入' }],
        remark: [{ required: true, message: '请输入' }]

      },
      url: {
        add: '/business/baseinfo/base/prevent/tree/add',
        edit: '/business/baseinfo/base/prevent/tree/update',
        tree: '/system/dept/treeSelect'
      }
    }
  },
  created() {
    this.loadTree()
  },
  watch: {
    'model.deptId': {
      immediate: true,
      handler: function(newV, oldV) {
        this.lineShow = false
        this.$nextTick(() => {
          this.lineShow = true
        })
      }
    },
    'model.lineId': {
      immediate: true,
      handler: function(newV, oldV) {
        this.stationShow = false
        this.$nextTick(() => {
          this.stationShow = true
        })
      }
    }
  },
  methods: {
    lineChange() {
      this.model.stationId = undefined
    },
    deptChange() {
      this.model.lineId = undefined
      this.model.stationId = undefined
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    add() {
      this.model = { lineId: null, stationId: 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.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>
