<template>
  <div>
    <el-dialog :title="title" :visible="openDialogInfor" width="1000px" @close="cancel" :destroy-on-close="true">
      <el-descriptions title="案件信息" border style="margin-bottom: 30px">
        <el-descriptions-item label="案件编号">{{
          form.caseNum
        }}</el-descriptions-item>
        <el-descriptions-item label="案件标的">{{
          form.caseSubjectAmount
        }}</el-descriptions-item>
        <el-descriptions-item label="借款开始日期">{{
          form.loanStartDate
        }}</el-descriptions-item>
        <el-descriptions-item label="借款结束日期">{{
          form.loanEndDate
        }}</el-descriptions-item>
        <el-descriptions-item label="合同编号">{{
          form.contractNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="申请人主张欠本金">{{
          form.claimPrinciOwed
        }}</el-descriptions-item>
        <el-descriptions-item label="申请人主张欠利息">{{
          form.claimInterestOwed
        }}</el-descriptions-item>
        <el-descriptions-item label="申请人主张违约金">{{
          form.claimLiquidDamag
        }}</el-descriptions-item>
        <el-descriptions-item label="申请人案件证据资料">
          <p @click="toFile(index, 2)" v-for="(item, index) in applicantFileArr" :key="index">
            <a href="#">{{ item }}</a>
          </p>
        </el-descriptions-item>
        <el-descriptions-item label="被申请人案件证据资料">
          <p v-for="(item, index) in respondentFileArr" :key="index" @click="toFile(index, 6)">
            <a href="#">{{ item }}</a>
          </p>
        </el-descriptions-item>
        <el-descriptions-item label="裁决书附件">
          <p v-for="(item, index) in awardFileArr" :key="index" @click="toFile(index, 3)">
            <a href="#">{{ item }}</a>
          </p>
        </el-descriptions-item>
        <el-descriptions-item label="庭审笔录附件">
          <p v-for="(item, index) in bookFileArr" :key="index" @click="toFile(index, 7)">
            <a href="#">{{ item }}</a>
          </p>
        </el-descriptions-item>
      </el-descriptions>
      <el-form ref="form" :model="arbitrateRecord" label-width="300px" label-position="left" :rules="rules"
        :disabled="flag == 3">
        <!-- <el-form-item label="经庭审质证，对各方提供的证据认定如下" prop="evidenDetermi">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.evidenDetermi" />
        </el-form-item>
        <el-form-item label="仲裁庭经审理查明(写明仲裁庭认定的事实)" prop="factDetermi">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.factDetermi" />
        </el-form-item>
        <el-form-item label="综上所述" prop="caseSketch">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.caseSketch" />
        </el-form-item>
        <el-form-item label="本庭认为" prop="arbitrateThink">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.arbitrateThink" />
        </el-form-item>
        <el-form-item label="裁决如下" prop="rulingFollows">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.rulingFollows" />
        </el-form-item>
        <el-form-item label="核验裁决书意见：" prop="verificaOpinion">
          <el-input type="textarea" autosize :disabled="flag == 2" placeholder="请输入内容"
            v-model="arbitrateRecord.verificaOpinion" />
        </el-form-item> -->
        <!-- <el-form-item label="笔录内容：" prop="responCrossOpin">
          <el-input type="textarea" :autosize="{ minRows: 6 }" v-model="arbitrateRecordt.responCrossOpin"
            placeholder="请输入" />
        </el-form-item> -->
         <el-col :span="24">
          <el-form-item label="上传裁决书:" label-width="100px" v-if="flag==1|| flag==4">
            <el-upload
              class="upload-demo"
              accept=".doc,.docx"
              ref="upload"
              :action="UploadUrls()"
              :headers="headers"
              :data="filedatas"
              :before-remove="beforeRemoves"
              :limit="2"
              :on-change="beforeUpload"
              :on-success="handlSuccessAw"
              :file-list="fileListAw"
              :before-upload="beforeAvatarUploads"
            >
              <el-button slot="trigger" size="small" type="primary"
                >选取文件</el-button
              >
              <div slot="tip" class="el-upload__tip">
                只能上传.doc，docx文件
              </div>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-form-item label="驳回原因：" label-width="100px"  v-if="flag==1 && awardDetailform.arbitrateReject">
          <div style="width:800px;">{{awardDetailform.arbitrateReject}}</div>
        </el-form-item>
        <el-form-item label="驳回原因：" label-width="100px"  v-if="flag==4 && awardDetailform.deptorReject">
          <div style="width:800px;">{{awardDetailform.deptorReject}}</div>
        </el-form-item>
        <el-form-item label-width="100px" label="审核意见：" prop="checkOpinion" v-if="flag == 2 || flag == 4">
          <el-input type="textarea" autosize placeholder="请输入内容" v-model.trim="arbitrateRecord.checkOpinion" />
        </el-form-item>
        <!-- 判断是否驳回 -->
        <el-form-item label-width="100px" label="是否驳回：" v-if="flag == 2 || flag == 4">
          <el-radio-group v-model="agreeOrNotCheck">
            <el-radio :label="1">同意</el-radio>
            <el-radio :label="2">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label-width="100px" label="驳回原因" v-if="agreeOrNotCheck==2 && flag == 4" prop="arbitrateReject"
          :rules="[
            { required: true, message: '请输入驳回原因', trigger: 'blur' }
          ]">
          <el-input type="textarea" :rows="2" placeholder="请输入驳回原因" v-model.trim="arbitrateRecord.arbitrateReject"></el-input>
        </el-form-item>
        <el-form-item label-width="100px" label="驳回原因" v-if="agreeOrNotCheck==2 && flag ==2" prop="deptorReject"
          :rules="[
            { required: true, message: '请输入驳回原因', trigger: 'blur' }
          ]">
          <el-input type="textarea" :rows="2" placeholder="请输入驳回原因" v-model.trim="arbitrateRecord.deptorReject"></el-input>
        </el-form-item>
      </el-form>
      <!-- 画布 -->
      <!-- <h4 v-if="flag == 3">签名区域</h4>
      <div class="esign" v-if="flag == 3">
        <vue-esign ref="esign" :width="900" :height="300" bgColor="#ffffff" />
      </div> -->
      <!-- 清除画布 -->
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="danger" @click="submitForm(4)" v-if="flag == 1">重新生成裁决书</el-button> -->
        <el-button type="primary" @click="submitForm(0)" v-if="flag == 1" :loading="buttonFlag">提 交</el-button>
        <el-button type="primary" @click="submitForm(1)" v-if="flag == 2" :loading="buttonFlag">提交</el-button>
        <!-- <el-button type="danger" @click="submitForm(2)" v-if="flag == 2">驳 回</el-button> -->
        <!-- <el-button type="primary" @click="submitForm(3)" v-if="flag == 3">提 交</el-button> -->
        <!-- <el-button type="primary" @click="submitForm(5)" v-if="flag == 4">同 意</el-button> -->
        <el-button type="primary" @click="submitForm(6)" v-if="flag == 4" :loading="buttonFlag">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  verificationArbitrateRecord,
  checkArbitrateRecord,
  adjudicationSignature,
  regenerationDocument,
  arbitratorCheckArbitrateRecord
} from "@/api/awardManagement/awardManagement";
import { getToken } from "@/utils/auth";
export default {
  props: ["openDialogInfor", "title", "flag", "awardDetailform","queryParams"],
  data() {
    return {
      buttonFlag:false,
      // key: value
      arbitrateRecordt: {},
      agreeOrNotCheck:1,
      form: {},
      headers: {
        Authorization: "Bearer " + getToken(),
      },
       filedatas: {
        annexType: 3,
        id:null
      },
      fileListAw:[],
      arbitrateRecord: {
        // checkOpinion:"同意"
        checkOpinion: ""
      },
      // 表单校验
      rules: {
        evidenDetermi: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        factDetermi: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        caseSketch: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        arbitrateThink: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        rulingFollows: [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        verificaOpinion: [
          {
            required: true,
            message: "意见不能为空",
            trigger: "blur",
          },
        ],
        checkOpinion: [
          {
            required: true,
            message: "意见不能为空",
            trigger: "blur",
          }
        ],
        // arbitrateReject: [
        //   {
        //     required: true,
        //     message: "拒绝原因不能为空",
        //     trigger: "blur",
        //   },
        // ],
      },
      applicantFileArr: [], //申请人
      applicantPathArr: [], //申请人
      respondentFileArr: [], //被申请人
      respondenPathArr: [], //被申请人
      awardFileArr: [], //庭审笔录文件
      awardPathArr: [], //庭审笔录路径
      bookFileArr: [], //裁决书文件
      bookPathArr: [], //庭审笔录路径
    };
  },
  watch: {
    openDialogInfor:{
        handler(val) {
        if (val) {
          this.agreeOrNotCheck = 1;
        }
      },
    },
    awardDetailform: {
      handler(val) {
        if (val) {
          (this.applicantFileArr = []), //申请人
            (this.applicantPathArr = []), //申请人
            (this.respondentFileArr = []), //被申请人
            (this.respondenPathArr = []), //被申请人
            (this.awardFileArr = []), //裁决书
            (this.awardPathArr = []), //裁决书
            // setTimeout(() => {
            this.form = this.awardDetailform;
            console.log(this.form)
          // if(this.form.arbitrateRecord && this.form.arbitrateRecord.checkOpinion == null ){
          // this.form.arbitrateRecord.checkOpinion = '同意';
          this.arbitrateRecord.checkOpinion = '';
          // }
          this.arbitrateRecord = {};
          if (this.form.arbitrateRecord) {
            this.arbitrateRecord.id = this.form.arbitrateRecord.id;
          }
          this.evidenceFile(this.form);
          // }, 1000);
        }
      },
    },
  },
  methods: {
    // 申请人裁决书
    evidenceFile(val) {
      if (val) {
        this.applicantFileArr = [];
        this.applicantPathArr = [];
        this.respondentFileArr = [];
        this.respondenPathArr = [];
        this.awardFileArr = [];
        this.awardPathArr = [];
        this.bookFileArr = [];
        this.bookPathArr = [];
        val.caseAttachList.forEach((item) => {
          if (item.annexType == 2) {
            this.applicantFileArr.push(item.annexName);
            this.applicantPathArr.push(item.annexPath);
          } else if (item.annexType == 6) {
            this.respondentFileArr.push(item.annexName);
            this.respondenPathArr.push(item.annexPath);
          } else if (item.annexType == 3) {
            this.awardFileArr.push(item.annexName);
            this.awardPathArr.push(item.annexPath);
          } else if (item.annexType == 7) {
            this.bookFileArr.push(item.annexName);
            this.bookPathArr.push(item.annexPath);
          }
        });
      }
    },
    // 预览文件
    toFile(index, val) {
      let headPath = window.location.origin + "/API";
      if (val == 2) {
        window.open(headPath + this.applicantPathArr[index], "_blank");
      } else if (val == 6) {
        window.open(headPath + this.respondenPathArr[index], "_blank");
      } else if (val == 3) {
        window.open(headPath + this.awardPathArr[index], "_blank");
      } else if (val == 7) {
        window.open(headPath + this.bookPathArr[index], "_blank");
      }
    },
    // 校验裁决书
    verificationArbitrateRecordFn(parms) {
      this.buttonFlag = true;
      verificationArbitrateRecord(parms).then((res) => {
        this.cancel();
        this.buttonFlag = false;
        // this.$emit("updataList");
        this.$emit("getcaseApply", this.queryParams);
        // this.$message("成功");
        this.$modal.msgSuccess("提交成功");
      });
    },
    //审核裁决书
    checkArbitrateRecordFn(parms) {
      this.buttonFlag = true;
      checkArbitrateRecord(parms).then((res) => {
        this.cancel();
        this.buttonFlag = false;
        // this.$emit("updataList");
        this.$emit("getcaseApply", this.queryParams);
        // this.$message("成功");
        this.$modal.msgSuccess("提交成功");
      });
    },
    // 仲裁员审核裁决书
    arbitratorCheckArbitrateRecordFn(parms) {
      this.buttonFlag = true;
      console.log(parms)
      arbitratorCheckArbitrateRecord(parms).then((res) => {
        this.cancel();
        this.buttonFlag = false;
        // this.$emit("updataList");
        this.$emit("getcaseApply", this.queryParams);
        // this.$message("成功");
        this.$modal.msgSuccess("提交成功");
      });
    },
    // 签名
    adjudicationSignatureFn(parms) {
      this.buttonFlag = true;
      adjudicationSignature(parms).then((res) => {
        this.cancel();
        this.buttonFlag = false;
        // this.$emit("updataList");
        this.$emit("getcaseApply", this.queryParams);
        this.$message("成功");
      })
    },
        // 上传裁决书
    UploadUrls() {
      return window.location.origin + "/API/evidence/uploadRecord";
    },
    beforeRemoves(file,fileList){
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    beforeUpload(flie, fileList) {
      // this.fileList = fileList;
    },
     // 上传文件成功
    handlSuccessAw(res, file) {
      // this.fileListAw.push(file);         
    },
    beforeAvatarUploads(){
      this.filedatas.id = this.awardDetailform.id;
    },
    submitForm(parms) {
      let that = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (parms == 0) {
            this.verificationArbitrateRecordFn({
              id: this.form.id,
              arbitrateRecord: this.arbitrateRecord,
            });
          } else if (parms == 1) {
            if(this.agreeOrNotCheck==1){
              //部门长同意
               this.checkArbitrateRecordFn({
              id: this.form.id,
              agreeOrNotCheck: 1,
              arbitrateRecord: this.arbitrateRecord,
            });
            }else if(this.agreeOrNotCheck==2){
              //  部门长驳回
              this.checkArbitrateRecordFn({
                id: this.form.id,
                agreeOrNotCheck: 2,
                arbitrateRecord: this.arbitrateRecord,
              });
            } 
          } else if (parms == 2) {
           
          } else if (parms == 3) {
            this.adjudicationSignatureFn({
              id: this.form.id,
            });
          }else if (parms == 6) {
              if(this.agreeOrNotCheck==1){
                //仲裁员同意
                 this.arbitratorCheckArbitrateRecordFn({
              id: this.form.id,
              agreeOrNotCheck: 1,
              arbitrateRecord: this.arbitrateRecord,
            });
              }else if(this.agreeOrNotCheck==2){
                //仲裁员驳回
                  this.arbitratorCheckArbitrateRecordFn({
                id: this.form.id,
                agreeOrNotCheck: 2,
                arbitrateRecord: this.arbitrateRecord,
              });
              }
          }
        }
      });
    },
    cancel() {
      this.$emit("cancelpaymentdetailsCom");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  border-radius: 30px;
}

.esign {
  border: 1px solid;
}

a {
  color: blue;
}
</style>