<template>
  <div
    class="edit-add-detail-new-content"
    ref="targetSection"
    data-name="control"
  >
    <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"
        label-width="100px"
        label-position="right"
        label-suffix="："
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">交通管制</div>
          </div>
        </div>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="路段名称" prop="roadId">
              <el-select
                v-model="addEditForm.roadId"
                clearable
                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="16">
            <el-form-item label="桩号范围" prop="roadId">
              <span style="margin-right: 5px">{{ pileNumberRange }}</span>
              <el-radio-group v-model="addEditForm.eventPlace" size="small">
                <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"
            :type="addEditForm.eventPlace"
            :roadId="addEditForm.roadId"
          >
          </ScopeClassify>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="道路方向" prop="direction">
              <el-radio-group v-model="addEditForm.direction" size="small">
                <el-radio
                  v-for="(item, index) in getDictionaryByCode('xsfx')"
                  :label="item.value"
                  :key="index"
                  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
                type="textarea"
                :autosize="{minRows: 2, maxRows: 4}"
                v-model="addEditForm.placeRemarks"
                placeholder="请输入备注信息"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="16">
            <el-form-item label="管制措施">
              <el-radio-group
                v-model="addEditForm.measureType"
                size="small"
                @input="getOptions"
              >
                <el-radio
                  v-for="(item, index) in getDictionaryByCode('gzcs')"
                  :label="item.value"
                  :key="index"
                  border
                  >{{ item.name }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" v-if="gzcsxqOptions.length">
          <el-col :span="16">
            <el-form-item>
              <el-checkbox-group
                v-model="addEditForm.measureContent"
                @change="measureContentChange"
              >
                <el-checkbox
                  v-for="(item, index) in gzcsxqOptions"
                  :key="index"
                  :label="item.value"
                  >{{ item.name }}</el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="管制时间" prop="eventLevel">
              <el-date-picker
                style="width: 100%"
                v-model="addEditForm.startTime"
                type="datetime"
                placeholder="选择日期时间"
                default-time="12:00:00"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预计解除时间" prop="eventLevel">
              <el-date-picker
                style="width: 100%"
                v-model="addEditForm.endTime"
                type="datetime"
                placeholder="选择日期时间"
                default-time="12:00:00"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="" prop="eventLevel">
              <el-button type="primary" @click="handleSave">确认添加</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import PubSub from 'pubsub-js'
import {tableDetail} from '@/api/patrolAndRescue/commandDispatch/eventRegistration'
import {getDictionaryByCode, dictionaryEscape} from '@/utils/dictionary'
import ScopeClassify from '../ScopeClassify'

export default {
  name: 'TrafficControl',
  components: {ScopeClassify},
  props: {
    roadOptions: {
      default: [],
      type: Array,
    },
  },
  data() {
    return {
      addEditForm: {
        eventPlace: '1',
        measureType: '1',
        measureContent: [],
      },
      addEditRules: {
        eventTel: [
          {
            required: true,
            message: '报案电话不能为空',
            trigger: ['change', 'blur'],
          },
        ],
        roadId: [
          {
            required: true,
            message: '路段信息不能为空',
            trigger: ['change'],
          },
        ],
        teamId: [
          {
            required: true,
            message: '处置队伍不能为空',
            trigger: ['change'],
          },
        ],
        eventTime: [
          {
            required: true,
            message: '事件发生时间不能为空',
            trigger: ['change'],
          },
        ],
      },
      id: '', // id标识符
      trafficControls: [],
      // 管制措施详情
      gzcsxqOptions: [],
      pileNumberRange: '',
      formType: 'add',
      editIndex: 0,
    }
  },
  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
          )
          this.pileNumberRange = roadInfo.startPeg + '~' + roadInfo.endPeg
        }
      },
      deep: true,
    },
  },
  created() {
    this.getOptions('1')
  },
  mounted() {},
  methods: {
    dictionaryEscape,
    getDictionaryByCode,

    roadChange(e) {
      let roadInfo = this.roadOptions.find((item) => item.id === e)
      this.pileNumberRange = roadInfo.startPeg + '~' + roadInfo.endPeg
    },

    // 获取详情
    async getDetail(id) {
      try {
        this.addEditForm = await tableDetail(id)
        const {roadId = '', pegSVal = '', pegEVal = ''} = this.addEditForm
        if (pegSVal) {
          let startPegArr = pegSVal.split('+')
          this.addEditForm.startPeg = startPegArr[0].slice(1)
          this.addEditForm.startPegAdd = startPegArr[1]
        }
        if (pegEVal) {
          let endPegArr = pegEVal.split('+')
          this.addEditForm.endPeg = endPegArr[0].slice(1)
          this.addEditForm.endPegAdd = endPegArr[1]
        }
      } finally {
        this.formLoading = false
      }
    },

    // 获取管制措施详情
    getOptions(val) {
      let data = this.getDictionaryByCode('gzcs')
      data.forEach((item) => {
        if (item.value == val) {
          this.gzcsxqOptions = item.lowerDic ? item.lowerDic.gzcsxq : []
        }
      })
    },

    measureContentChange(e) {
      if (e.length > 1) {
        this.addEditForm.measureContent.shift()
      }
    },
    // 保存
    handleSave() {
      this.$confirm('确认新增交通管制信息数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.$refs.addEditForm.validate(async (valid, object) => {
          if (!valid) {
            this.$message.warning(
              `有${
                Object.keys(object).length
              }个字段未通过校验，请正确填写表单内容。`
            )
          } else {
            this.saveLoading = true
            let data = JSON.parse(JSON.stringify(this.addEditForm))
            data.startTime = moment(data.startTime).format(
              'YYYY-MM-DD HH:mm:ss'
            )
            data.endTime = moment(data.endTime).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
            data.measureContent = data.measureContent[0] || ''
            let roadData = this.$refs.scopeClassify.sendData()
            data = {...data, ...roadData}
            if (this.formType === 'edit') {
              this.trafficControls[this.editIndex] = data
            } else {
              this.trafficControls.push(data)
            }
            this.addEditForm.eventPlace = '1'
            this.$emit('sendTrafficData', this.trafficControls)
          }
        })
      })
    },

    handleEdit(row, index) {
      this.formType = 'edit'
      this.editIndex = index
      this.addEditForm = row
      this.$refs.scopeClassify.getDetail(row)
    },
    handleDelete(row, index) {
      this.$confirm('确认删除这条数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.trafficControls.splice(index, 1)
      })
    },
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  },
}
</script>

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

.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: 100%;
            }
        }
    }
}

.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>
