<template>
  <div>
    <van-nav-bar
      left-arrow
      title="纠纷调解"
      fixed
      placeholder
      @click-left="$router.back()"
    >
      <template #right>
        <van-icon name="add" size="20" @click="addArbitration" 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.applyTime}}</p>
            </template>
            <template #title>
              <p style="font-weight: bold;">{{item.disputes}}</p>
            </template>
            <template #right-icon>
              <p :style="{'font-weight': 'bold',
                color: {
                  '1': '#000000',
                  '2': '#000000',
                  '3': '#00FF00',
                  '4': '#FF0000',
                  '5': '#00FF00',
                  '6': '#00FF00',
                  '7': '#00FF00',
                }[item.dispute_status],
              }">{{formatDict(options.dispute_status, item.disputeStatus)}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row style="height: 100%;">
              <van-col style="height: 100%;">
                <van-button v-if="allowCUD && item.disputeStatus == '1'" square text="修改" type="info" style="height: 100%;" @click="editItem(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button v-if="allowCUD && item.disputeStatus == '1'" square text="提交" type="primary" style="height: 100%;" @click="submitItem(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button v-if="allowCUD && item.disputeStatus == '1'" square text="删除" type="danger" style="height: 100%;" @click="removeItem(item)"/>
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </van-pull-refresh>

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

  </div>
</template>

<script>
import {getArbitrationList, submitArbitration, removeArbitration} from "@/api/onlineHome/homestead/arbitration";
import { formatDate } from "element-ui/src/utils/date-util.js"
import {Dialog, ImagePreview, Notify} from 'vant';
import onlineHomeIndex from "@/views/onlineHomeIndex";

export default {
  components: {onlineHomeIndex, },
  name: "ArbitrationList",
  data() {
    return {
      list: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'createTime',
        isAsc: 'desc',
        disputeStatus: null,
      },
      refreshing: false,
      loading: false,
      finished: false,
      options: {
        dispute_status: [],
      },
    }
  },
  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 = []
      }
      getArbitrationList(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;
      }).finally(() => {
        this.loading = false;
        this.refreshing = false;
      });
    },
    viewItem(item) {
      this.$router.push({name:'arbitrationDetail', query: {
        type: 'view',
          id: item.id,
        }});
    },
    editItem(item) {
      this.$router.push({name:'arbitrationDetail', query: {
          type: 'modify',
          id: item.id,
        }});
    },
    submitItem(item) {
      if(item.disputeStatus != '1')
      {
        this.notify('只有草稿才可提交', 'danger');
        return;
      }

      submitArbitration(item.id, '2').then((response) => {
        this.notify("提交成功", 'success');
        this.getList();
      }).catch((e) => {
        this.notify("提交失败!", 'danger');
      }).finally(() => {
      });
    },
    notify(message, type) {
      Notify.clear();
      Notify({ type: type || 'primary', message: message });
    },
    initOptions() {
      for(let k in this.options)
      {
        this.houseGetDicts(k).then((res) => {
          this.options[k] = res.data;
        });
      }
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    addArbitration() {
      this.$router.push({name:'arbitrationDetail', query: {
        type: 'add',
        }});
    },
    removeItem(item) {
      if(item.disputeStatus != '1')
      {
        this.notify("只允许删除草稿!", 'danger');
        return;
      }
      Dialog.confirm({
        title: '警告',
        message: '确定删除?',
      })
        .then(() => {
          removeArbitration(item.id).then((response) => {
            this.notify("删除成功", 'success');
            this.getList();
          }).catch((e) => {
            this.notify("删除失败!", 'danger');
          }).finally(() => {
          });
        })
        .catch(() => {
        });
    },
  },
}
</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>
