<template>
  <div class="card-container">
    <div class="card-content">
      <el-form
        :model="editForm"
        :rules="rule"
        v-loading="loading"
        ref="editForm"
        label-position="right"
        label-width="180px">
        <div class="section-title">Monthly Credit Limit:</div>
        <el-row :gutter="20">
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Fleet Company:"
              prop="fleetCompanyId">
              <el-select
                class="add-text"
                remote
                filterable
                :remote-method="filterCompany"
                v-model="editForm.fleetCompanyId"
                placeholder="Select with search">
                <el-option
                  v-for="(item, index) in companyOptions"
                  :key="index"
                  :label="item.fleetCompanyName"
                  :value="item.fleetCompanyId"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Status:"
              prop="registrationStatus">
              <el-select
                class="add-text"
                v-model="editForm.creditLimitStatus"
                placeholder="Select">
                <el-option
                  v-for="(item, index) in statusOptions"
                  :key="index"
                  :label="item.name"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Monthly Credit Limit:"
              prop="creditLimitAmount">
              <span class="item-unit">(S$)</span>
              <el-input
                class="add-text"
                v-model="editForm.creditLimitAmount"
                placeholder="Add text"
                maxlength="10"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Effective Start Date:"
              prop="effectiveStartDate">
              <el-date-picker
                class="add-text"
                v-model="editForm.effectiveStartDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="Select date"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Used Credit Limit:">
              <span class="item-unit">(S$)</span>
              <el-input
                class="add-text"
                v-model="editForm.usedCreditLimitAmount"
                readonly
                maxlength="10"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Effective End Date:"
              prop="effectiveEndDate">
              <el-date-picker
                class="add-text"
                v-model="editForm.effectiveEndDate"
                type="date"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                placeholder="Select date"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Remaining Credit Limit:">
              <span class="item-unit">(S$)</span>
              <el-input
                class="add-text"
                v-model="editForm.creditLimitRemainingAmount"
                readonly
                maxlength="10"/>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :md="12">
            <el-form-item
              label="Deposit Received:"
              prop="depositReceivedAmount">
              <span class="item-unit">(S$)</span>
              <el-input
                class="add-text"
                v-model="editForm.depositReceivedAmount"
                placeholder="Add text"
                maxlength="10"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="hr-full"/>
        <div class="buttons">
          <el-button
            type="primary"
            class="cancel-button"
            @click="handleClickCancleButton">
            Cancel
          </el-button>
          <el-button
            style="margin-left: 20px;"
            type="primary"
            native-type="submit"
            @click="handleClickSaveButton">
            Save
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import limit from '../../http/api/limit.js'
  import fleet from '../../http/api/fleetCompany.js'
  export default {
    data() {
      return {
        loading: false,
        companyOptions: [],
        statusOptions: [],
        rule: {
          "fleetCompanyId": {
            required: true,
            trigger: 'change',
            message: 'Please select a fleet company'
          },
          "creditLimitStatus": {
            required: true,
            trigger: 'change',
            message: 'Please select a status'
          },
          "creditLimitAmount": [{
            required: true,
            trigger: 'blur',
            message: 'Please type Monthly Credit Limit'
          }, {
            pattern: /^\d+(.{0,1}\d{1,})?$/,
            trigger: 'blur',
            message: 'Please type a number of amount'
          }],
          "effectiveStartDate": {
            required: true,
            trigger: 'change',
            message: 'Please select effective start date'
          },
          "effectiveEndDate": {
            required: true,
            trigger: 'change',
            message: 'Please select effective end date'
          },
          "depositReceivedAmount": [{
            required: true,
            trigger: 'blur',
            message: 'Please type deposit received'
          }, {
            pattern: /^\d+(.{0,1}\d{1,})?$/,
            trigger: 'blur',
            message: 'Please type a number of amount'
          }],
        },
        editForm: {
          creditLimitId: 0,
          fleetCompanyId: '',
          creditLimitAmount: '',
          depositReceivedAmount: '',
          effectiveStartDate: "",
          effectiveEndDate: "",
          creditLimitStatus: ""
        },
        companyParams: {
          "pageSize": 20,
          "pageNo": 1,
          "pageVo": {
            "criteria": ""
          }
        }
      }
    },
    computed: {
      ...mapState('provider', ['creditLimit'])
    },
    created() {
      this.loading = true;
      this.getLimitInfo();
      this.getCompanyList();
    },
    methods: {
      getLimitInfo() {
        console.log('sdds', this.creditLimit);
        limit.viewCreditLimit(this.creditLimit.creditLimitId).then(res => {
          if (res.data) {
            this.editForm = res.data
          }
        }).catch(err => {
          this.loading = false;
          this.$message({
            message: err,
            type: 'error'
          })
        });
      },
      filterCompany(word) {
        this.companyParams.pageVo.criteria = word;
        this.getCompanyList();
      },
      getCompanyList() {
        fleet.fetchFleetCompanyPages(this.companyParams).then(res => {
          if (res.data) {
            this.companyOptions = res.data
          }
          this.getLimitStatus();
        }).catch(err => {
          this.loading = false;
          this.$message({
            message: err,
            type: 'error'
          })
        });
      },
      getLimitStatus() {
        limit.getLimitStatus().then(res => {
          this.loading = false;
          if (res.data) {
            this.statusOptions = res.data
          }
        }).catch(err => {
          this.loading = false;
          this.$message({
            message: err,
            type: 'error'
          })
        });
      },
      handleClickCancleButton() {
        this.$router.go(-1)
      },
      handleClickSaveButton() {
        this.$refs['editForm'].validate(result => {
          if (result) {
            this.updateLimites();
          }
        })
      },
      updateLimites() {
        this.loading = true;
        limit.updateCreditLimit(this.editForm).then(res => {
          this.loading = false;
          this.$message({
            message: 'Update credit limit successfully',
            type: 'success'
          })
          this.$router.go(-1);
        }).catch(err => {
          this.loading = false;
          this.$message({
            message: err,
            type: 'error'
          })
        });
      }
    }
  }
