<template>
  <div>
    <el-form
      inline
      :model="form"
      :rules="rules"
      ref="form"
      class="form-wrap dialog-info-full"
      label-width="180px">

      <el-form-item
        verify
        class="is-required"
        label="项目的中文名称"
        prop="projectNickName">
        <el-input v-model="form.projectNickName"></el-input>
      </el-form-item>
      <el-form-item
        label="项目描述"
        prop="projectDesc">
        <el-input v-model="form.projectDesc"></el-input>
      </el-form-item>
      <el-form-item
        label="线下系统的地址"
        prop="offlineUrl">
        <el-input v-model="form.offlineUrl"></el-input>
      </el-form-item>
      <el-form-item
        label="项目的有效期截止时间"
        prop="validEndTime">
        <el-date-picker
          :editable="false"
          :clearable="false"
          v-model="form.validEndTime"
          type="datetime"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div class="dialog-btn-wrap">
      <el-button @click="handleClose">关闭</el-button>
      <el-button
        @click="submit"
        type="primary">保存</el-button>
    </div>
  </div>
</template>

<script>
import { updateProject } from '@/api/platform'
import moment from 'moment'

export default {
  props: {
    projectItem: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      visible: false,
      form: {
        projectNickName: '',
        projectDesc: '',
        offlineUrl: '',
        validEndTime: moment().add(99, 'year').toDate() // 默认有效期为1年
      },
      rules: {}
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    reset () {
      this.$refs.form.resetFields()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.updateAccount()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    updateAccount () {
      if (this.form.validEndTime) {
        this.form.validEndTime = moment(this.form.validEndTime).format('YYYY-MM-DD HH:mm:ss')
      }
      updateProject(this.form).then(res => {
        this.$message.success('保存成功')
        this.reset()
        this.handleClose()
        this.$emit('updateList')
      })
    }
  },
  watch: {
    async visible (val) {
      if (!val) return
      // 获取角色列表
      // 重置表单
      this.reset()
      // 初始化编辑数据
      if (this.projectItem) {
        const { id, projectNickName, projectDesc, offlineUrl, validEndTime } = this.projectItem
        this.form = {
          id,
          projectNickName,
          projectDesc,
          offlineUrl,
          validEndTime: validEndTime ? moment(validEndTime).toDate() : moment().add(99, 'year').toDate()
        }
      }
    }
  }
}
</script>

<style scoped>
.btn-wrap{
  padding-right: 20px;
}
</style>
