<template>
  <BaseDialog v-bind="$attrs" v-on="listeners" @submit="onSubmit" :loading="loading">
    <el-form class="form" ref="form" v-bind="{ labelWidth: '7em', size: 'small' }" :model="formData" :rules="rules">
      <el-tooltip :content="no" placement="top">
        <el-form-item required label="监测点编号">
          <el-input v-model="no" disabled />
        </el-form-item>
      </el-tooltip>

      <el-form-item required label="修改人">
        <el-input v-model="userName" placeholder="请输入修改人" clearable />
      </el-form-item>
      <el-form-item required label="修改时间" prop="updateTime">
        <el-date-picker
          type="datetime"
          v-model="formData.updateTime"
          placeholder="请选择修改时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width:100%"
        />
      </el-form-item>
      <el-form-item required label="站点状态" prop="runState">
        <el-switch v-model="formData.runState" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="说明" prop="explain">
        <el-input v-model="formData.explain" type="textarea" :rows="4" clearable />
      </el-form-item>
    </el-form>
  </BaseDialog>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator'
import BaseDialog from '@/components/BaseDialog/index.vue'
import { ElForm } from 'element-ui/types/form'
import { IPoint, IPointEnableParams } from '../api/common'

@Component({ name: 'EnableForm', components: { BaseDialog } })
export default class EnableForm extends Vue {
  @Prop({ type: Array, default: () => [] }) selected!: IPoint[]
  @Prop({ type: Object, default: () => ({}) }) data!: IPointEnableParams
  @Prop({ type: Boolean, default: () => false }) loading!: boolean
  $refs!: { form: ElForm }
  userName: string = ''
  formData: Partial<IPointEnableParams> = {
    explain: undefined,
    runState: undefined,
    runStateName: undefined,
    updateTime: undefined,
    updateUser: undefined,
    list: []
  }

  no: string = ''

  get listeners() {
    const { submit, ...rest } = this.$listeners
    return rest
  }

  rules = {
    // updateUser: [{ required: true, message: '请输入操作人姓名' }],
    updateTime: [{ required: true, message: '请选择修改时间' }],
    runState: [{ required: true, message: '请选择站点状态' }],
    explain: [{ required: false, max: 255, message: '说明最长为255个字符' }]
  }

  onSubmit() {
    this.$refs.form.validate((valid) => {
      if (valid) {
        this.$emit('submit', this.formData)
      }
    })
  }

  @Watch('data', { immediate: true })
  setDefaultValue(val: IPoint) {
    const { userId, realName } = this.$store.state.user || {}
    const selected = this.selected.filter((item) => !!item.deviceId)
    this.userName = realName
    const disabledPoints = !val.id ? selected.filter((item) => item.siteState !== 1) : val.siteState === 1 ? [] : [val]
    this.formData = {
      list: (val.id
        ? [{ siteId: val.id, deviceId: val.deviceId }]
        : (disabledPoints.length ? disabledPoints : selected).map(({ id: siteId, deviceId }) => ({
            siteId,
            deviceId
          }))
      ).filter((item) => !!item.deviceId),
      updateUser: userId,
      updateTime: this.$moment().format('YYYY-MM-DD HH:mm:ss'),
      runState: val.id ? (val.siteState === 1 ? 0 : 1) : disabledPoints.length ? 1 : 0,
      runStateName: val.id ? (val.siteState === 1 ? '停用' : '启用') : disabledPoints.length ? '启用' : '停用',
      explain: ''
    }
    this.no = val.id
      ? val.siteCode
      : (disabledPoints.length ? disabledPoints : selected).map((item) => item.siteCode).join(', ')
  }
}
</script>
