<template>
  <div class="card-container">
    <div class="card-content">
      <el-form
        :model="addForm"
        :rules="rule"
        ref="addForm"
        v-loading="loading"
        label-position="left"
        label-width="150px"
        style="width: 100%;">
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <div class="section-title">Service Provider</div>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Provider Name:"
              prop="providerName">
              <el-input
                class="add-text"
                maxlength="30"
                v-model="addForm.providerName"
                placeholder="Add text"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Contact Person:">
              <el-input
                class="add-text"
                maxlength="50"
                v-model="addForm.providerAddress"
                placeholder="Add text"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Login ID:">
              <el-input
                class="add-text"
                maxlength="50"
                v-model="addForm.loginId"
                placeholder="Add text"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Password Set:">
              <el-input
                class="add-text"
                maxlength="50"
                v-model="addForm.password"
                type="password"
                placeholder="Add text"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Contact Number:">
              <el-input
                class="add-text"
                maxlength="15"
                v-model="addForm.providerContact"
                placeholder="Add text"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :xs="24" :md="18">
            <el-form-item
              label="Provider Logo:">
              <span class="logo-span" v-if="logos.length > 0"></span>
              <el-upload
                class="logo-upload"
                action
                :limit="1"
                :file-list="logos"
                list-type="picture-card"
                :on-remove="file => removeLogo(file)"
                :http-request="file => uploadLogo(file)">
                <i class="el-icon-plus avatar-uploader-icon" />
              </el-upload>
            </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="handleClickConfigUpateButton">
            Save
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {baseURL} from '../../http/http'
  import provider from '../../http/api/provider'
  export default {
    data() {
      return {
        loading: false,
        rule: {
          providerName: {
            required: true,
            trigger: 'blur',
            message: 'Please type provider name'
          }
        },
        addForm: {
          providerName: '',
          providerAddress: '',
          providerContact: '',
          providerLogo: '',
          loginId: '',
          password: ""
        },
        logos: [],
        imgAddress: baseURL
      }
    },
    created() {
      
    },
    methods: {
      uploadLogo(file) {
        const formData = new FormData()
        formData.append('file', file.file)
        provider.uploadLogo(formData).then(res => {
          console.log("upload", res);
          this.logos.push({
            path: res.data.picturePath,
            url: this.imgAddress + res.data.picturePath
          })
        })
      },
      removeLogo(file) {
        this.logos = []
      },
      handleClickConfigUpateButton() {
        this.$refs['addForm'].validate(result => {
          if (result) {
            this.addProvider();
          }
        })
      },
      addProvider() {
        this.loading = true;
        if (this.logos.length > 0) {
          const url = this.logos[0].path
          if (url) this.addForm.providerLogo = url
        }
        provider.addServiceProvider(this.addForm).then(res => {
          this.$message({
            message: 'Add service provider successfully',
            type: 'success'
          })
          this.handleClickCancleButton();
        }).catch(err => {
          this.loading = false;
          this.$message({
            message: err,
            type: 'error'
          })
        });
      },
      handleClickCancleButton() {
        this.$router.push({
          path: "/service-provider-management/index"
        })
      }
    }
  }
</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: 400px;
  }
  .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;
    flex-wrap: wrap;
    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 40px;
    }
  }
  
  .logo-upload {
    height: 148px;
    position: relative;
    background: white;
  }
  
  .logo-upload ::v-deep .el-upload {
    top: 0;
    left: 0;
    position: absolute;
    background-color: rgba(0,0,0,0);
  }
  
  .logo-span + .logo-upload ::v-deep .el-upload {
    display: none;
  }
</style>
