<template>
  <el-dialog
    :visible="visible"
    title="GENERATE"
    :before-close="onHide">
    <el-form
      ref="dialogForm"
      :model="filter"
      :rules="rules"
      class="filter-container"
      label-position="right"
      label-width="160px">
      <el-form-item
        label="Select Account:"
        prop="entityId">
        <el-select
          v-model="filter.entityId"
          class="dialog-form-item"
          filterable
          remote
          clearable
          :remote-method="getAccountOptions">
          <el-option
            v-for="(item,index) in accountOptions"
            :key="index"
            :label="item.entityName"
            :value="item.entityId"/>
        </el-select>
      </el-form-item>
      <el-form-item
        label="Select Date:"
        prop="dateRange">
        <el-date-picker
          v-model="filter.dateRange"
          class="dialog-form-item"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="Start Date"
          end-placeholder="End Date"
          clearable/>
      </el-form-item>
      <div class="dialog-button">
        <el-button
          type="cancel"
          @click="onHide">
          CANCEL
        </el-button>
        <el-button
          type="primary"
          :loading="btnLoading"
          @click="onSubmit">
          SUBMIT
        </el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import financial from '@/http/api/financial';
export default {
  name: "DialogManualGenerate",
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      filter: {
        entityId: "",
        startDate: "",
        endDate: "",
        dateRange: []
      },
      rules: {
        entityId: {
          required: true,
          trigger: "change",
          message: "Please select account"
        },
        dateRange: {
          required: true,
          trigger: "change",
          message: "Please select date"
        }
      },
      accountOptions: [],
      btnLoading: false
    };
  },
  watch: {
    visible: {
      handler(n, o) {
        if (n) {
          this.filter.entityId = "";
          this.filter.dateRange = [];
          this.filter.startDate = "";
          this.filter.endDate = "";
          this.$refs.dialogForm.clearValidate();
        }
      }
    }
  },
  mounted() {
    this.getAccountOptions();
  },
  methods: {
    onHide(e, a) {
      this.$emit("hide", a)
    },
    getAccountOptions(search) {
      financial.getBillingAccountOptions({
        entityName: search
      }).then(res => {
        if (res.data) {
          this.accountOptions = res.data
        } else {
          this.accountOptions = []
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.accountOptions = []
      })
    },
    onSubmit() {
      this.$refs.dialogForm.validate(result => {
        if (result) {
          if (this.filter.dateRange.length == 2) {
            this.filter.startDate = this.filter.dateRange[0]
            this.filter.endDate = this.filter.dateRange[1]
          }
          this.onGenerate();
        }
      })
    },
    onGenerate() {
      this.btnLoading = true;
      financial.manualGenerationSettlement(this.filter).then(res => {
        this.$message({
          type: 'success',
          message: res.msg || "Generate success"
        })
        this.onHide(false, true)
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.btnLoading = false;
      })
    }
  }
}
</script>

<style scoped>
.dialog-form-item {
  width: 100%;
  max-width: 350px;
}
.dialog-button {
  display: flex;
  align-items: center;
  padding-top: 30px;
  justify-content: center;
}
</style>