<template>
  <div
    ref="targetSection"
    class="edit-add-detail-new-content"
    data-name="basics"
    style="height: auto"
  >
    <div
      v-loading="false"
      class="form-content"
      element-loading-background="rgba(10 ,38 ,61, 0.95)"
      element-loading-text="数据加载中..."
    >
      <el-form
        ref="addEditForm"
        :model="addEditForm"
        :rules="addEditRules"
        label-position="right"
        label-suffix="："
        label-width="150px"
      >
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="事件标题" prop="eventName">
              <el-input
                v-model="addEditForm.eventName"
                clearable
                placeholder="请输入事件标题"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="事件来源">
              <el-select
                v-model="addEditForm.eventSrc"
                clearable
                placeholder="请选择事件来源"
              >
                <el-option
                  v-for="(item, index) in getDictionaryByCode('sjly')"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="确认情况" prop="eventSituation">
              <el-select
                v-model="addEditForm.eventSituation"
                clearable
                placeholder="请选择确认情况"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in getDictionaryByCode('qrqk')"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发生时间" prop="eventTime">
              <el-date-picker
                v-model="addEditForm.eventTime"
                default-time="12:00:00"
                placeholder="选择事件发生时间"
                style="width: 100%"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="事件类型" prop="lwEventType">
              <el-cascader
                ref="cascader"
                v-model="eventTypeValue"
                :options="sjlxOptions"
                :props="{checkStrictly: true, label: 'name'}"
                :show-all-levels="false"
                clearable
                filterable
                popper-class="twoStage"
                style="width: 100%"
                @change="eventTypeChange"
              ></el-cascader>
              <!-- <el-select v-model="addEditForm.eventType" style="width: 100%" placeholder="事件类型" clearable>
                                <el-option v-for="(item, index) in getDictionaryByCode(
                                    'sjlx'
                                )" :key="index" :label="item.name" :value="item.value" />
                            </el-select> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接警时间" prop="receiveTime">
              <el-date-picker
                v-model="addEditForm.receiveTime"
                default-time="12:00:00"
                placeholder="请选择接警时间"
                style="width: 100%"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="确认时间" prop="confirmTime">
              <el-date-picker
                v-model="addEditForm.confirmTime"
                default-time="12:00:00"
                placeholder="请选择确认时间"
                style="width: 100%"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报案电话" prop="eventTel">
              <el-input
                v-model="addEditForm.eventTel"
                clearable
                placeholder="报案电话"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="路段名称" prop="roadId">
              <el-select
                v-model="addEditForm.roadId"
                clearable
                disabled
                filterable
                placeholder="请选择所属路段"
                style="width: 100%"
                @change="roadChange"
              >
                <el-option
                  v-for="item in roadOptions"
                  :key="item.id"
                  :label="item.roadName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="桩号范围">
              <span style="margin-right: 16px;font-size: 14px;">{{
                pileNumberRange
              }}</span>
              <el-radio-group
                v-model="addEditForm.eventPlace"
                size="small"
                @input="eventPlaceChange"
              >
                <el-radio
                  v-for="(item, index) in getDictionaryByCode('sjfsqy')"
                  :key="index"
                  :label="item.value"
                  border
                  >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <ScopeClassify
            ref="scopeClassify"
            :pileNumberRange="pileNumberRange"
            :roadId="addEditForm.roadId"
            :type="addEditForm.eventPlace"
          >
          </ScopeClassify>
        </el-row>
        <el-row v-if="whereTheRoadGoes.length > 0" :gutter="24">
          <el-col :span="24">
            <el-form-item label="道路方向" prop="direction">
              <el-radio-group v-model="addEditForm.direction" size="small">
                <el-radio
                  v-for="(item, index) in whereTheRoadGoes"
                  :key="index"
                  :label="item.value"
                  border
                  >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <!--        <el-row :gutter="24">-->
        <!--          <el-col :span="24">-->
        <!--            <el-form-item label="道路方向" prop="direction">-->
        <!--              <el-radio-group v-model="addEditForm.direction" size="small">-->
        <!--                <el-radio-->
        <!--                  v-for="(item, index) in getDictionaryByCode('xsfx')"-->
        <!--                  :key="index"-->
        <!--                  :label="item.value"-->
        <!--                  border-->
        <!--                >{{ item.name }}-->
        <!--                </el-radio-->
        <!--                >-->
        <!--              </el-radio-group>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="备注信息" prop="placeRemarks">
              <el-input
                v-model="addEditForm.placeRemarks"
                :autosize="{minRows: 2, maxRows: 4}"
                placeholder="请输入备注信息"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="事件等级" prop="eventLevel">
              <el-radio-group v-model="addEditForm.eventLevel" size="small">
                <el-radio
                  v-for="(item, index) in getDictionaryByCode('sjdj')"
                  :key="index"
                  :label="item.value"
                  border
                  >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="预计恢复时间">
              <el-date-picker
                v-model="addEditForm.eventTime"
                default-time="12:00:00"
                placeholder="选择预计恢复时间"
                style="width: 100%"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="拥堵里程" prop="blockLength">
              <el-input
                v-model="addEditForm.blockLength"
                class="number-input"
                placeholder="请输入内容"
                type="number"
              >
                <template slot="append">KM</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="滞留人数" prop="holdupUserNum">
              <el-input
                v-model="addEditForm.holdupUserNum"
                clearable
                placeholder="滞留人员数量"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="滞留车辆" prop="holdupCarNum">
              <el-input
                v-model="addEditForm.holdupCarNum"
                clearable
                placeholder="滞留车辆数量"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="受伤人数" prop="hurtUserNum">
              <el-input
                v-model="addEditForm.hurtUserNum"
                clearable
                placeholder="受伤人员数量"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="死亡人数" prop="deadUserNum">
              <el-input
                v-model="addEditForm.deadUserNum"
                clearable
                placeholder="死亡人数"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="损坏车辆" prop="brokenCarNum">
              <el-input
                v-model="addEditForm.brokenCarNum"
                clearable
                placeholder="损坏车辆数量"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="肇事方/故障车方" prop="cause">
              <el-input
                v-model="addEditForm.cause"
                clearable
                placeholder="肇事方/故障车方"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="肇事车辆/故障车辆" prop="causeCar">
              <el-input
                v-model="addEditForm.causeCar"
                clearable
                placeholder="肇事车辆/故障车辆"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路产损失" prop="loss">
              <el-input
                v-model="addEditForm.loss"
                clearable
                disabled
                placeholder="路产损失"
                type="number"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="事件描述" prop="memo">
              <el-input
                v-model="addEditForm.memo"
                :autosize="{minRows: 3, maxRows: 6}"
                placeholder="请输入事件描述"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="处理措施" prop="loss">
              <el-input
                v-model="addEditForm.disposition"
                :autosize="{minRows: 3, maxRows: 6}"
                placeholder="请输入处理措施"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import {getDictionaryByCode} from '@/utils/dictionary'
