<template>
  <div class="view-container" v-loading="loading">
    <el-form
      ref="addForm"
      :rules="rules"
      :model="settingsForm"
      label-position="right"
      label-width="80px"
      style="width: 100%;">
      <div class="flexr">
        <div class="view-content flex1">
          <div class="section-title">CHARGER TYPE SETTINGS</div>
          <div class="rate-list-view" v-for="(item, index) in settingsForm.chargeTypes" :key="index">
            <el-form-item
              label="Type:"
              :prop="'chargeTypes.'+index+'.chargeType'"
              :rules="rules.chargeTypes.chargeType">
              <el-select
                v-model="item.chargeType"
                class="rate-text">
                <el-option
                  v-for="i in chargeTypeOptions"
                  :key="i"
                  :label="i"
                  :value="i" />
              </el-select>
            </el-form-item>
            <el-form-item
              label="Wattage:"
              :prop="'chargeTypes.'+index+'.wattage'"
              :rules="rules.chargeTypes.wattage">
              <el-input
                v-model="item.wattage"
                class="rate-text"
                maxlength="5"/>
            </el-form-item>
            <img
              class="list-item-icon"
              @click="subChargeType(index)"
              src="@/assets/form-list-sub.png"
              v-if="settingsForm.chargeTypes.length > 1"/>
            <img
              class="list-item-icon"
              @click="addChargeType"
              src="@/assets/form-list-add.png"
              v-if="index == settingsForm.chargeTypes.length - 1"/>
          </div>
        </div>
        <div class="view-content flex1">
          <div class="section-title">CHARGER TYPE SUMMARIES</div>
          <div style="width: 100%; overflow-x: auto;">
            <el-table
              class="table-types no-border"
              :data="settingsForm.summaries"
              :fit="true">
              <el-table-column
                label="Type"
                prop="chargeType"
                align="center"/>
              <el-table-column
                label="Wattage"
                prop="wattage"
                align="center"/>
              <el-table-column
                label="Number of Connectors"
                prop="countConnector"
                align="center"
                width="200"/>
            </el-table>
          </div>
        </div>
      </div>
      <div class="view-content flexcr">
        <div class="buttons">
          <el-button
            class="cancel-button"
            type="primary"
            @click="handleClickCancleButton">
              Cancel
          </el-button>
          <el-button
            class="confirm-button"
            type="primary"
            @click="handleClickSaveButton">
              Save
          </el-button>
        </div>
        <div
          class="update-by"
          v-if="isEdit">
          <span
            class="add-text"
            :title='"CREATED BY " + settingsForm.createdBy + " ON " + settingsForm.createdOn'>
            LAST UPDATED BY {{settingsForm.updatedBy}} TIMESTAMP: {{settingsForm.updatedOn}}
          </span>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import api from '@/http/api/settings'
