<template>
  <div class="fullheight">
    <ht-table
      @load="loadData"
      @row-dblclick="row => showDialog(row.id)"
      :data="data"
      :pageResult="pageResult"
      :selection="true"
      :default-sorter="[{direction: 'DESC', property: 'plan_year_'}]"
      :show-export="false"
      :show-custom-column="false"
      ref="htTable"
    >
      <template v-slot:toolbar>
        <el-input
          style="width:353px"
          v-model="value"
          clearable
          placeholder="请输入计划年来搜索"
          ref="input"
        ></el-input>
        <el-button
          slot="append"
          icon="el-icon-search"
          class="search-btn"
          @click="searchClick"
        ></el-button>
        <span style="padding:0 30px;color:#eee">|</span>
        <el-button
          size="small"
          @click="showDialog()"
          icon="el-icon-plus"
          type="primary"
          >添加</el-button
        >
        <ht-delete-button
          url="${cssc}/biz/scada/planElectric/v1/"
          :htTable="$refs.htTable"
          >删除</ht-delete-button
        >
      </template>
      <template>
        <ht-table-column
          prop="planYear"
          label="计划年"
          :sortable="true"
          :show-overflow-tooltip="true"
        >
        </ht-table-column>
        <!-- <ht-table-column
          prop="planMonth"
          label="计划月"
          :sortable="true"
          :show-overflow-tooltip="true"
        >
        </ht-table-column> -->
        <ht-table-column
          prop="planElectric"
          label="计划电量(MW)"
          :sortable="true"
          :show-overflow-tooltip="true"
        >
        </ht-table-column>
      </template>
    </ht-table>

    <common-dialog
      width="800px"
      title="计划电量配置"
      :visible="dialogVisible"
      :before-close="beforeCloseDialog"
      destroy-on-close
    >
      <el-form v-form data-vv-scope="BizPlanInfoForm" class="form-single-row">
        <ht-form-item label="计划年" label-width="120px">
          <ht-input
            v-model="BizPlanInfo.planYear"
            :validate="{required: true, min_value: '1990', max_value: '2100'}"
            :disabled="disabled"
            placeholder="请输入计划年"
          />
        </ht-form-item>
        <!-- <ht-form-item label="计划月" label-width="120px">
          <ht-input
            v-model="BizPlanInfo.planMonth"
            :validate="{required: true, min_value: '1', max_value: '12'}"
            :disabled="disabled"
          />
        </ht-form-item> -->
        <ht-form-item label="计划电量(MW)" label-width="120px">
          <ht-input
            v-model="BizPlanInfo.planElectric"
            placeholder="请输入计划电量"
          />
        </ht-form-item>
      </el-form>
      <div slot="footer" style="text-align: center">
        <ht-submit-button
          url="${cssc}/biz/scada/planElectric/v1/"
          :model="BizPlanInfo"
          :request-method="saveMethod"
          scope-name="BizPlanInfoForm"
          @after-save-data="afterSaveData"
          >保存</ht-submit-button
        >
        <el-button @click="beforeCloseDialog">取消</el-button>
      </div>
    </common-dialog>
  </div>
</template>
<script>
export default {
  name: 'plan-electric-config',
  data() {
    return {
      value: '',
      disabled: false,
      dialogVisible: false,
      data: [],
      pageResult: {
        page: 1,
        pageSize: 50,
        total: 0
      },
      BizPlanInfo: {},
      saveMethod: 'POST'
    }
  },
  mounted() {
    this.$refs.input.focus()
  },
  watch: {},
  methods: {
    searchClick() {
      let pageBean = {
        page: 1,
        pageSize: 50,
        total: 0
      }
      let querys = [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'planYear',
          relation: 'AND',
          value: parseInt(this.value)
        }
      ]
      let sorter = [{direction: 'DESC', property: 'plan_year_'}]
      let param = {}
      if (this.value != '') {
        param = {pageBean, querys, sorter}
      }
      this.$http
        .post('${cssc}/biz/scada/planElectric/v1/query', param)
        .then(res => {
          let response = res.data
          this.data = response.rows
          this.pageResult = {
            page: response.page,
            pageSize: response.pageSize,
            total: response.total
          }
        })
    },
    loadData(param, cb) {
      this.$http
        .post('${cssc}/biz/scada/planElectric/v1/query', param)
        .then(
          res => {
            let response = res.data
            this.data = response.rows
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total
            }
          },
          error => {
            reject(error)
          }
        )
        .finally(() => cb())
    },
    showDialog(id) {
      if (id) {
        this.disabled = true
        this.saveMethod = 'PUT'
        this.$http.get('${cssc}/biz/scada/planElectric/v1/' + id).then(
          resp => {
            this.BizPlanInfo = resp.data
            this.dialogVisible = true
          },
          error => {
            reject(error)
          }
        )
      } else {
        this.dialogVisible = true
        this.saveMethod = 'POST'
      }
    },
    beforeCloseDialog() {
      this.BizPlanInfo = {}
      this.dialogVisible = false
    },
    afterSaveData() {
      setTimeout(() => {
        this.beforeCloseDialog()
        this.$refs.htTable.load()
      }, 500)
    }
  }
}
</script>
<style lang="scss" scoped>
.search-btn {
  background-color: #f5f7fa;
  border-radius: 0;

  color: #adb1b5;
}
</style>