import ScopeClassify from '../ScopeClassify'

export default {
  name: 'EventRegistration',
  components: {ScopeClassify},
  props: {
    roadOptions: {
      default: () => [],
      type: Array
    }
  },
  data() {
    return {
      addEditForm: {
        roadId: '',
        eventPlace: '1',
        eventTime: new Date()
      },
      addEditRules: {
        memo: [
          {
            required: true,
            message: '事件描述不能为空',
            trigger: ['change', 'blur']
          }
        ],
        eventName: [
          {
            required: true,
            message: '事件标题不能为空',
            trigger: ['change', 'blur']
          }
        ],
        eventSituation: [
          {
            required: true,
            message: '确认情况不能为空',
            trigger: ['change']
          }
        ],
        lwEventType: [
          {
            required: true,
            message: '事件类型不能为空',
            trigger: ['change']
          }
        ],
        eventTime: [
          {
            required: true,
            message: '事件发生时间不能为空',
            trigger: ['change']
          }
        ],
        receiveTime: [
          {
            required: true,
            message: '接警时间不能为空',
            trigger: ['change']
          }
        ],
        confirmTime: [
          {
            required: true,
            message: '确认时间不能为空',
            trigger: ['change']
          }
        ],
        // eventTel: [
        //   {
        //     required: true,
        //     message: '报案电话不能为空',
        //     trigger: ['change']
        //   }
        // ],
        roadId: [
          {
            required: true,
            message: '报案电话不能为空',
            trigger: ['change']
          }
        ],
        direction: [
          {
            required: true,
            message: '道路方向不能为空',
            trigger: ['change']
          }
        ],
        placeRemarks: [
          {
            required: true,
            message: '备注信息不能为空',
            trigger: ['change']
          }
        ],
        eventLevel: [
          {
            required: true,
            message: '事件等级不能为空',
            trigger: ['change']
          }
        ]
      },
      id: '',
      // 桩号范围
      pileNumberRange: '',
      sjlxOptions: [],
      eventTypeValue: [],
      whereTheRoadGoes: []
    }
  },
  watch: {
    roadOptions: {
      handler(newVal, oldVal) {
        if (newVal.length) {
          this.$set(this.addEditForm, 'roadId', this.roadOptions[0].id)
          let roadInfo = this.roadOptions.find(
            item => item.id === this.roadOptions[0].id
          )
          if (roadInfo.startPegK && roadInfo.endPegK) {
            this.pileNumberRange = roadInfo.startPegK + '~' + roadInfo.endPegK
          } else {
            this.pileNumberRange = ''
          }
        }
      },
      deep: true
    }
  },
  created() {
    this.eventPlaceChange('1')
    this.sjlxOptions = this.getDictionaryByCode('lx_sjlx')
    this.sjlxOptions.forEach(item => {
      if (item.lowerDic.lx_sjlxxq) {
        item.children = item.lowerDic.lx_sjlxxq
        item.children.forEach(elm => (elm.parentValue = item.value))
      }
    })
  },
  mounted() {},
  methods: {
    getDictionaryByCode,
    eventPlaceChange(val) {
      let data = this.getDictionaryByCode('sjfsqy')
      data.forEach(item => {
        if (item.value == val) {
          this.whereTheRoadGoes = item.lowerDic ? item.lowerDic['jtgz-fx'] : []
        }
      })
    },
    eventTypeChange() {
      let nodeItem = this.$refs.cascader.getCheckedNodes()[0]
      this.$set(this.addEditForm, 'lwEventType', nodeItem ? nodeItem.value : '')
    },
    // 路段变化
    roadChange(e) {
      let roadInfo = this.roadOptions.find(item => item.id === e)
      if (roadInfo.startPegK && roadInfo.endPegK) {
        this.pileNumberRange = roadInfo.startPegK + '~' + roadInfo.endPegK
      } else {
        this.pileNumberRange = ''
      }
      this.$emit('roadChange', e)
    },
    // 获取详情
    getDetail(data) {
      data.eventLevel = data.eventLevel + ''
      this.eventTypeValue = this.getParentsById(
        this.sjlxOptions,
        data.lwEventType
      )
      this.addEditForm = data
      let roadInfo = this.roadOptions.find(item => item.id === data.roadId)
      if (roadInfo.startPegK && roadInfo.endPegK) {
        this.pileNumberRange = roadInfo.startPegK + '~' + roadInfo.endPegK
      } else {
        this.pileNumberRange = ''
      }
      this.$refs.scopeClassify.getDetail(data)
    },

    getParentsById(list, value) {
      let eventTypeArr = []
      list.forEach(item => {
        eventTypeArr = [...eventTypeArr, ...item.children]
      })
      let data = eventTypeArr.find(item => item.value == value)
      return [data.parentValue, value]
    },

    // 保存
    sendBaseData() {
      let baseData = {}
      this.$refs.addEditForm.validate(async (valid, object) => {
        if (!valid) {
          baseData = {fieldNum: Object.keys(object).length}
        } else {
          let data = JSON.parse(JSON.stringify(this.addEditForm))
          data.eventTime = moment(data.eventTime).format('YYYY-MM-DD HH:mm:ss')
          data.receiveTime = moment(data.receiveTime).format(
            'YYYY-MM-DD HH:mm:ss'
          )
          data.confirmTime = moment(data.confirmTime).format(
            'YYYY-MM-DD HH:mm:ss'
          )
          data.recoverTime = moment(data.recoverTime).format(
            'YYYY-MM-DD HH:mm:ss'
          )
          let roadInfo = this.roadOptions.find(item => item.id === data.roadId)
          data.roadName = roadInfo.name
          data.roadCode = roadInfo.code
          let roadData = this.$refs.scopeClassify.sendData()
          if (data.eventPlace > '1') {
            data.placeDetail = JSON.stringify(roadData)
            data.lat = roadData[0]?.lat
            data.lng = roadData[0]?.lng
            baseData = data
          } else {
            baseData = {...data, ...roadData, placeDetail: '[]'}
          }
        }
      })
      return baseData
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/assets/css/element-variables';

::v-deep .edit-add-detail-new-content {
  height: auto !important;
}

.name-check-box {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;

  .name-check {
    display: flex;
    width: fit-content;
  }
}

::v-deep .el-tabs {
  overflow: hidden;

  .el-tabs__nav {
    transform: translateX(20px) !important;

    .el-tabs__item {
      letter-spacing: 1px;
      font-size: 15px;
      font-weight: 600;
    }
  }

  height: calc(100% - 93px);
  background: #114066;
  border-radius: 5px;

  .el-tabs__content {
    height: 92%;

    div#pane-first {
      height: 100%;

      .edit-add-detail-new-content {
        // overflow: hidden;
        height: auto !important;
      }
    }
  }
}

.el-icon-plus {
  cursor: pointer;
  display: block;
  height: 100%;
  font-size: 16px !important;
  font-weight: 900;
  color: #43a1ff;
}

.number-input {
  ::v-deep .el-input__inner {
    font-size: 14px;
  }
}

.edit-add-detail-new-content {
  width: 100%;
  display: flex;
  height: 0;
  flex: 1;
  column-gap: 20px;

  .form-content {
    flex: 3;
  }

  .map-content {
    flex: 4;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: $--system-border-color3 0.5px 0.5px 5px 1px;

    ::v-deep.marker-tip {
      position: relative;
      background-image: url('~@/assets/img/patrolAndRescue/inspection-points-icon.png');
      background-size: cover;
      width: 48px !important;
      height: 48px !important;
      z-index: 999;

      &::after {
        position: absolute;
        content: '救援点位置';
        left: 100%;
        top: 8px;
        width: 80px;
        height: 25px;
        font-size: 16px;
        color: $--color-warning;
        text-align: center;
        line-height: 25px;
        padding: 5px 10px;
        background-color: rgba(darken($--color-warning, 70%), 0.9);
        border-radius: 3px;
        box-shadow: 0 0 6px rgba($--color-warning, 0.8);
        border: none;
        z-index: 999;
      }
    }
  }

  .stake-mark {
    ::v-deep .el-form-item__content {
      display: flex;
      justify-content: space-between;

      .pegVal-text2,
      .pegVal-text1 {
        padding-right: 10px;
        font-size: 16px;
        width: fit-content;
      }

      .pegVal-text2 {
        padding-left: 10px;
        font-size: 24px;
      }
    }
  }
}
</style>
