<template>
  <el-dialog
    title="请选择需设置的年度"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="640px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="95px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row>
        <el-col :span="24">
          <el-row>
            <el-col :span="24" style="text-align:left">
              <el-form-item label="年度" prop="dateTime">
                <el-row>
                  <el-col>
                    <el-select
                      placeholder="请选择年度"
                      style="width:100%"
                      v-model="baseForm.dateTime">
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
              
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-row :gutter="5">
            <el-col :span="18">
              <el-checkbox v-model="checked">
                <b>沿用上一年 </b>
                <p class="label_text">（沿用上一年的情况下，所有区域都创建一个和上年同样的水权设置信息可修改；<br>不选沿用的情况下，所有区域重新为0，重新设置）</p>
              </el-checkbox>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>

    <div style="text-align: right;margin-top:30px;">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { addWaterright } from '@/api/waterright/index.js'
import { assign } from 'lodash-es';
export default {
  name:'waterPriceSetting',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogSubmitLoading: false,
      dialogVisible: false,
      baseForm: {
        dateTime: ''
      },
      hasFetched: false,
      options:[
        {
          label: '2023',
          value: '2023'
        },{
          label: '2024',
          value: '2024'
        },{
          label: '2025',
          value: '2025'
        },{
          label: '2026',
          value: '2026'
        },{
          label: '2027',
          value: '2027'
        }
      ],
      checked: false,
      rules: {
        dateTime: [
          { required: true, message: '请选择您的年份', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
          if (!this.hasFetched) {
            this.hasFetched = true;
          }
        }
      }
    },
  },
  methods: {
    async addWaterrightHandler() {
      const useOldStatus = this.checked ? 1 : 0
      const waterrightYear = this.baseForm.dateTime
      await addWaterright({
        useOldStatus,
        waterrightYear
      })
      this.handleClose()
      this.$emit('refresh')
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        valid && this.addWaterrightHandler()
      })
    },
    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep .base-form{
  padding: 0;
}
::v-deep .el-checkbox{
  margin-top: 10px;
}
::v-deep .el-checkbox__input{
  margin-top: -75px;
}
::v-deep .el-checkbox__input.is-checked+.el-checkbox__label{
  p{
    color:#14A478;
  }
}
.label_text{
  width:550px;
  padding:0;
  margin:0;
  color: #999;
  font-size: 14px;
}
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
