<template>
  <div>
<!--    <van-nav-bar-->
<!--      left-arrow-->
<!--      title="纠纷调解"-->
<!--      fixed-->
<!--      placeholder-->
<!--      @click-left="$router.back()"-->
<!--    >-->
<!--      <template #right>-->
<!--        <van-icon name="add"  @click="addArbitration" v-if="allowCUD"/>-->
<!--      </template>-->
<!--    </van-nav-bar>-->

    <div class="header_main">
      纠纷调解
      <div class="return_btn" @click="onClickLeft"></div>
      <div class="add_btn" @click="addArbitration" v-if="allowCUD"></div>
    </div>

    <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.disputeAt}}</p>
            </template>
            <template #title>
              <p style="font-weight: bold;">{{item.disputeRemark}}</p>
            </template>
            <template #right-icon>
              <p :style="{'font-weight': 'bold',
                color: {
                  '0': '#000000',
                  '1': '#00FF00',
                }[item.status],
              }">{{formatDict(options.jfcl_status, item.status)}}</p>
            </template>
          </van-cell>
          <template #right>
            <van-row style="height: 100%;">
              <van-col style="height: 100%;">
                <van-button v-if="!cljgShow && allowCUD && item.status == '0'" square text="修改" type="info" style="height: 100%;" @click="editItem(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button v-if="cljgShow && item.status == '0'" square text="处理结果" type="primary" style="height: 100%;" @click="cljgItem(item)"/>
              </van-col>
              <van-col style="height: 100%;">
                <van-button v-if="!cljgShow && allowCUD && item.status == '0'" square text="删除" type="danger" style="height: 100%;" @click="removeItem(item)"/>
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </van-pull-refresh>

    <van-dialog v-model="cljgInfoShow" title="处理结果" show-cancel-button   confirmButtonText="保存" cancelButtonText="关闭" @confirm="confirmn" >
      <van-field :readonly="!allowCUD" v-model="arbitrationData.handleBy" label="处理人" placeholder="处理人" input-align="right" required :rules="[{ required: true }]"/>
      <van-field v-model="arbitrationData.handlePhone" label="处理人电话" placeholder="处理人电话" input-align="right" type="digit" />
      <field-date-picker
        v-model="arbitrationData.handleAt"
        label="处理时间"
        placeholder="请选择处理时间"
        :rules="[{ required: true }]"
        formatter="yyyy-MM-dd"
        required
        :readonly="!allowCUD"
      />
      <van-field
        rows="3"
        autosize
        label="处理结果"
        type="textarea"
        placeholder="处理结果"
        input-align="right"
        :rules="[{ required: true }]"
        required
        v-model="arbitrationData.handleRemark"
        :readonly="!allowCUD"
      />
    </van-dialog>

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

  </div>
</template>

<script>
import {getArbitrationList, removeArbitration,editArbitration} from "@/api/sunVillage_info/homestead/arbitration";
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";
import Cookies from "js-cookie";

export default {
  components: {onlineHomeIndex,FieldDatePicker},
  name: "ArbitrationList",

  data() {
    return {
      list: [],
      total: 0,
      idcard:null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: 'createTime',
        deptId: Cookies.get('deptId'),
        isAsc: 'desc',
        disputeStatus: null,
      },
      refreshing: false,
      loading: false,
      finished: false,
      cljgShow:false,
      cljgInfoShow:false,
      arbitrationData:{},
      options: {
        jfcl_status: [],
      },
    }
  },
  created() {
    if(this.$route.query.status === "0"){
      this.$set(this.queryParams, 'status', '0');
      this.cljgShow = true;
    }else{
      this.idcard = Cookies.get('idcard');
      this.$set(this.queryParams, "shyqrzjhm", this.idcard);
    }

    this.initOptions();
    this.getList();
  },
  computed: {
    allowCUD: function () {
      return this.$route.query.status !== "0";
    },
  },
  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:'sunVillageInfoArbitrationDetail', query: {
        type: 'view',
          id: item.id,
        }});
    },
    editItem(item) {
      this.$router.push({name:'sunVillageInfoArbitrationDetail', 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;
        });
      }
    },
    confirmn(){
      this.$set(this.arbitrationData, 'status', '1');
      editArbitration(this.arbitrationData).then((response) => {
        this.notify("保存成功", 'success');
        this.cljgInfoShow = false;
        this.getList();
      }).catch((e) => {
        this.notify("保存失败!", 'danger');
        this.cljgInfoShow = false;
      }).finally(() => {
      });
    },
    formatDict(dict, value) {
      return this.selectDictLabel(dict, value);
    },
    addArbitration() {
      this.$router.push({name:'sunVillageInfoArbitrationDetail', query: {
        type: 'add',
        }});
    },
    cljgItem(item){
      this.cljgInfoShow = true;
      this.arbitrationData = item;
    },
    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 lang="scss">
  .header_main{
    height: 116px;
    background: url('../../../assets/images/sunVillage_info/list_head_green.png') no-repeat;
    background-size: 100% 100%;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    font-size: 36px;
    line-height: 116px;
    text-align: center;
    color: #fff;
    position: relative;
  .return_btn{
    width: 24px;
    height: 43.2px;
    background: url('../../../assets/images/sunVillage_info/list_icon_5.png') center center no-repeat;
    background-size: 20px 36px;
    position: absolute;
    left: 38px;
    top: 36px;
  }
  .add_btn{
    width: 56.4px;
    height: 40.8px;
    background: url('../../../assets/images/sunVillage_info/list_icon_9.png') center center no-repeat;
    background-size: 47px 34px;
    position: absolute;
    right: 38px;
    top: 36px;
  }
  }
  /deep/.van-list{
    padding: 0.2rem 3%;
  }
.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>
