<template>
  <div>
    <el-dialog :title="title" :visible="mediationVisable" @close="cancel" center :distroy-on-close="true" width="1000px">
      <el-form ref="form" :model="formData" label-width="180px">
        <div class="caseInfo">案件信息：</div>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件编号：" prop="caseNum">
              <el-input v-model="formData.caseNum" placeholder="请输入案件编号" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人：">
              <el-input v-model="applicant" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被申请人：" prop="respondentName">
              <el-input v-model="mediationData.respondentName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="案件标的：" prop="caseSubjectAmount">
              <el-input v-model="formData.caseSubjectAmount" placeholder="请输入案件标的" :disabled="true" />
            </el-form-item> -->
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="调解费用：" prop="feePayable">
              <el-input v-model="formData.feePayable" placeholder="请输入仲裁费用" :disabled="true" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="调解员：" prop="mediatorName">
              <el-input v-model="formData.mediatorName" placeholder="请输入" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="24">
          <el-form-item label="申请人案件证据：">
            <div v-if="applicateArr.length == 0">申请人暂未提供证据！</div>
            <div v-for="(item, index) in applicateArr" :key="index" v-if="item.annexType == 2">
              <a href="#" @click="toFile2(item.annexPath)" style="color: blue">{{ item.annexName }}</a>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调解结果:">
            <el-radio-group v-model="formData.mediaResult" :disabled="!selectRoido" @change="resultsMediation">
              <el-radio :label="1">达成调解</el-radio>
              <el-radio :label="2">未达成调解</el-radio>
              <el-radio :label="3">未达成调解但不在争议</el-radio>
              <el-radio :label="4">未达成调解但同意引入仲裁</el-radio>
              <el-radio :label="5">达成和解</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否用印申请" v-if="formData.mediaResult == 1&& mediationData.mediationMethod=='1'">
             <el-radio-group v-model="formData.sealFlag" :disabled="!selectRoido">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="被申请人案件证据：">
            <div v-if="quiltArr.length == 0">被申请人暂未提供证据！</div>
            <div v-for="(item, index) in quiltArr" :key="index">
              <a href="#" @click="toFile1(item, index)" style="color: blue">{{ item.annexName }}</a>
            </div>
          </el-form-item>
        </el-col> -->
        <el-col :span="24" v-if="recordArr.length > 0">
          <el-form-item label="庭审笔录：">
            <div v-for="(item, index) in recordArr" :key="index" style="color: blue">
              <a href="#" @click="toFile2(item.annexPath)">{{ item.annexName }}</a>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调解书：" v-if="recordArrMediate.length > 0">
            <div v-for="(item, index) in recordArrMediate" :key="index" style="color: blue">
              <a href="#" @click="toFileMediate(item.onlyOfficeFileId)">{{ item.annexName }}</a>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="mediationType">
          <el-form-item label="预约会议房间号：">
            <div>{{ formData.roomId }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="mediationType">
          <el-form-item label="调解时间：">
            <div>{{ formData.hearDate }}</div>
          </el-form-item>
        </el-col>
        <!-- v-if="!mediationType" -->
        <el-col :span="24">
          <el-form-item label="上传调解书">
            <el-upload class="upload-demo" accept=".doc,.docx,.pdf" ref="upload" :action="UploadUrl()"
              :headers="headers" :data="filedata" :on-preview="handlePreview" :on-remove="handleRemove" :limit="2"
              :on-change="beforeUpload" :on-success="handlSuccess" :file-list="fileList">
              <el-button slot="trigger" size="small" :disabled="!isSecretaryRole" type="primary">选取文件</el-button>
              <!-- <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button> -->
              <div slot="tip" class="el-upload__tip">只能上传.doc，docx，pdf文件</div>
            </el-upload>
          </el-form-item>
        </el-col>
        <!-- </el-form> -->
      </el-form>
      <el-button @click="openmeeting" type="primary" v-if="mediationType">{{ isSecretaryRole ? '发起会议' :
        '进入会议' }}</el-button>
      <el-button @click="openArbitrationresults" type="primary"
        v-if="this.recordArrMediate.length <= 0 && mediationData.mediationMethod == '1' && isSecretaryRole">确认会议结果</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="endbutton1" round>
          <span>取 消</span>
        </el-button>
        <el-button v-if="isSecretaryRole" :loading="loadingSubmit" @click="submitMediation"
          :disabled="this.recordArrMediate.length <= 0 && mediationData.mediationMethod == '1' && isSecretaryRole" class="endbutton1"
          type="primary" round>
          提 交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<!-- hearDate -->
<script>
import _ from 'lodash'
import {
  caseApplicationSelectById,
  mediation,
  confirmMeetingResult,
  secretaryRoleByUserId
} from "@/api/caseManagement/caseManagement.js";
import { getUserProfile } from "@/api/system/user";
import { reserveConferenceList } from "@/api/metting/metting.js";
import { getToken } from "@/utils/auth";
export default {
  props: ["mediationVisable", "mediationData", "queryParams","applicant"],
  data() {
    return {
      sealFlag:1,
      loanStartDate: "",
      codes: null,
      title: "线上调解",
      applicateArr: [],
      quiltArr: [],
      recordArr: [],
      recordArrMediate: [],
      roomId: null,
      startTime: null,
      mediaResult: 1,
      // isReconci:0,
      formData: {
        affiliate: {},
        mediaResult:1,
        sealFlag:1,
      },
      user: "",
      userId: null,
      flieList: [],
      mediationType: true,
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      filedata: {
        annexType: 7,
      },
      fileList: [],
      attachList: [],
      isSecretaryRole: true,
      loadingSubmit: false,
      selectRoido:true
    };
  },
  watch: {
    mediationVisable(val) {
      this.attachList = [];
      this.formData.mediaResult = 1;
      this.formData.sealFlag = 1;
      this.recordArr = [];
      this.recordArrMediate = [];
      if (this.mediationData.mediationMethod == "1") {
        this.title = "线上调解";
        this.mediationType = true;
      } else if (this.mediationData.mediationMethod == "2") {
        this.title = "线下调解";
        this.mediationType = false;
        this.selectRoido = true
      }
      if (val) {
        this.caseApplicationSelectByIdFn({
          id: this.mediationData.id
        });
        this.getUser(); 
      }
    },
    recordArrMediate(val){
      if(val.length >= 1){
        this.selectRoido = false;
      }
    }
  },
  created() { },
  methods: {
    // 根据用户id查询是否为调解员
    secretaryRoleByUserIdFn() {
      secretaryRoleByUserId({
        userId: this.userId,
        caseId: this.mediationData.id
      }).then(res => {
         this.isSecretaryRole = res.data.isSecretaryRole;
      })
    },
    /**上传地址*/
    UploadUrl() {
      return window.location.origin + "/API/common/upload";
    },
    handlePreview(file) {
    },
    handleRemove(file, fileList) {
      let updatedArray = this.attachList.filter(item => item.annexId != file.response.annexId);
      this.attachList = updatedArray;
    },
    beforeUpload(flie, fileList) {
      this.fileList = fileList;
    },
    handlSuccess(res, file) {
      this.codes = res.code
      console.log(res)
      this.attachList.push({ annexId: res.annexId,annexType:res.annexType});
      // this.$set(this.mediationData, "attachList", {annexId:res.annexId});
    },
    /** 获取案件详情信息 */
    caseApplicationSelectByIdFn(data) {
      caseApplicationSelectById(data).then(res => {
        if(res.data.mediaResult){
           this.formData = res.data;
        }else{
          this.formData = res.data;
          this.formData.mediaResult = 1;
        }
        if(res.data.sealFlag==1||res.data.sealFlag==0){
           this.formData = res.data;
        }else{
          this.formData = res.data;
          this.formData.sealFlag = 1;
        }
        this.applicateArr = res.data.caseAttachList;
        res.data.caseAttachList.forEach(item => {
          if (item.annexType == 6) {
            this.recordArr.push(item);
          } else if (item.annexType == 7) {
            this.recordArrMediate.push(item);
          }
        });
      });
    },
    /** 获取用户信息 */
    getUser() {
      getUserProfile().then(response => {
        this.user = response.data.userName;
        this.userId = response.data.userId;
        this.secretaryRoleByUserIdFn();
      });
    },
    /** 发起会议 */
    openmeeting() {
      this.mediationVisable = false
      window.open(
        // `http://localhost:8080/#/home?name=${this.user}&id=${this.formData.id}&userId=${this.userId}&roomId=${this.formData.roomId}&type=tiaojie&token=${getToken()}`
        `https://txroom.xayunmei.com/#/home?name=${this.user}&id=${this.formData.id}&userId=${this.userId}&roomId=${this.formData.roomId}&type=tiaojie&token=${getToken()}`
      );
    },
    /**提交仲裁结果 */
    openArbitrationresults() {
      this.recordArr = [];
      caseApplicationSelectById({
        id: this.mediationData.id
      }).then(res => {
        res.data.caseAttachList.forEach(item => {
          if (item.annexType == 6) {
            this.recordArr.push(item);
          }
        });
      });
      confirmMeetingResult({
        id: this.mediationData.id,
        caseFlowId: this.mediationData.caseFlowId,
        batchNumber: "",
        mediaResult: this.formData.mediaResult,
        sealFlag:this.formData.sealFlag
      }).then(res => {
        caseApplicationSelectById({
          id: this.mediationData.id
        }).then(res => {
          res.data.caseAttachList.forEach(item => {
            if (item.annexType == 7) {
              this.recordArrMediate.push(item);
            }
          });
        });
      })
      // this.selectRoido = true;
    },
    /** 庭审笔录附件 */
    toFile2(annexPath) {
      window.open(window.location.origin + "/API" + annexPath, "_black");
    },
    toFileMediate(val) {
      // window.open(window.location.origin + "/API" + annexPath, "_black");
      this.$router.push({ path: '/onlyoffice', query: {id:val,flag:1} })
    },
    /** 生成调解书 */
    mediationFn(data) {
      mediation(data).then(res => {
        this.$modal.msgSuccess("成功");
        this.$emit("cancelMediation");
        this.$emit("getList", this.queryParams);
        this.loadingSubmit = false;
      }).catch(err=>{
        this.loadingSubmit = false;
      });
    },
    cancel() {
      this.$emit("cancelMediation");
    },

    submitMediation() {
      if (this.fileList.length < 1 && !this.mediationType && this.mediationData.mediationMethod == "2") {
        this.$modal.msgError("请上传调解书");
        return
      } else {
        this.loadingSubmit = true;
      }
      let mediationVal = {}
      
      if (this.codes == 200) {
        if(this.formData.mediaResult != 1){
          this.sealFlag = 0
        }
        mediationVal = {
          id: this.mediationData.id,
          caseFlowId: this.mediationData.caseFlowId,
          attachList: this.attachList,
          mediaResult: this.formData.mediaResult,
        }
      } else {
        if(this.formData.mediaResult != 1){
          this.sealFlag = 0
        }
        mediationVal = {
          id: this.mediationData.id,
          caseFlowId: this.mediationData.caseFlowId,
          sealFlag:this.sealFlag,
          attachList: this.attachList,
          mediaResult: this.formData.mediaResult,
        }
      }
      console.log(mediationVal)
      this.mediationFn(mediationVal);
    },
    resultsMediation() {

    }
  }
};
</script>
  
<style lang="scss" scoped>
.steps {
  display: flex;
  flex-wrap: wrap;
}
::v-deep .el-input--medium .el-input__inner {
  height: 40px;
  line-height: 40px;
}
</style>