<template>
    <div class="app-container">
      <van-nav-bar
        title="纠纷调请"
        left-arrow
        fixed
        placeholder
        @click-left="goBack()"
        z-index="998"
      >
      </van-nav-bar>

      <div class="main" style="padding-bottom: 1rem;">
        <van-form ref="form">
          <div :class="allowCUD && formEnabled.baseFormEnabled ? '' : 'noModify'">
<!--            <p class="topTit">纠纷处理过程</p>-->
            <template> <!-- 申请 基本信息 -->
              <div>
                <p class="main_title">基本信息</p>
                <div class="main_box">
                  <van-field :readonly="!allowCUD || !formEnabled.baseFormEnabled" v-model="arbitrationData.handlerName" label="负责人" placeholder="负责人" input-align="right" required :rules="[{ required: true }]"/>
                  <field-date-picker
                    v-model="arbitrationData.handleTime"
                    label="申请时间"
                    placeholder="请选择申请时间"
                    :rules="[{ required: true }]"
                    formatter="yyyy-MM-dd"
                    required
                    :readonly="!allowCUD || !formEnabled.baseFormEnabled"
                  />

                  <field-radio
                    v-model="arbitrationData.handlerType"
                    label="处理方式"
                    value-key="dictLabel"
                    data-key="dictValue"
                    :rules="[{ required: true }]"
                    required
                    :readonly="!allowCUD || !formEnabled.baseFormEnabled"
                    :columns="options.handler_type"
                  />

                </div>
              </div>

              <div>
                <p class="main_title">处理结果</p>
                <div class="main_box">
                  <van-field
                    rows="3"
                    autosize
                    type="textarea"
                    placeholder="请输入处理结果"
                    input-align="left"
                    v-model="arbitrationData.handleResult"
                    :readonly="!allowCUD || !formEnabled.baseFormEnabled"
                    :rules="[{ required: true }]" required
                  />
                </div>
              </div>
            </template>
          </div>

        </van-form>
      </div>

      <!-- 底部按钮 -->
      <van-goods-action style="z-index: 999;" v-if="allowCUD">
        <van-goods-action-button type="primary" text="保存"  @click="onSubmit('add')" v-if="formEnabled.baseFormEnabled" />
        <van-goods-action-button type="danger" text="删除"  @click="onSubmit('del')" v-if="formEnabled.removeEnabled" />
      </van-goods-action>
    </div>
</template>

<script>

import FieldDatePicker from "@/components/form/FieldDatePicker";
import FieldRadio from "@/components/form/FieldRadio";
import {formatDate} from "element-ui/src/utils/date-util.js";
import {Dialog, Notify} from "vant";
import { addArbitrationProcess, getArbitrationProcessDetail, editArbitrationProcess, removeArbitrationProcess } from "@/api/onlineHome/homestead/arbitration";

// 意图
const INTENT_VIEW = 1;
const INTENT_EDIT = 2;
const INTENT_ADD = 3;

