<template>
  <div>
    <van-nav-bar
      left-arrow
      title="纠纷处理"
      fixed
      placeholder
      @click-left="$router.back()"
    >
      <template #right>
        <van-icon name="add" size="20" @click="addArbitrationProcess" v-if="allowCUD"/>
      </template>
    </van-nav-bar>

    <van-pull-refresh v-model="refreshing" @refresh="getList()">
      <van-list
        v-model="loading"
        :finished="finished"
        :immediate-check="false"
        finished-text="没有更多了"
        @load="getList('+1')"
      >
        <van-swipe-cell v-for="(item,index) in list" :key="index" class="delegate">
          <van-cell :title="item.disputes" center @click="viewItem(item)">
            <template #label>
              <p style="font-weight: bold;">{{item.handleTime}}</p>
            </template>
            <template #title>
              <p style="font-weight: bold;">{{item.handlerName}}</p>
            </template>
            <template #right-icon>
              <p :style="{'font-weight': 'bold',
                color: {
                  '1': '#00FF00',
                  '2': '#FF0000',
                }[item.handlerType],
              }">{{formatDict(options.handler_type, item.handlerType)}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row style="height: 100%;">
              <van-col style="height: 100%;">
                <van-button square text="编辑" type="info" style="height: 100%;" @click="editItem(item)" v-if="allowCUD"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button square text="删除" type="danger" style="height: 100%;" @click="removeItem(item)" v-if="allowCUD"/>
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </van-pull-refresh>

<!--    <onlineHomeIndex :current="1"></onlineHomeIndex>-->

  </div>
</template>

<script>
import {getArbitrationProcessList, removeArbitrationProcess} from "@/api/onlineHome/homestead/arbitration";
import FieldSelect from "@/components/form/FieldSelect";
import { formatDate } from "element-ui/src/utils/date-util.js"
import {Dialog, ImagePreview, Notify} from 'vant';
import onlineHomeIndex from "@/views/onlineHomeIndex";
import FieldDatePicker from "@/components/form/FieldDatePicker";

export default {
  components: {FieldSelect, onlineHomeIndex, FieldDatePicker},
  name: "ArbitrationProcessList",
  data() {
    return {
      list: [],
      total: 0,
      queryParams: {
        arbitrationId: null,
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'createTime',
        isAsc: 'desc',
      },
      refreshing: false,
      loading: false,
      finished: false,
      options: {
        handler_type: [],
      },
    }
  },
  created() {
    this.initOptions();
    this.getList();
  },
  computed: {
    allowCUD: function () {
      return this.$store.getters.businessLevel == '2' || true;
    },
  },
  methods: {
    getList(target) {
      let type = typeof (target);
      console.log(type, target);
      if (target === 0) {
        this.refreshing = true;
        this.finished = true;
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.list = [];
        this.filterVisible = false;
      }
      else if (type === 'number')
        this.queryParams.pageNum = target;
      else if (type === 'string') {
        this.queryParams.pageNum = eval(this.queryParams.pageNum + target)
      }
      else
      {
        this.refreshing = true;
        this.finished = true;
        this.total = 0;
        this.queryParams.pageNum = 1;
        this.list = []
      }
      getArbitrationProcessList(this.queryParams).then((response) => {
        console.log(response)
        if (response.rows.length === 0) {
          this.finished = true;
          return;
        }
        response.rows.forEach((e) => {
          this.list.push(e);
        });
        this.total += response.rows.length;
        this.finished = this.total >= response.total;
      }).catch(() => {
        this.finished = true;
      }).finally(() => {
        this.loading = false;
        this.refreshing = false;
      });
    },
    viewItem(item) {
      this.$router.push({name:'arbitrationProcessDetail', query: {
        type: 'view',
          id: item.id,
        }});
    },
    editItem(item) {
      this.$router.push({name:'arbitrationProcessDetail', query: {
          type: 'modify',
          id: item.id,
        }});
    },
    removeItem(item) {
      Dialog.confirm({
        title: '警告',
        message: '确定删除?',
      })
        .then(() => {
          removeArbitrationProcess(item.id).then((response) => {
            this.notify("删除成功", 'success');
            this.getList();
          }).catch((e) => {
            this.notify("删除失败!", 'danger');
          }).finally(() => {
          });
        })
        .catch(() => {
        });
    },
    notify(message, type) {
      Notify.clear();
      Notify({ type: type || 'primary', message: message });
    },
    initOptions() {
      this.queryParams.arbitrationId = this.$route.query.arbitrationId;
      for(let k in this.options)
      {
        this.houseGetDicts(k).then((res) => {
          this.options[k] = res.data;
        });
      }
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    addArbitrationProcess() {
      this.$router.push({name:'arbitrationProcessDetail', query: {
          arbitrationId: this.queryParams.arbitrationId,
        }});
    },
  },
}
</script>

<style scoped>
.delegate {
  width: 96%;
  margin: 3% 2% 3% 2%;
  border-radius: 0.18rem;
  overflow: hidden;
  box-shadow: 0.1rem 0.1rem 0.15rem 0.02rem rgba(0,0,0,0.16);
}
</style>
