<template>
  <div class="plans-content">
    <div class="button-right">
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="showAddPlan"
        v-if="canEdit">
        Individual Plan
      </el-button>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.data"
      empty-text="No individual plans assigned. Click 'Individual Plan' to add a new one.">
      <el-table-column
        align="center"
        label="Amount"
        prop="planCredit"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Date Created"
        prop="createTime"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Assigned Users"
        prop="assignedUsers"
        min-width="150">
        <template slot-scope="{row}">
          <span
            v-if="canEdit"
            class="link-type"
            @click="assignPlan(row)">{{row.assignedUsers}}</span>
          <span v-else>{{row.assignedUsers}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Suspended Users"
        prop="suspendUsers"
        min-width="170">
        <template slot-scope="{row}">
          <span
            v-if="!canEdit"
            class="link-type"
            @click="suspendPlan(row)">{{row.suspendUsers}}</span>
          <span v-else>{{row.suspendUsers}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Suspended Credit"
        prop="suspendCredit"
        min-width="170"/>
      <el-table-column
        align="center"
        label="Not Suspended Credit"
        prop="notSuspendCredit"
        min-width="170"/>
<!--      <el-table-column
        align="center"
        label="Current Consumption"
        prop="individualCurrentConsumption"
        min-width="170"/> -->
      <!-- <el-table-column
        align="center"
        label="Action"
        min-width="80"
        v-if="canEdit">
        <template slot-scope="{row, $index}">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="assignPlan">
                Assign Users
              </el-dropdown-item>
              <el-dropdown-item
                command="deletePlan"
                v-if="canDelete">
                Delete
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="right">
      <Pagination
        v-show="table.total"
        :total="table.total"
        :page.sync="filter.pageNum"
        :limit.sync="filter.pageSize"
        @pagination="getPlanList"/>
    </div>
    <el-dialog
      title="Individual Plan"
      :visible="addPlan.visible"
      :before-close="hideAddPlan"
      :close-on-click-modal="false"
      class="individual-plan-dialog">
      <div class="bold" style="padding-bottom: 15px;">Set Amount</div>
      <el-input
        class="add-text"
        v-model.number="addPlan.form.planCredit"
        maxlength="9"/>
      <div class="buttons">
        <el-button
          type="primary"
          class="cancel-button flex1"
          @click="hideAddPlan">
          Cancel
        </el-button>
        <el-button
          class="flex1"
          style="margin-left: 10px;"
          type="primary"
          :loading="addPlan.loading"
          @click="addPlanRow">
          Add
        </el-button>
      </div>
    </el-dialog>
    <assignment
      v-bind="dialogAssign"
      @hide="hideAssignDialog"/>
    <suspendment
      v-bind="dialogSuspend"
      @hide="hideSuspendDialog"/>
  </div>
</template>

<script>
import limit from '@/api/limit.js'
import Pagination from '@/components/Pagination'
import assignment from './assignment.vue'
import suspendment from './suspendment.vue'
export default {
  name: "plans",
  props: {
    id: {
      type: Number|String,
      default: ""
    },
    groupPk: {
      type: Number|String,
      default: ""
    },
    canEdit: {
      type: Boolean,
      default: true
    },
    canDelete: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      table: {
        total: 0,
        data: [],
        loading: false
      },
      filter: {
        pageNum: 1,
        pageSize: 10,
        pageCriteria: {
          criteria: "",
          groupCreditPk: ""
        }
      },
      addPlan: {
        visible: false,
        loading: false,
        form: {
          planCredit: "",
          groupCreditPk: ""
        }
      },
      dialogAssign: {
        item: {},
        groupPk: "",
        visible: false
      },
      dialogSuspend: {
        item: {},
        groupPk: "",
        visible: false
      }
    };
  },
  components: {Pagination, assignment, suspendment},
  mounted() {
    if (this.id) {
      this.addPlan.form.groupCreditPk = this.id;
      this.filter.pageCriteria.groupCreditPk =  this.id;
      this.getPlanList();
    }
  },
  methods: {
    getPlanList() {
      this.table.loading = true;
      limit.getCreditLimitPlanPages(this.filter).then(res => {
        if (res.data.totalRow && res.data.records) {
          this.table.data = res.data.records
          this.table.total = res.data.totalRow
        } else {
          this.table.data = []
          this.table.total = 0
        }
        this.table.loading = false;
      }).catch(err => {
        this.table.loading = false;
        this.table.data = [];
        this.table.total = 0;
      })
    },
    handleCommand(fuc, item) {
      this[fuc](item)
    },
    showAddPlan() {
      this.addPlan.visible = true;
    },
    hideAddPlan() {
      this.addPlan.visible = false;
      this.addPlan.form.planCredit = "";
    },
    addPlanRow() {
      this.addPlan.loading = true;
      limit.addCreditLimitPlan(this.addPlan.form).then(res => {
        this.$message.success("Added successfully.")
        this.hideAddPlan();
        this.getPlanList();
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
      }).finally(() => {
        this.addPlan.loading = false;
      });
    },
    assignPlan(item) {
      this.dialogAssign.item = item;
      this.dialogAssign.groupPk = this.groupPk;
      this.dialogAssign.visible = true;
    },
    suspendPlan(item) {
      this.dialogSuspend.item = item;
      this.dialogSuspend.groupPk = this.groupPk;
      this.dialogSuspend.visible = true;
    },
    deletePlan(item) {
      this.$confirm('Are you sure you want to delete this plan?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.handleDeletePlan(item.groupCreditPlanId)
      })
    },
    handleDeletePlan(id) {
      limit.deleteCreditLimitPlan(id).then(res => {
        this.$message({
          message: 'Delete successfully',
          type: 'success'
        });
        this.getPlanList();
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
      })
    },
    hideAssignDialog(e) {
      this.dialogAssign.item = {};
      this.dialogAssign.visible = false;
      if (e) {
        this.getPlanList();
        this.$emit("refresh");
      }
    },
    hideSuspendDialog(e) {
      this.dialogSuspend.item = {};
      this.dialogSuspend.visible = false;
      if (e) {
        this.getPlanList();
        this.$emit("refresh");
      }
    }
  }
}
</script>

<style scoped>
.plans-content {
  position: relative;
}
.button-right {
  top: -55px;
  right: 0;
  position: absolute;
}
.individual-plan-dialog {
  padding-bottom: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.individual-plan-dialog >>> .el-dialog {
  width: 100%;
  max-width: 400px;
}
.buttons {
  display: flex;
  padding-top: 35px;
  align-items: center;
}
</style>