<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 部队行动数据`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="60%"
    @closed="closed"
    @open="open"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row :gutter="24" style="flex-wrap:wrap" type="flex">
        <el-col :span="8">
          <el-form-item label="单位" prop="orgName">
            <el-input
              disabled
              v-model="form.orgName"
              clearable
              placeholder="请输入单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="行动名称" prop="actionName">
            <el-input
              v-model="form.actionName"
              clearable
              placeholder="请输入行动名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="兵力编组" prop="troopFormation">
            <el-input
              v-model="form.troopFormation"
              clearable
              placeholder="请输入兵力编组"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="行动时间" prop="actionTime">
            <el-date-picker
              style="width: 100%;"
              v-model="form.actionTime"
              clearable
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择行动时间"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="环境条件" prop="environmentalConditions">
            <el-input
              v-model="form.environmentalConditions"
              clearable
              placeholder="请输入环境条件"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="机动速度" prop="maneuveringSpeed">
            <el-input
              v-model="form.maneuveringSpeed"
              clearable
              placeholder="请输入机动速度"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="打击效果" prop="strikeEffect">
            <el-input
              v-model="form.strikeEffect"
              clearable
              placeholder="请输入打击效果"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="saveLoading" type="primary" @click="handleSave">
        确 认
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/examDataMenu/examActivityData/forceActionDataMenu'

export default {
  name: 'AddEditDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    },
    activeTreeItem: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        actionName: [
          {required: true, message: '请输入行动名称', trigger: 'blur'}
        ]
      },
      form: {},
      saveLoading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {},
  methods: {
    // 打开弹层
    open() {
      this.$set(this.form, 'orgName', this.activeTreeItem.name)
      this.$set(this.form, 'orgId', this.activeTreeItem.id)
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      }
    },

    // 关闭弹层后
    closed() {
      const {rules} = this
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {rules})

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

    // 获取详情
    async getDetail(id) {
      try {
        const {startTime = '', endTime = '', ...form} = await api.tableDetail(
            id
          ),
          startEndTime = startTime && endTime ? [startTime, endTime] : ''
        this.form = {...form, startEndTime}
      } finally {
        this.saveLoading = false
      }
    },

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

          const {
            startEndTime: [startTime = '', endTime = ''] = [],
            ...addEditForm
          } = this.form

          try {
            let {state, message} = await myInterFace({
              ...addEditForm,
              startTime,
              endTime
            })

            if (state) {
              this.$emit('submit', this.id ? 'edit' : 'add')
              message = this.id ? '编辑' : '新增'
              this.$message.success(`部队行动数据${message}成功`)
              this.dialogVisible = false
            }
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
