<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      width="50%"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="160px" class="demo-ruleForm">
        <el-form-item label="统一社会信用代码" prop="uniscid">
          <el-input v-model="ruleForm.uniscid" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="年报年" prop="ancheyear">
          <el-select v-model="ruleForm.ancheyear" placeholder="请选择年报年">
            <el-option
              v-for="item in ancheyearOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />>
          </el-select>
        </el-form-item>
        <el-form-item prop="timeStamp" label="填报时间">
          <el-date-picker
            v-model="ruleForm.timeStamp"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
            value-format="yyyy-MM-dd hh:mm:ss"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="允许次数" prop="allowTime">
          <el-input-number v-model="ruleForm.allowTime" placeholder="请输入允许次数" :controls="false" />
        </el-form-item>
        <el-form-item>
          <span v-if="title==='添加'">
            <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </span>
          <span v-else>
            <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { anModifyUpdata, anModifySave, anModifyGet } from '@/api/年报授权修改.js'
export default {
  data() {
    return {
      dataId: '',
      title: '',
      dialogVisible: false,
      ruleForm: {
        allowTime: 0,
        endTime: '',
        beginTime: '',
        ancheyear: '',
        uniscid: '',
        timeStamp: []
      },
      ancheyearOptions: [],
      rules: {
        allowTime: [
          { required: true, message: '请输入允许次数', trigger: 'blur' }
        ],
        timeStamp: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ],
        ancheyear: [
          { required: true, message: '请选择年报年', trigger: 'change' }
        ],
        uniscid: [
          { required: true, message: '请输入统一社会信用代码', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    id(newVal, oldVal) {
      this.dataId = newVal
    }
  },
  created() {

  },
  mounted() {
    this.generateYearList()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const api = this.title === '添加' ? anModifySave : anModifyUpdata
          const submitForm = {
            ...this.ruleForm,
            begintime: this.ruleForm.timeStamp[0],
            endtime: this.ruleForm.timeStamp[1]
          }
          api(submitForm).then(res => {
            if (res.code === 0) {
              this.dialogVisible = false
              this.$message({
                message: this.title + '成功！',
                type: 'success'
              })
              this.$emit('refreshList')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    generateYearList(endYear = new Date().getFullYear()) {
      this.ancheyearOptions = []
      const startYear = 1990
      for (let year = startYear; year <= endYear; year++) {
        this.ancheyearOptions.push({ value: year, label: year + '年' })
      }
    },
    openDialog() {
      this.dialogVisible = true
      this.title = '添加'
      this.ruleForm = {
        allowTime: 0,
        endTime: '',
        beginTime: '',
        ancheyear: '',
        uniscid: '',
        timeStamp: []
      }
    },
    updateDialog(id) {
      this.dialogVisible = true
      this.title = '修改'
      anModifyGet(id).then(res => {
        const data = res.data
        const beginTime = data.begintime ? this.$moment(data.begintime).format('YYYY-MM-DD HH:mm:ss') : ''
        const endTime = data.endtime ? this.$moment(data.endtime).format('YYYY-MM-DD HH:mm:ss') : ''
        this.ruleForm = {
          id: data.id,
          allowTime: data.allowTime,
          ancheyear: data.ancheyear,
          uniscid: data.uniscid,
          timeStamp: [beginTime, endTime]
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