</script>

<style scoped="scoped" lang='scss'>
  @import '../../styles/variables.scss';
  
  .card-container {
    width: 100%;
    padding: 20px 60px;
    min-height: $mainAppMinHeight;
    background-color: #F0F5FC;
  }
  .card-content {
    padding: 15px 80px;
    border-radius: 6px;
    background-color: white;
  }
  .section-title {
    color: #333;
    margin-top: 20px;
    margin-bottom: 30px;
    font-size: 16px;
    user-select: none;
    line-height: 24px;
    font-weight: 500;
    font-family: sans-serif;
    text-transform: uppercase;
  }
  .add-text {
    width: 100%;
    max-width: 300px;
  }
  .add-text ::v-deep .el-textarea__inner {
    font-family: sans-serif;
  }
  .hr {
    height: 2px;
    margin: 10px -40px;
    background-color: #F0F5FC;
  }
  .hr-full {
    height: 2px;
    margin: 20px -80px;
    background-color: #F0F5FC;
  }
  .rate-list-view {
    display: flex;
    align-items: center;
  }
  .rate-text {
    max-width: 150px;
    padding-right: 14px;
  }
  .list-item-icon {
    width: 30px;
    height: 30px;
    cursor: pointer;
    margin: 0 10px 22px;
  }
  .buttons {
    padding-top: 15px;
    padding-bottom: 30px;
  }
  @media screen and (max-width: 500px) {
    .card-container {
      padding: 0px;
    }
    .card-content {
      padding: 15px 30px;
    }
  }
  .item-unit {
    top: 12px;
    left: -40px;
    font-size: 12px;
    user-select: none;
    position: absolute;
  }
</style>
