<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" plain @click="handleAdd" class="myButton">新增</el-button>
    <!--上报对象表格-->
    <el-table
      :data="tableData"
      :header-cell-style="{background:'#ECF1FE',}"
      v-loading="tableLoading" border style="width: 100%">
      <el-table-column prop="fileName" align="center" label="文件名关键字" width="200px"></el-table-column>
      <el-table-column prop="forecastTime" align="center" label="预测时间公式"  width="300px"></el-table-column>
      <el-table-column prop="fpValue" align="center" label="预测数据公式"  width="300px"></el-table-column>
      <el-table-column prop="dataType" align="center" label="数据类型" width="100px" :formatter="formatDataType"></el-table-column>
      <el-table-column prop="stationCode" align="center" label="场站编号"></el-table-column>
      <el-table-column label="操作" align="center" width="150px">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=this.page.currentPage
        :page-sizes="[10, 15, 30, 50]"
        :page-size=this.page.pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total=this.page.total>
      </el-pagination>
    </div>
    <!--编辑框-->
    <el-dialog :title="saveFlag==1?'新增':'编辑'" :visible.sync="visible" width="100%">

      <el-row :gutter="20">
        <el-col :span="16">
          <div>
                    <span style="color: red">
            <font size="4px">灰色文本框需要点击后，在右侧进行编辑录入！</font>
                    </span>
          </div>
          &nbsp;
          <div class="grid-content bg-purple">
            <el-form v-model="form" ref="form" :key="timer">
              <el-row :gutter="16">
                <el-col :span="12">
                  <el-form-item prop="fileName" label-width="110px">
                    <span slot="label">
                      <font color="red">*</font>文件名关键字
                    </span>
                    <el-input v-model="form.fileName" @focus="clearformula"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="dataType" label-width="110px">
                    <span slot="label">
                      <font color="red">*</font>数据类型
                    </span>
                    <el-select v-model="form.dataType" clearable @focus="clearformula">
                      <el-option
                        v-for="item in this.dataType"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"/>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="16">
                <el-col :span="12">
                  <el-form-item prop="fpValue" label-width="110px" class="formulaColor">
                    <span slot="label">
                      <font color="red">*</font>预测数据公式
                    </span>
                    <el-input v-model="form.fpValue" readonly
                              @click.native="ftck(form.fpValue,'form.fpValue','(预测数据)')"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item prop="forecastTime" label-width="110px"
                                class="formulaColor">
                    <span slot="label">
                      <font color="red">*</font>预测时间公式
                    </span>
                    <el-input v-model="form.forecastTime" readonly
                              @click.native="ftck(form.forecastTime,'form.forecastTime','(预测时间)')"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item prop="stationCode" label-width="110px">
                    <span slot="label">
                      <font color="red">*</font>场站编号
                    </span>
                    <el-select v-model="form.stationCode" multiple clearable @focus="clearformula" style="width: 100%">
                      <el-option
                        v-for="item in this.stationCodeList"
                        :key="item.stationCode"
                        :label="item.stationCode"
                        :value="item.stationCode"/>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="handleSave('form')">
                <span v-if="saveFlag == 1">保 存</span>
                <span v-else>修 改</span>
              </el-button>
              <el-button @click="visible = false">取 消</el-button>
            </div>
          </div>
        </el-col>
        <el-col :span="1">
          <el-divider direction="vertical"></el-divider>
        </el-col>
        <el-col :span="6">
          <h2>公式编辑区{{ this.editinfo }}</h2>
          <el-form>
            <el-row>
              <el-col>
                <el-form-item prop="rownumber" label-width="110px">
                  <span slot="label">
                      <font color="red">*</font>解析行
                  </span>
                  <el-input v-model="pfform.rownumber"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item prop="columnumber" label-width="110px">
                  <span slot="label">
                      <font color="red">*</font>解析列
                  </span>
                  <el-input v-model="pfform.columnumber"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="标签名" prop="signname" label-width="110px">
                  <el-input v-model="pfform.signname"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col>
                <el-form-item label="时间格式" prop="timeformat" label-width="110px">
                  <el-select v-model="pfform.timeformat" clearable>
                    <el-option
                      v-for="item in this.timeFormat"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"/>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-button type="primary" @click="genformula">
                <span>生成公式</span>
              </el-button>
              <el-button type="primary" @click="destoryformula">
                <span>清除录入</span>
              </el-button>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>

