<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :before-close="onHide"
    custom-class="assign-dialog">
    <div class="filter-container filter-view">
      <el-select
        style="min-width: 70px; max-width: 120px;"
        clearable
        v-model="filter.pageCriteria.status"
        placeholder="Status"
        @change="onSearch">
        <el-option
          v-for="(item, index) in statusOptions"
          :key="index"
          :label="item"
          :value="item"/>
      </el-select>
      <div style="flex: 1; min-width: 150px; max-width: 300px;">
        <el-input
          clearable
          v-model="filter.pageCriteria.criteria"
          placeholder="Search by Site Name or Service Provider"
          @keyup.enter.native="onSearch"/>
      </div>
      <el-button
        type="primary"
        @click="onSearch">
        Search
      </el-button>
    </div>
    <div class="assign-table-actions">
      <el-button
        type="danger"
        :disabled="selectRow.length == 0"
        :loading="loading.unassign"
        @click="onClickUnassign">
        Batch Un-assign
      </el-button>
      <el-button
        type="accent"
        :disabled="selectRow.length == 0"
        :loading="loading.assign"
        @click="onClickAssign">
        Batch Assign
      </el-button>
    </div>
    <div class="table-view" v-loading="table.loading">
      <el-table
        :data="table.data"
        height="100%"
        class="no-border"
        @selection-change="changeSelection">
        <el-table-column
          align="center"
          label="Location"
          prop="name"
          min-width="120"/>
        <el-table-column
          align="center"
          label="Address"
          prop="address"
          min-width="150"/>
        <el-table-column
          align="center"
          label="CHARGING STATION"
          prop="stations"
          min-width="120"/>
        <el-table-column
          align="center"
          label="Assignment Status"
          prop="status"
          min-width="120"/>
        <el-table-column
          align="center"
          label="Select"
          type="selection"/>
      </el-table>
    </div>
    <div class="center" style="margin-bottom: -20px;">
      <Pagination
        v-show="table.total"
        :total="table.total"
        :page.sync="filter.pageNum"
        :limit.sync="filter.pageSize"
        @pagination="getTableData"/>
    </div>
  </el-dialog>
</template>

<script>
import ocpi from '@/http/api/ocpi'
import apiBase from '../../../api/apiBase.js'
import Pagination from '@/components/Pagination'
export default {
  name: "DialogAssignment",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: "ASSIGN SITES"
    },
    isOutBound: {
      type: Boolean,
      default: false
    },
    operator: {
      type: String|Number,
      default: 0
    },
  },
  components: {Pagination},
  data() {
    return {
      filter: {
        pageSize: 10,
        pageNum: 1,
        pageCriteria: {
          status: "",
          criteria: "",
          credentialId: ""
        }
      },
      table: {
        data: [],
        total: 0,
        loading: false
      },
      loading: {
        assign: false,
        unassign: false
      },
      statusOptions: [],
      selectRow: []
    };
  },
  watch: {
    visible: {
      handler(n, o) {
        console.log("watch.visible", n, o);
        if (n) {
          if (this.operator) {
            this.filter.pageCriteria.criteria = "";
            this.filter.pageCriteria.status = "";
            this.filter.pageCriteria.credentialId = this.operator;
            this.onSearch()
          }
        }
      }
    }
  },
  mounted() {
    this.getStatusOptions();
  },
  methods: {
    onHide() {
      this.$emit("hide");
    },
    onSearch() {
      this.filter.pageNum = 1;
      this.getTableData();
    },
    getStatusOptions() {
      apiBase.getAssignStatusOptions().then(res => {
        if (res.data) {
          this.statusOptions = res.data
        }
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      })
    },
    getTableData() {
      this.selectRow = []
      this.table.loading = true;
      const promise = this.isOutBound 
        ? ocpi.pagesSharePartyLocations(this.filter)
        : ocpi.pagesPartyShareLocations(this.filter)
      promise.then(res => {
        if (res.data.records && res.data.totalRow) {
          this.table.total = res.data.totalRow;
          this.table.data = res.data.records;
        } else {
          this.table.total = 0;
          this.table.data = [];
        }
        this.table.loading = false;
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
        this.table.total = 0;
        this.table.data = [];
        this.table.loading = false;
      })
    },
    changeSelection(val) {
      this.selectRow = val;
    },
    getSelectIds() {
      const ids = [];
      this.selectRow.forEach(item => {
        ids.push(item.locationId)
      })
      return ids;
    },
    onClickAssign() {
      const params = {
        credentialId: this.operator,
        locationIds: this.getSelectIds()
      }
      if (this.isOutBound) {
        this.assignShareParty(params);
      } else {
        this.assignPartyShare(params);
      }
    },
    onClickUnassign() {
      const params = {
        credentialId: this.operator,
        locationIds: this.getSelectIds()
      }
      if (this.isOutBound) {
        this.unassignShareParty(params);
      } else {
        this.unassignPartyShare(params);
      }
    },
    assignShareParty(params) {
      this.loading.assign = true;
      ocpi.assignSharePartyLocations(params).then(res => {
        this.$message({
          type: 'success',
          message: res.msg || "Success"
        })
        this.getTableData()
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      }).finally(() => {
        this.loading.assign = false;
      })
    },
    assignPartyShare(params) {
      this.loading.assign = true;
      ocpi.assignPartyShareLocations(params).then(res => {
        this.$message({
          type: 'success',
          message: res.msg || "Success"
        })
        this.getTableData()
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      }).finally(() => {
        this.loading.assign = false;
      })
    },
    unassignShareParty(params) {
      this.loading.unassign = true;
      ocpi.unassignSharePartyLocations(params).then(res => {
        this.$message({
          type: 'success',
          message: res.msg || "Success"
        })
        this.getTableData()
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      }).finally(() => {
        this.loading.unassign = false;
      })
    },
    unassignPartyShare(params) {
      ocpi.unassignPartyShareLocations(params).then(res => {
        this.$message({
          type: 'success',
          message: res.msg || "Success"
        })
        this.getTableData()
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      }).finally(() => {
        this.loading.unassign = false;
      })
    }
  }
}
</script>

<style scoped>
>>> .assign-dialog {
  width: 65vw;
  height: 90vh;
  display: flex;
  max-width: 1200px;
  flex-direction: column;
  margin-top: 5vh !important;
}
>>> .assign-dialog .el-dialog__header {
  padding: 20px 20px 0;
  font-weight: bold;
}
>>> .assign-dialog .el-dialog__body {
  flex: 1;
  padding: 20px;
  display: flex;
  overflow: hidden;
  flex-direction: column;
}
.assign-table-actions {
  display: flex;
  padding-top: 5px;
  flex-wrap: wrap-reverse;
  align-items: center;
  justify-content: flex-end;
}
.assign-dialog .table-view {
  flex: 1;
  overflow-y: auto;
  padding-top: 10px;
  margin-bottom: -10px;
}
@media screen and (max-width: 1200px) {
  >>> .assign-dialog {
    width: 70vw;
  }
}
@media screen and (max-width: 1000px) {
  >>> .assign-dialog {
    width: 80vw;
  }
}
@media screen and (max-width: 800px) {
  >>> .assign-dialog {
    width: 90vw;
  }
}
@media screen and (max-width: 700px) {
  >>> .assign-dialog {
    width: 99vw;
  }
}
@media screen and (max-width: 320px) {
  >>> .assign-dialog {
    width: 100%;
    min-width: 300px;
  }
}
</style>