<template>
  <el-dialog
    :title="title"
    :visible="visible"
    :before-close="onHide"
    class="assign-label-dialog">
    <div class="filter-container filter-view">
      <el-select
        style="min-width: 70px; max-width: 120px;"
        clearable
        v-model="filter.pageVo.assignmentStatus"
        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.pageVo.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-label-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">
        <template v-if="isGroup">
          <el-table-column
            align="center"
            label="Name"
            prop="name"
            min-width="140"/>
          <el-table-column
            align="center"
            label="Type"
            prop="type"
            min-width="120"/>
          <el-table-column
            align="center"
            label="Country"
            prop="country"
            min-width="120"/>
          <el-table-column
            align="center"
            label="Approved"
            prop="approved"
            min-width="100"/>
          <el-table-column
            align="center"
            label="Pending"
            prop="pending"
            min-width="100"/>
        </template>
        <template v-else>
          <el-table-column
            align="center"
            label="Site Name"
            prop="siteName"
            min-width="120"/>
          <el-table-column
            align="center"
            label="Address"
            prop="address"
            min-width="120"/>
          <el-table-column
            align="center"
            label="Service Provider"
            min-width="140">
            <template slot-scope="{row}">
              <div v-for="item in row.serviceProviders" :key="item">{{item}}</div>
            </template>
          </el-table-column>
        </template>
        <el-table-column
          align="center"
          label="Assignment Status"
          prop="assignmentStatus"
          min-width="150"/>
        <el-table-column
          align="center"
          label="Select"
          type="selection"
          width="50"
          v-if="visible"/>
      </el-table>
    </div>
    <div class="center" style="margin-bottom: -20px;">
      <Pagination
        v-show="table.total"
        :total="table.total"
        :page.sync="filter.pageNo"
        :limit.sync="filter.pageSize"
        @pagination="getTableData"/>
    </div>
  </el-dialog>
</template>

<script>
import api from '../../http/api/financial.js'
import Pagination from '@/components/Pagination'
export default {
  name: "AssignmentSiteSettle",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    isGroup: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      filter: {
        pageSize: 10,
        pageNo: 1,
        pageVo: {
          criteria: "",
          entityId: "",
          countryCode: "",
          assignmentStatus: ""
        }
      },
      table: {
        data: [],
        total: 0,
        loading: false
      },
      loading: {
        assign: false,
        unassign: false
      },
      statusOptions: [],
      selectRow: []
    };
  },
  components: {Pagination},
  computed: {
    title() {
      return this.isGroup ? "ASSIGN GROUPS" : "ASSIGN SITES";
    }
  },
  watch: {
    visible: {
      handler(n, o) {
        console.log("watch.visible", n, this.item);
        if (n) {
          this.filter.pageVo.entityId = this.item.entityId;
          this.filter.pageVo.countryCode = this.item.entityCountryCode;
          this.filter.pageVo.assignmentStatus = "";
          this.onSearch()
        }
      }
    }
  },
  mounted() {
    this.getStatusOptions();
  },
  methods: {
    onHide() {
      this.$emit("hide", true);
    },
    onSearch() {
      this.filter.pageNo = 1;
      this.getTableData();
    },
    getStatusOptions() {
      api.getAssignStatusOptions().then(res => {
        if (res.data) {
          this.statusOptions = res.data
        }
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      })
    },
    getTableData() {
      const promise = this.isGroup
        ? api.getAssignGroupPages(this.filter)
        : api.getAssignSitePages(this.filter)
      promise.then(res => {
        if (res.total && res.data) {
          this.table.total = res.total;
          this.table.data = res.data;
        } 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 => {
        if (item.sitePk) {
          ids.push(item.sitePk)
        } else {
          ids.push(item.id)
        }
      })
      return ids;
    },
    onClickAssign() {
      const params = {
        entityId: this.item.entityId,
        eligibleRefIds: this.getSelectIds()
      }
      this.loading.assign = true;
      api.assignEligible(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;
      })
    },
    onClickUnassign() {
      const params = {
        entityId: this.item.entityId,
        eligibleRefIds: this.getSelectIds()
      }
      this.loading.unassign = true;
      api.unassignEligible(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 lang="scss" scoped>
.assign-label-dialog
  ::v-deep .el-dialog {
    width: 65vw;
    height: 90vh;
    display: flex;
    max-width: 1200px;
    flex-direction: column;
    margin-top: 5vh !important;
  .el-dialog__header {
    padding: 20px 20px 0;
    font-weight: bold;
  }
  .el-dialog__body {
    flex: 1;
    padding: 20px;
    display: flex;
    overflow: hidden;
    flex-direction: column;
  }
  @media screen and (max-width: 1200px) {
    & {
      width: 70vw;
    }
  }
  @media screen and (max-width: 1000px) {
    & {
      width: 80vw;
    }
  }
  @media screen and (max-width: 800px) {
    & {
      width: 90vw;
    }
  }
  @media screen and (max-width: 700px) {
    & {
      width: 99vw;
    }
  }
  @media screen and (max-width: 320px) {
    & {
      width: 100%;
      min-width: 300px;
    }
  }
}
.assign-label-dialog .table-view {
  flex: 1;
  overflow-y: auto;
  padding-top: 10px;
  margin-bottom: -10px;
}
.assign-label-actions {
  display: flex;
  padding-top: 5px;
  flex-wrap: wrap-reverse;
  align-items: center;
  justify-content: flex-end;
}
</style>