<template>
  <div class="edit-add-detail disposition-page">
    <div class="edit-add-detail-new-content">
      <div class="form-content">
        <el-form
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-position="top"
          label-suffix="："
          label-width="100px"
        >
          <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" style="flex-wrap: wrap" type="flex">
            <el-col :span="6">
              <el-form-item label="内容" prop="content">
                <el-input v-model="addEditForm.content" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="类型" prop="patrolCategory">
                <el-input
                  v-model="addEditForm.patrolCategory"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="位置" prop="positions">
                <el-input
                  v-model="addEditForm.positions"
                  disabled
                  style="width: 100%"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="路段" prop="roadCode">
                <el-input v-model="addEditForm.roadCode" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="方向" prop="patrolDirection">
                <el-input
                  v-model="addEditForm.patrolDirection"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="桩号" prop="pegVal">
                <el-input v-model="addEditForm.pegVal" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="登记人员" prop="inspecetUserName">
                <el-input v-model="addEditForm.inspecetUserName" disabled>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="登记时间" prop="inspecetTime">
                <el-input v-model="addEditForm.inspecetTime" disabled>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <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" style="flex-wrap: wrap" type="flex">
            <el-col :span="6">
              <el-form-item label="处理结果" prop="handleStatus">
                <el-select
                  v-model="addEditForm.handleStatus"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="(item, i) in dicts"
                    :key="i"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态" prop="inspecetStatus">
                <el-select
                  v-model="addEditForm.inspecetStatus"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="(item, i) in xcdwztDicts"
                    :key="i"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6">
              <el-form-item label="异常处理措施" prop="handleType">
                <el-select
                  v-model="addEditForm.handleType"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="(item, i) in warnDicts"
                    :key="i"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="巡查情况" prop="inspecetInfo">
                <el-input
                  v-model="addEditForm.inspecetInfo"
                  :rows="3"
                  placeholder="请输入巡查情况"
                  type="textarea"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="处理措施" prop="handleContent">
                <el-input
                  v-model="addEditForm.handleContent"
                  :rows="3"
                  placeholder="请输入处理措施"
                  type="textarea"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="异常图片">
                <div v-if="addEditForm.fileArr.length" class="image-box">
                  <el-image
                    v-for="(fileUrl, index) in addEditForm.fileArr"
                    :key="index"
                    :preview-src-list="addEditForm.fileArr"
                    :src="fileUrl"
                  >
                  </el-image>
                </div>
                <template v-else>-</template>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <div slot="footer" class="footer">
      <el-button :loading="saveLoading" type="primary" @click="handleSave">
        处置
      </el-button>
      <el-button @click="changePage('TablePage')">
        {{ $t('domestic.cancel') }}
      </el-button>
    </div>

    <!-- 外包公司选择弹层 
    <RoadSegmentDialog
      :is-show.sync="roadSegmentDialog"
      :selected-content.sync="addEditForm.roadSegment"
    /> -->
  </div>
</template>

<script>
import PubSub from 'pubsub-js'
import {tableAdd, tableDetail, tableEdit} from '@/api/patrolAndRescue/roadWatch/abnormalHandling'

// import RoadSegmentDialog from './components/RoadSegmentDialog/index.vue'
import {dictionaryEscape, getDictionaryByCode} from '@/utils/dictionary'