export default {
  name: "ArbitrationProcessDetail",
    components: {
      FieldRadio, FieldDatePicker,},
    data() {
    return {
      // 申请ID
      id: '',
      // 表单数据
      arbitrationData: {
        arbitrationId: this.$route.query.arbitrationId,
        handleResult: '',
        handleTime: '',
        handlerType: '1',
        handlerName: '',
      },
      // 表单意图
      operationIntent: INTENT_ADD,
      // 显示控制
      formVisible: {
      },
      // 表单启用控制
      formEnabled: {
        baseFormEnabled: false,
        removeEnabled: false,
      },
      currentUserRole: null,
      options: {
        handler_type: [],
      },
    };
  },
  created() {
    this.id = this.$route.query.id;
    this.type = this.$route.query.type;
    this.getFormIntent();
    this.initOptions();
    this.getDetail();
  },
  computed: {
    allowCUD: function () {
      return this.$store.getters.businessLevel == '2' || true;
    },
  },
  methods: {
    // 初始化当前数据, 有ID则查询, 否则新增
    getDetail(){
      this.reset();
      if(this.id)
      {
        getArbitrationProcessDetail(this.id).then(response => {
          this.init(response.data);
        });
      }
      else
      {
        this.init();
      }
    },
    // 全局初始化
    init(value) {
      const role = this.$store.getters.roles;
      this.currentUserRole = role[0];

      // 默认状态
      this.formEnabled.baseFormEnabled = false;
      this.formEnabled.removeEnabled = false;

      switch (this.operationIntent) {
        // 查看
        case INTENT_VIEW:
          this.arbitrationData = value;
          this.formEnabled.removeEnabled = true;
          break;

        // 编辑
        case INTENT_EDIT:
          this.arbitrationData = value;
          this.formEnabled.baseFormEnabled = true;
          this.formEnabled.removeEnabled = true;
          break;

        // 新建
        case INTENT_ADD:
          this.formEnabled.baseFormEnabled = true;
          break;
      }
    },
    // 获取query的意图
    getFormIntent() {
      console.log(this.type);
      switch (this.type) {
        case 'view':
          this.operationIntent = INTENT_VIEW;
          break;
        case 'modify':
          this.operationIntent = INTENT_EDIT;
          break;
        case 'add':
        default:
          this.operationIntent = INTENT_ADD;
          break;
      }
      return this.operationIntent;
    },
    // 获取日期, yyyy-MM-dd
    getDate(d) {
      return formatDate(d ? d : new Date(), 'yyyy-MM-dd');
    },
    // 初始化基础表单
    reset() {
      this.$set(this.arbitrationData, 'handleResult', '');
      this.$set(this.arbitrationData, 'arbitrationId', this.$route.query.arbitrationId);
      this.$set(this.arbitrationData, 'handlerName', '');
      this.$set(this.arbitrationData, 'handlerType', '1');
      this.$set(this.arbitrationData, 'handleTime', this.getDate());
    },
    //返回上一步操作
    goBack(){
      this.$router.push({name: this.$router.back(-1)});
    },
    // 全局提交
    onSubmit(intent){
      console.log(this.arbitrationData, intent);
      switch (intent) {
        case 'add':
        case 'modify':
          this.saveArbitrationProcess();
          break;
        case 'del':
          this.removeArbitrationProcess();
          break;
        default:
          console.error('Unknown intent! ', intent);
          break;
      }
    },
    // 检查字符串, 不符合返回true
    checkString(value, regexp) {
      let res = value === undefined || value === null || value === '' || value.toString().trim().length === 0;
      if(res)
        return true;
      if(regexp)
        res = !value.match(regexp);
      return res;
    },
    // 保存申请(是否提交)
    saveArbitrationProcess() {
      this.$refs.form.validate().then(() => {
        console.log("进行保存", this.arbitrationData);
        (this.arbitrationData.id ? editArbitrationProcess : addArbitrationProcess)(this.arbitrationData).then((response) => {
          this.notify("保存成功", 'success');
          this.goBack();
        }).catch((e) => {
          this.notify("保存失败!", 'danger');
        }).finally(() => {
        });
      }).catch(e => {
        this.notify('请填写完整表单', 'danger');
        return;
      });
    },
    // 请求结果提示工具函数
    notify(message, type) {
      Notify.clear();
      Notify({ type: type || 'primary', message: message });
    },
    initOptions() {
      this.arbitrationData.arbitrationId = this.$route.query.arbitrationId;
      for(let k in this.options)
      {
        this.houseGetDicts(k).then((res) => {
          this.options[k] = res.data;
        });
      }
    },
    // 删除
    removeArbitrationProcess() {
      Dialog.confirm({
        title: '警告',
        message: '确定删除?',
      })
        .then(() => {
          removeArbitrationProcess(this.arbitrationData.id).then((response) => {
            this.notify("删除成功", 'success');
            this.goBack();
          }).catch((e) => {
            this.notify("删除失败!", 'danger');
          }).finally(() => {
          });
        })
        .catch(() => {
        });
    },
  },
  watch: {
  }
}
</script>