import {addObj, putObj, delObj, fetchListPage} from "@/api/an/PFParsingDq";
import {fetchElectricFieldList} from "@/api/an/PFParsingCommon";

export default {
  name: "index",
  data() {
    return {
      stationCodeList: [],
      dataType: [
        {value: 's', label: '时间单行'},
        {value: 'm', label: '时间多行'}
      ],
      timeFormat: [
        {value: 'yyyy-MM-dd HH:mm:ss', label: 'yyyy-MM-dd HH:mm:ss'},
        {value: 'yyyy-MM-dd', label: 'yyyy-MM-dd'},
        {value: 'yyyy-MM-dd_HH:mm', label: 'yyyy-MM-dd_HH:mm'},
        {value: 'yyyy-MM-dd_HH:mm:ss', label: 'yyyy-MM-dd_HH:mm:ss'}
      ],
      editinfo: '',
      timer: new Date().getTime(),
      props: {multiple: true},
      staionList: [],//场站集合
      tableLoading: false,
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      info: '',
      visible: false,
      form: {
        fileName: '',
        forecastTime: '',
        fpValue: '',
        dataType: '',
        stationCode: ''
      },
      pfform: {
        rownumber: '',
        columnumber: '',
        signname: '',
        timeformat: ''
      },
      tempformulasign: '',
      saveFlag: 1
    };
  },
  mounted() {
    this.getElectricFieldList()
    this.getTableList()
  },
  methods: {
    formatDataType(row, column) {
      let belongTo = '未知类型'
      for (let i = 0; i < this.dataType.length; i++) {
        if (row.dataType === this.dataType[i].value) {
          belongTo = this.dataType[i].label
        }
      }
      return belongTo
    },
    getTableList() {
      this.tableLoading = true
      // 获取列表
      fetchListPage(Object.assign({
        current: this.page.currentPage,
        size: this.page.pageSize
      })).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch((e) => {
        this.tableLoading = false
      })
    },
    getElectricFieldList() {
      // 获取所有场站
      fetchElectricFieldList().then(response => {
        this.stationCodeList = response.data.data
        this.tableLoading = false
      }).catch((e) => {
        this.tableLoading = false
      })
    },
    destoryformula() {
      // 清空公式区字段
      if (this.tempformulasign == 'form.forecastTime') {
        this.form.forecastTime = ''
      } else if (this.tempformulasign == 'form.fpValue') {
        this.form.fpValue = ''
      }
      this.clearformula()
    },
    clearformula() {
      // 清空公式区字段
      this.pfform.rownumber = ''
      this.pfform.columnumber = ''
      this.pfform.signname = ''
      this.pfform.timeformat = ''
      this.editinfo = ''
      this.tempformulasign = ''
    },
    ftck(obj, sign, editname) {
      this.clearformula()
      if (obj != undefined && obj != null) {
        var objs = obj.split('<=>')
        for (let i = 0; i < objs.length; i++) {
          if (objs[i] != "") {
            if (i == 0) {
              this.pfform.rownumber = objs[i]
            } else if (i == 1) {
              this.pfform.columnumber = objs[i]
            } else if (i == 2) {
              this.pfform.signname = objs[i]
            } else if (i == 3) {
              this.pfform.timeformat = objs[i]
            }
          }
        }
      }
      this.tempformulasign = sign;
      this.editinfo = editname
    },
    genformula() {
      if (this.tempformulasign==''){
        this.$message.warning('请先选定左侧公式项')
        return
      }
      if (!this.pfform.rownumber || !this.pfform.columnumber) {
        this.$message.warning('填写必填项')
        return
      }
      if ((this.pfform.signname && !this.pfform.timeformat) || (!this.pfform.signname && this.pfform.timeformat)) {
        this.$message.warning('标签名称和时间格式要同时填写')
        return
      }
      // 拼装公式
      var resultStr = ''
      if (this.pfform.rownumber) {
        resultStr = this.pfform.rownumber
      }
      if (this.pfform.columnumber) {
        resultStr = resultStr + '<=>' + this.pfform.columnumber
      }
      if (this.pfform.signname) {
        resultStr = resultStr + '<=>' + this.pfform.signname
      }
      if (this.pfform.timeformat) {
        resultStr = resultStr + '<=>' + this.pfform.timeformat
      }
      // 将生成的公式赋值到左侧表格中
      if (this.tempformulasign == 'form.forecastTime') {
        this.form.forecastTime = resultStr
      } else if (this.tempformulasign == 'form.fpValue') {
        this.form.fpValue = resultStr
      }
      this.timer = new Date().getTime()
      this.clearformula()
    },
    /*保存*/
    handleSave(formName) {
      if (!this.form.fileName || !this.form.forecastTime || !this.form.fpValue || !this.form.dataType || this.form.stationCode=="") {
        this.$message.warning('填写必填项');
        return false;
      } else {
        const data = this.form.stationCode
        let data1 = "";
        for (let n = 0; n < data.length; n++) {
          for (let j = 0; j < this.stationCodeList.length; j++) {
            if (data[n] == this.stationCodeList[j]["stationCode"]) {
              data1 = data1 + this.stationCodeList[j]["stationCode"] + ","
            }
          }
        }
        data1 = data1.substr(0, data1.length - 1)
        this.form.stationCode = data1
        if (this.saveFlag === 1) {
          addObj(this.form).then(response => {
            this.dialogVisible = false
            this.getTableList()
            this.tableLoading = false
            this.$message.success(response.data.data)
          }).catch(() => {
            this.tableLoading = false
          })
        } else {
          console.log(this.form)
          putObj(this.form).then(response => {
            this.dialogVisible = false
            this.getTableList()
            this.tableLoading = false
            this.$message.success(response.data.data)
          }).catch(() => {
            this.tableLoading = false
          })
        }
        this.visible = false
      }
    },
    /*上报对象*/
    handleAdd() {
      this.saveFlag = 1
      this.info = ''
      this.form = {}
      this.clearformula()
      this.visible = true
    },
    /*上报对象编辑*/
    handleEdit(row) {
      this.saveFlag = 2
      let newRow = Object.assign({},row)
      let tempStationCode = newRow.stationCode.split(',')
      this.form = newRow
      this.form.stationCode = tempStationCode
      this.clearformula()
      this.visible = true
    },
    /*上报对象删除*/
    handleDelete(row) {
      this.$confirm('是否确认删除此条配置?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delObj(row.id).then(response => {
          this.getTableList()
          this.$message.success(response.data.data)
        })
      })
    },
    /*上报对象pageSize改变*/
    handleSizeChange(val) {
      this.page.pageSize = val
      this.page.currentPage = 1
      this.getTableList()

    },
    /*上报对象currentPage改变*/
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getTableList()
    }
  }
}
</script>

<style scoped>
.block {
  float: right;
}

.myButton {
  margin-bottom: .5%;
}

.uploadno {
  background: #2d8cf0;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  text-align: center;
  display: inline-block;
  color: #fff;
}

.el-divider--vertical {
  display: inline-block;
  width: 1px;
  height: 35em;
  margin: 0 8px;
  vertical-align: middle;
  position: relative;
}

/deep/ .formulaColor .el-input--small .el-input__inner {
  height: 32px;
  line-height: 32px;
  background-color: #cccccc;
}

</style>
