<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 style="gutter:30;text-align: left" class="row-bg el-row-two" justify="space-between">
              <el-col :span="12">
                <el-form-item label="场站编号：" prop="stationCode" label-width="180px">
                  <el-input v-model="form.stationCode" placeholder="必填项" maxlength="6" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站标识：" prop="sign" label-width="180px">
                  <el-input v-model="form.sign" placeholder="必填项" maxlength="10" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row style="gutter:30" class="row-bg el-row-two" justify="space-between">
              <el-col :span="12">
                <el-form-item label="场站名称：" prop="name" label-width="180px">
                  <el-input v-model="form.name" placeholder="必填项" maxlength="50" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="装机容量(MW)："
                  prop="capacity"
                  label-width="180px"
                  style="min-width: 110px"
                >
                  <el-input v-model="form.capacity" placeholder="必填项" maxlength="85" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-row style="gutter:30" class="row-bg el-row-two" justify="space-between">
              <el-col :span="12">
                <el-form-item label="并网设备数：" prop="gridce" label-width="180px">
                  <el-input v-model="form.gridce" placeholder="必填项" maxlength="10" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站类型：" label-width="180px" prop="fieldType">
                  <el-select
                    v-model="form.fieldType"
                    placeholder="请选择"
                    filterable
                    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 style="gutter:30" class="row-bg" justify="space-between">
              <el-col :span="12">
                <el-form-item label="场站位置：" prop="location" label-width="180px">
                  <el-input v-model="form.location" placeholder="必填项" maxlength="100" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="场站所属公司：" prop="company" label-width="180px">
                  <el-input v-model="form.company" placeholder="必填项" maxlength="100" />
                </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"
                type="primary"
                style="width: 15%"
                @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+(\.\d{0,2})?$/, message: '只能输入正数数字或带两位小数的数字' }
          // {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() {
      let sysTime
      let lk
      await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
        sysTime = res.data.sysTime
        lk = res.data.lk
      }).catch((error) => {
      })

      var searchParams = {
        sysTime: sysTime,
        lk: lk
      }
      await this.$axios.get('/electricField/getAll',{params: searchParams}).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) {
          let sysTime
          let lk
          await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
            sysTime = res.data.sysTime
            lk = res.data.lk
          }).catch((error) => {
          })

          this.form.sysTime = sysTime
          this.form.lk = lk
          this.tableLoading = true
          await this.$axios.post('/electricField/', this.form).then(res => {
            if (res.code == 0) {
              this.$message.success('场站信息保存成功')
              this.getData()
            }
            if (res.code == 1) {
              this.$message.error(res.data)
            }
            this.tableLoading = false
          }).catch((error) => {
            this.$message.error('场站信息保存出错' + error)
            this.tableLoading = false
          })
        }
      });
    },1000)
  }
}
</script>

<style scoped>

</style>