export default {
  name: 'DispositionPage',
  //components: {RoadSegmentDialog},
  props: {
    componentName: {
      default: 'DispositionPage',
      type: String
    }
  },
  data() {
    return {
      addEditForm: {
        subjectCode: '',
        roadSegment: {},
        fileArr: []
      },
      xcdwztDicts: getDictionaryByCode('xcdwzt'),
      warnDicts: getDictionaryByCode('xcycclfs'),
      dicts: getDictionaryByCode('ycclzt'),
      addEditRules: {
        subjectName: [
          {required: true, message: '项目明细名称不能为空', trigger: ['blur']}
        ],
        subjectCode: [
          {required: true, message: '项目明细编码不能为空', trigger: ['blur']}
        ],
        typeId: [
          {
            required: true,
            message: '项目明细类型不能为空',
            trigger: ['change', 'blur']
          }
        ],
        model: [
          {
            required: false,
            message: '规格型号不能为空',
            trigger: ['change', 'blur']
          }
        ],
        price: [
          {
            required: true,
            message: '收费单价不能为空',
            trigger: ['change', 'blur']
          }
        ],
        unit: [
          {
            required: true,
            message: '计费单位不能为空',
            trigger: ['change', 'blur']
          }
        ],
        'roadSegment.roadSegmentName': [
          {required: true, message: '所在路段不能为空', trigger: ['change']}
        ]
      },
      saveLoading: false, // 上传loading
      id: '', // id标识符
      pubSub: null,
      cancelTokenSource: null, // 用于存储取消令牌
      roadSegmentDialog: false // 是否展示选择所在路段
    }
  },
  watch: {
    componentName(newVal) {
      if (newVal === this.$options.name) {
        this.openPage()
      } else {
        this.closePage()
      }
    }
  },
  mounted() {
    this.pubSub = PubSub.subscribe('DispositionPage', (_, data) => {
      if (data.id) {
        console.log(1111111111)
        this.id = data.id
        this.getDetail(data.id)
      }
    })
  },
  methods: {
    getDictionaryByCode,

    // 打开页面执行
    openPage() {
    },

    // 关闭页面执行
    closePage() {
      const {pubSub, addEditRules} = this
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {
        pubSub,
        addEditRules
      })

      this.$nextTick(_ => {
        this.$refs.addEditForm.$el.scrollTop = 0
        this.$refs.addEditForm.clearValidate()
      })
    },

    // 获取详情
    async getDetail(id) {
      const {...addEditForm} = await tableDetail(id)
      if (addEditForm.fileUrl) {
        addEditForm.fileArr = addEditForm.fileUrl.split(',').map(item => {
          return (
            window.context.portal +
            '/file/onlinePreviewController/v1/getFileById_' +
            item
          )
        })
      }
      if (addEditForm.patrolDirection) {
        addEditForm.patrolDirection = dictionaryEscape(
          'xsfx',
          addEditForm.patrolDirection
        )
      }
      if (addEditForm.patrolCategory) {
        addEditForm.patrolCategory = dictionaryEscape(
          'xcdwlx',
          addEditForm.patrolCategory
        )
      }
      this.addEditForm = {
        ...addEditForm,
        positions: addEditForm.lng + ',' + addEditForm.lat
      }
    },

    // 名称输入框事件
    // handleInput(chinese = '') {
    //   if (chinese) {
    //     getCodeRequest({chinese, type: '0'}).then(
    //       res => (this.addEditForm.subjectCode = res.value)
    //     )
    //   } else {
    //     // 输入的值为空的时候，停止请求并清空编码
    //     getCodeRequest.cancel(_ => (this.addEditForm.subjectCode = ''))
    //   }
    // },

    // 保存
    handleSave() {
      this.$refs.addEditForm.validate(async (valid, object) => {
        if (!valid) {
          this.$message.warning(
            `有${
              Object.keys(object).length
            }个字段未通过校验，请正确填写表单内容。`
          )
        } else {
          this.saveLoading = true
          const myInterFace = this.id ? tableEdit : tableAdd

          const {
            roadSegment: {roadSegmentName = '', roadSegmentId = ''} = {},
            ...addEditForm
          } = this.addEditForm

          try {
            let {state, message} = await myInterFace({
              ...addEditForm,
              roadSegmentName,
              roadSegmentId
            })

            if (state) {
              message = this.id ? '编辑' : '新增'
              this.$message.success(`路损项目明细${message}成功`)
              this.changePage('TablePage', this.id ? 'edit' : 'add')
            }
          } finally {
            this.saveLoading = false
          }
        }
      })
    },

    // 展示弹层
    showDialog(dialogName) {
      if (!dialogName) return
      this[dialogName + 'Dialog'] = true
    },

    // 改变页面
    changePage(pageName = '', type = '') {
      if (!pageName) return
      this.$emit('changePage', pageName, _ => {
        PubSub.publish(pageName, {type})
      })
    }
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  }
}
</script>

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

.image-box {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 15px;
  margin: 5px 0;

  ::v-deep .el-image {
    width: 100px;
    height: 100px;
    border-radius: 4px;
    user-select: none;
    position: relative;
    transition: all 0.15s;
    box-shadow: 3px 3px 10px rgba(darken($--system-background-color, 30%), 0.3);

    .el-image__inner {
      transition: transform 0.2s;
      position: absolute;
    }

    &:hover {
      .el-image__inner {
        transform: scale(1.5);
      }
    }

    .el-image__error {
      background-color: $--system-box-color2;
    }

    .el-image__placeholder {
      background-color: $--color-text-placeholder;
    }

    &:has(.el-image__error, .el-image__placeholder) {
      box-shadow: none;
    }
  }
}

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

    .form-content {
      flex: 1;
    }
  }
}
</style>
