<template>
  <super-dialog
    v-model="isShowDialog"
    :modal="false"
    :top="top"
    :width="width"
    title="批量完善"
    v-bind="$attrs"
    @closed="closed"
    @open="open"
  >
    <div :style="{height}" class="eventInfo-dialog">
      <div class="form-module">
        <el-form
          ref="addEditForm"
          :model="addEditForm"
          :rules="addEditRules"
          label-position="top"
          label-suffix="："
          label-width="100px"
        >
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="预警名称" prop="warningName">
                <el-input
                  v-model="addEditForm.warningName"
                  clearable
                  placeholder="请输入预警名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预警等级" prop="warningLevel">
                <el-radio-group
                  v-model="addEditForm.warningLevel"
                  border
                  size="medium"
                >
                  <el-radio
                    v-for="(item, index) in getDictionaryByCode('yxjc-yjdj')"
                    :key="index"
                    :label="Number(item.value)"
                    :value="Number(item.value)"
                    >{{ 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="预警发布单位" prop="warningUnit">
                <el-input
                  v-model="addEditForm.warningUnit"
                  clearable
                  placeholder="请输入预警发布单位"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="预警灾害类型" prop="warningValue">
                <el-select
                  v-model="addEditForm.warningValue"
                  clearable
                  placeholder="请选择预警灾害类型"
                  style="width: 100%"
                >
                  <el-option
                    v-for="(item, index) in getDictionaryByCode('yjlxdm')"
                    :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="warnRange">
                <el-input
                  v-model="addEditForm.warnRange"
                  clearable
                  placeholder="请输入可能受影响区县"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12"
              ><el-form-item label="预警时间" prop="warningTime">
                <el-date-picker
                  v-model="addEditForm.warningTime"
                  default-time="12:00:00"
                  format="yyyy-MM-dd HH:mm:ss"
                  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-form>
        <div class="content-bottom">
          <el-button size="small" @click="isShowDialog = false">
            取 消
          </el-button>
          <el-button
            :disabled="saveLoading"
            size="small"
            type="primary"
            @click="handleSave"
          >
            确 认
          </el-button>
        </div>
      </div>
    </div>
  </super-dialog>
</template>

<script>
import SuperDialog from '@/components/SuperDialog/index.vue'
import {getDictionaryByCode} from '@/utils/dictionary'
import {batchConfirm} from '@/api/patrolAndRescue/operationalMonitoring/calledPartyAnswer'

export default {
  name: 'BatchDialog',
  inheritAttrs: false,
  components: {
    SuperDialog
  },
  props: {
    isShow: {
      default: false,
      type: Boolean
    },
    top: {
      default: '14vh',
      type: String
    },
    width: {
      default: '60%',
      type: String
    },
    height: {
      default: 'auto',
      type: String
    },
    list: {
      default: () => [],
      type: Array
    },
    defaultData: {
      default: () => ({}),
      type: Object
    }
  },

  data() {
    return {
      addEditForm: {},
      saveLoading: false,
      addEditRules: {
        warningName: [
          {required: true, message: '请输入预警名称', trigger: 'blur'}
        ],
        
        warningLevel: [
          {required: true, message: '请选择预警等级', trigger: 'change'}
        ],
        
        warningValue: [
          {required: true, message: '请选择预警灾害类型', trigger: 'change'}
        ],
        warningTime: [
          {required: true, message: '请选择预警时间', trigger: 'change'}
        ],
        warnRange: [
          {required: true, message: '请输入可能受影响区县', trigger: 'blur'}
        ],
      }
    }
  },
  computed: {
    isShowDialog: {
      get() {
        return this.isShow
      },
      set(val) {
        this.$emit('update:isShow', val)
      }
    }
  },
  methods: {
    getDictionaryByCode,
    // 打开弹层时触发
    open() {
      this.$nextTick(() => {
        console.log('打开弹层', this.defaultData)
        const {defaultData} = this
        if (defaultData) {
          this.addEditForm = {...defaultData}
        }
      })
    },

    handleSave() {
      this.saveLoading = true
      const query = this.list.map(item => {
        item.warningName = this.addEditForm.warningName
        item.warningLevel = this.addEditForm.warningLevel
        item.warningUnit = this.addEditForm.warningUnit
        item.warningValue = this.addEditForm.warningValue
        item.warningTime = this.addEditForm.warningTime
        item.warnRange = this.addEditForm.warnRange
        item.pubStatus = 1
        return item
      })
      batchConfirm(query)
        .then(({state, message}) => {
          if (state) {
            this.$message.success('批量完善成功')
            this.isShowDialog = false
            this.$emit('change')
          } else {
            this.$message.error(message || '批量完善失败')
          }
        })
        .catch(error => {
          this.$message.error(error.message || '批量完善失败')
        })
        .finally(() => {
          this.saveLoading = false
        })
    },
    // 重置表单数据
    resetForm() {
      this.addEditForm = this.defaultData ? {...this.defaultData} : {}
      this.$nextTick(() => {
        this.$refs.addEditForm?.clearValidate()
      })
    },
    // 关闭弹层触发
    closed() {
      this.resetForm()
    }
  }
}
</script>

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

.eventInfo-dialog {
  width: 100%;
  display: flex;
  flex-direction: column;
  column-gap: 16px;

  .form-module {
    width: 100%;
    height: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    row-gap: 16px;
    background-color: $--system-module-color1;
    padding: 16px;
    box-sizing: border-box;
    border-radius: 8px;

    .confirm-tip {
      padding: 12px 16px;
      background: rgba(64, 158, 255, 0.1);
      border-left: 4px solid #409eff;
      border-radius: 4px;
      color: #e4e4e4;
      font-size: 14px;
      line-height: 1.5;

      i {
        color: #409eff;
        margin-right: 8px;
        font-size: 16px;
      }
    }

    .el-form {
      flex: 1;
    }

    .content-bottom {
      display: flex;
      justify-content: center;
      height: 40px;
      margin-top: 16px;
    }
  }
}

::v-deep::-webkit-scrollbar-thumb,
::v-deep::-webkit-scrollbar {
  display: none;
}
</style>
