<template>
  <div class="app-container">

    <el-card>
      <div slot="header" class="clearfix">
        <b><span>场站信息</span></b>
      </div>
      <div>
        <el-card style="width: 80%;margin-left: 10%">
          <div style="height: 30px"/>
          <el-form ref="form" v-loading="tableLoading" :model="form" :rules="rules">
            <el-row class="row-bg el-row-two" justify="space-between" style="gutter:30;text-align: left">
              <el-col :span="12">
                <el-form-item label="场站编号：" label-width="180px" prop="stationCode">
                  <el-input v-model="form.stationCode" maxlength="6" placeholder="必填项"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站标识：" label-width="180px" prop="sign">
                  <el-input v-model="form.sign" maxlength="10" placeholder="必填项"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg el-row-two" justify="space-between" style="gutter:30">
              <el-col :span="12">
                <el-form-item label="场站名称：" label-width="180px" prop="name">
                  <el-input v-model="form.name" maxlength="50" placeholder="必填项"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="装机容量(MW)："
                  label-width="180px"
                  prop="capacity"
                  style="min-width: 110px"
                >
                  <el-input v-model="form.capacity" maxlength="85" placeholder="必填项"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row class="row-bg el-row-two" justify="space-between" style="gutter:30">
              <el-col :span="12">
                <el-form-item label="并网设备数：" label-width="180px" prop="gridce">
                  <el-input v-model="form.gridce" maxlength="10" placeholder="必填项"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站类型：" label-width="180px" prop="fieldType">
                  <el-select
                    v-model="form.fieldType"
                    filterable
                    placeholder="请选择"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in this.electricTypeList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg" justify="space-between" style="gutter:30">
              <el-col :span="12">
                <el-form-item label="场站位置：" label-width="180px" prop="location">
                  <el-input v-model="form.location" maxlength="100" placeholder="必填项"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站所属公司：" label-width="180px" prop="company">
                  <el-input v-model="form.company" maxlength="100" placeholder="必填项"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div style="text-align: center">
            <span slot="footer" class="dialog-footer">
              <el-button
                v-loading="tableLoading"
                style="width: 15%"
                type="primary"
                @click="saveElectricField('form')"
              >保 存</el-button>
            </span>
          </div>
          <div style="height: 30px"/>
        </el-card>
        <div style="height: 20px"/>
      </div>
    </el-card>
  </div>
</template>

<script>

import {debounce} from "lodash";

export default {
  data() {
    return {
      addVisible: false,
      loading: true,
      tableLoading: false,
      tableData: {},
      datas: {},
      showCreateButton: false,
      provinceTypeList: [],
      electricTypeList: [
        {value: '0', label: '光伏'},
        {value: '1', label: '风电'}
      ],
      form: {
        stationStatusEnum: {
          code: 'E1',
          message: '未运维'
        }
      },
      electricFieldStatrsEnumList: [],
      intervals: [{key: 0, value: 0, label: '不入库'}, {key: 60, value: 60, label: '一分钟'}, {
        key: 900,
        value: 900,
        label: '十五分钟'
      }],
      // 表单验证规则
      rules: {
        stationCode: [
          {required: true, message: '请填写场站编号'}
          // {message: '输入过长', max: 8}
        ],
        sign: [
          {required: true, message: '请填写标识名称'}
          // {message: '输入过长', max: 15}
        ],
        name: [
          {required: true, message: '请填写场站名称'}
          // {message: '输入过长', max: 50}
        ],
        capacity: [
          {required: true, message: '请正确填写装机容量'},
          {pattern: /^\d{0,4}(?:\.\d{0,2})?$/, message: '最多4位整数，小数部分最多2位'}
          // {message: '输入过长', max: 10}
        ],
        gridce: [
          {required: true, message: '请正确填写并网设备数'},
          {pattern: /^[0-9]*[1-9][0-9]*$/, message: '只能输入正整数数字'}
          // {message: '输入过长', max: 5}
        ],
        longitude: [
          {required: true, message: '请正确填写场站经度'},
          {pattern: /^\d+(\.\d{1,6})?$/, message: '只能输入正数数字或带小数点6位以内的数字'},
          {pattern: /^-?((0|1?[0-8]?[0-9]?)(([.][0-9]{1,10})?)|180(([.][0]{1,10})?))$/, message: '请输入正确的经度'}
          // {message: '输入过长', max: 10}
        ],
        latitude: [
          {required: true, message: '请正确填写场站纬度'},
          {pattern: /^\d+(\.\d{1,6})?$/, message: '只能输入正数数字或带小数点6位以内的数字'},
          {pattern: /^-?((0|[1-8]?[0-9]?)(([.][0-9]{1,10})?)|90(([.][0]{1,10})?))$/, message: '请输入正确的纬度'}
          // {message: '输入过长', max: 10}
        ],
        provinceEnum: [
          {required: true, message: '请选择上报省调类型'}
        ],
        netSubstationName: [
          {required: true, message: '请填写别名'}
          // {message: '输入过长', max: 50}
        ],
        electricFieldTypeEnum: [
          {required: true, message: '请填写场站类型'}
        ],
        interval: [
          {required: true, message: '请填写入库时间'}],
        altitude: [
          {required: true, message: '请填写场站海拔'},
          {pattern: /^\d+(\.\d{2})?$/, message: '只能输入正数数字或带两位小数的数字'}
          // {message: '输入过长', max: 50}
        ],
        company: [
          {required: true, message: '请填写场站所属公司'}
          // {message: '输入过长', max: 50}
        ],
        location: [
          {required: true, message: '请填写场站位置'}
          // {message: '输入过长', max: 50}
        ],
        area: [
          {required: true, message: '请填写场站面积'},
          {pattern: /^\d+(\.\d{2})?$/, message: '只能输入正数数字或带两位小数的数字'}
          // {message: '输入过长', max: 50}
        ]
      }
    }
  },
  created() {
    this.getData()
  },
  mounted() {
  },
  methods: {
    async getData() {
      await this.$axios.get('/electricField/getAll').then((res) => {
        this.form = res.data
        this.loading = false
      }).catch((error) => {
        this.loading = false;
        // this.$message.error(error)
      })
    },
    saveElectricField: debounce(function (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.tableLoading = true
          await this.$axios.post('/electricField/', this.form).then(res => {
            if (res.code == 0) {
              this.$message.success('场站信息保存成功')
            }
            if (res.code == 1) {
              this.$message.error(res.data)
            }
            this.getData()
            this.tableLoading = false
          }).catch((error) => {
            this.tableLoading = false
          })
        }
      });
    }, 1000)
  }
}
</script>

<style scoped>

</style>