<style scoped lang="scss">
  .app-container {
    padding-bottom: 5%;
  }
  .examine_box{
    background-color: #1D6FE9!important;
    padding: 0.18rem!important;
    padding-left: 0!important;
    border-radius: 0.15rem!important;
    margin-top: 0.3rem!important;
  }
  .examine_box .van-col:first-child{
    color: #FFF!important;
    font-size: 0.45rem!important;
    text-align: center!important;
  }
  .examine_box .van-col:last-child{
    background-color: #FFF!important;
    border-radius: 0.15rem!important;
    overflow: hidden!important;
    .van-radio-group--horizontal{
      padding: 0.2rem 0;
      border-bottom: 1px solid #eee;
    }
  }
  #mapWrap{
    width: 96%;
    margin: 0 auto;
    border-bottom-left-radius: 12px;
    border-bottom-right-radius: 12px;
    overflow: hidden;
  }
  .mapBox{
    position: relative;
    .mapBox_button{
      position: absolute;
      top: 0.2rem;
      right: 2%;
    }
  }
  .van-steps{
    padding: 2% 6% 0;
  }
  .topTit{
    font-size: 0.45rem;
    background-color: #1D6FE9;
    color: #FFFFFF;
    line-height: 58px;
    text-align: center;
    padding: 15px 0;
    box-shadow: 0px 3px 6px 0px rgba(15,67,145,0.40);
  }
  .main_title{
    font-size: 0.4rem;
    color: #1D6FE9;
    margin: 0.2rem 6%;
    position: relative;
  }
  .main_box{
    width: 96%;
    margin: 0 auto;
    border-radius: 6px;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
    overflow: hidden;
    background-color: #FFF;
  }
  .collapse{
    width: 96%;
    margin: 0 auto;
    border-radius: 6px;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
    overflow: hidden;
    margin-bottom: 15px;
  }
  /deep/.van-radio--horizontal{
    margin-left: 20px;
    margin-right: 0;
  }
  .file-box{
    padding: 2% 5% 0;
  }
  .submitButton{
    width: 80%;
    margin: 0 auto;
    border-radius: 14px;
  }
  .timeTit{
    text-align: center;
    font-size: 16px;
    line-height: 27px;
  }
  .action-box{
    padding: 15px 0!important;
    margin-top: 0.4rem;
  }
  .check-box{
    margin-top: 0.4rem;
  }
  .addFamily{
    position: absolute;
    top: -2px;
    right: 0;
    border-radius: 50%;
    display: inline-block;
    width: 0.7rem;
    height: 0.7rem;
  }
  .deleteFamily{
    position: absolute;
    top: -0.35rem;
    right: 6%;
    z-index: 9;
    border-radius: 50%;
    display: inline-block;
    width: 0.7rem;
    height: 0.7rem;
  }
  .familyList{
    margin-top: 0.4rem;
    position: relative;
  }
  .noModify{
    .topTit{
      background-color:#ABABAB ;
      box-shadow: 0px 3px 6px 0px rgba(171,171,171,0.40);
    }
    .van-cell__title{
      color: #B4B0B0;
    }
  }
  .flow_main_box{
    width: 96%;
    margin: 0 auto;
    border-radius: 6px;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
    overflow: hidden;
    background-color: #FFF;
    margin-top: 2%;
    padding: 5% 1%;
    .van-col{
      text-align: center;
    }
    .tit{
      background: #1d6fe9;
      border-radius: 12px;
      font-size: 0.4rem;
      font-family: Source Han Sans CN, Source Han Sans CN-Regular;
      font-weight: 400;
      color: #ffffff;
      line-height: 0.65rem;
      letter-spacing: 0px;
      width: 70%;
      margin: 0 auto;
    }
    .van-step--vertical{
      padding-right: 0;
      text-align: left;
    }
    .van-step--vertical:not(:last-child)::after{
      border: none;
    }
    .van-step--finish{
      color: #1d6fe9;
    }
  }
  .van-goods-action {
    justify-content: center;
  }
</style>