import site from '@/http/api/site'
export default {
  data() {
    return {
      isEdit: false,
      loading: true,
      settingsForm: {
        chargeTypes: "",
        summaryData: []
      },
      chargeType: {
        chargeType: '',
        chargeTypePk: '',
        wattage: ''
      },
      rules: {
        chargeTypes: {
          chargeType: {
            required: true,
            trigger: 'change',
            message: 'Charge Type is required',
          },
          wattage: [{
            required: true,
            trigger: 'change',
            message: 'Wattage is required',
          }, {
            pattern: /^[1-9]+\d*.?\d*$/,
            trigger: 'blur',
            message: 'Please type a correct wattage'
          }]
        }
      },
      chargeTypeOptions: []
    }
  },
  created() {
    this.getChargeTypeOptions();
  },
  methods: {
    getChargeTypeOptions() {
      site.getTypeList().then(res => {
        if (res.data) {
          this.chargeTypeOptions = res.data
          this.chargeType.chargeType = res.data[0]
          if (!this.settingsForm.chargeTypes) {
            this.settingsForm.chargeTypes = []
            this.addChargeType()
          }
        }
        this.getChargeTypeList();
      }).catch(err => {
        this.loading = false;
        this.$message({
          message: error,
          type: "error",
          duration: 5000,
        })
      });
    },
    getChargeTypeList() {
      api.getAllChargeTypes().then(res => {
        if (res.data) {
          this.settingsForm = res.data
        }
      }).catch(err => {
        this.$message({
          message: error,
          type: "error",
          duration: 5000,
        })
      }).finally(() => {
        this.loading = false;
      })
    },
    subChargeType(index) {
      var chargeType = this.settingsForm.chargeTypes[index]
      if (chargeType.chargeTypePk) {
        this.$confirm('Confirm delete this charge type?', 'Delete', {
          confirmButtonText: 'Confirm',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(res => {
          this.deleteChargeType(chargeType.chargeTypePk, () => {
            this.settingsForm.chargeTypes.splice(index, 1)
          })
        });
      } else {
        this.settingsForm.chargeTypes.splice(index, 1)
      }
    },
    addChargeType() {
      this.settingsForm.chargeTypes.push(JSON.parse(JSON.stringify(this.chargeType)))
    },
    deleteChargeType(id, back) {
      api.deleteChargeType(id).then(res => {
        back()
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
      })
    },
    handleClickCancleButton() {
      this.$router.go(-1);
    },
    handleClickSaveButton() {
      this.$refs['addForm'].validate(result => {
        if (result) {
          this.loading = true;
          api.saveChargeTypes(this.settingsForm).then(res => {
            this.$message({
              message: 'Save charge types successfully',
              type: 'success'
            });
          }).catch(err => {
            this.$message({
              message: err,
              type: 'error'
            })
          }).finally(() => {
            this.loading = false;
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '../../styles/element-ui.scss';
  @import '../../styles/variables.scss';
  
  .view-container {
    width: 100%;
    padding: 20px 60px;
    min-height: $mainAppMinHeight;
    background-color: #F0F5FC;
  }
  
  .view-content {
    min-width: 35vw;
    margin: 0 8px 16px;
    padding: 15px 50px;
    border-radius: 6px;
    background-color: white;
  }
  
  .section-title {
    color: #333333;
    margin-top: 20px;
    margin-bottom: 30px;
    font-size: 15px;
    line-height: 24px;
    font-weight: 700;
    font-family: sans-serif;
    text-transform: uppercase;
  }
  
  .section-sub-title {
    font-size: 14px;
    padding-left: 5px;
    font-weight: normal;
  }
  
  .input-text {
    width: 100% !important;
    max-width: 300px;
  }
  
  .add-text {
    width: 100%;
    font-size: 14px;
    max-width: 300px;
  }
  
  .add-text ::v-deep .el-textarea__inner,
  .input-text ::v-deep .el-textarea__inner {
    font-family: sans-serif;
  }
  
  .rate-list-view {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
  }
  .rate-text {
    max-width: 100px;
    padding-right: 14px;
  }
  .list-item-icon {
    width: 30px;
    height: 30px;
    cursor: pointer;
    margin: 0 10px 22px;
  }
  
  .buttons {
    padding-top: 15px;
    padding-bottom: 15px;
  }
  
  .cancel-button {
    width: 94px;
    height: 40px;
  }
  
  .confirm-button {
    width: 94px;
    height: 40px;
    margin-left: 20px;
    background: $--color-primary;
    border: 1px solid $--color-primary;
    box-sizing: border-box;
    border-radius: 4px;
  }
  
  @media screen and (max-width: 1000px) {
    .view-container {
      padding: 10px 40px;
    }
    .view-content {
      min-width: 90vw;
      padding: 15px 40px;
    }
  }
  
  @media screen and (max-width: 800px) {
    .view-container {
      padding: 10px 20px;
    }
    .view-content {
      padding: 15px 30px;
    }
  }
  
  @media screen and (max-width: 500px) {
    .view-container {
      padding: 0px;
    }
    .view-content {
      padding: 15px 20px;
    }
  }
  .table-types {
    width: 100%;
    min-width: 500px;
  }
</style>