<template>
  <div>
    <el-dialog :title="title" :visible="openDialog" 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="arbitrateRecord.responCrossOpin" placeholder="请输入" />
          </el-form-item>
        <el-form-item label="审核意见：" prop="checkOpinion" v-if="flag == 2">
          <el-input type="textarea" autosize placeholder="请输入内容" v-model="arbitrateRecord.checkOpinion" />
        </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>
      <!-- 清除画布 -->
      <el-button v-if="flag == 3" type="info" @click="handleReset">清除画布</el-button>
      <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">提 交</el-button>
        <el-button type="primary" @click="submitForm(1)" v-if="flag == 2">同 意</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 @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  verificationArbitrateRecord,
  checkArbitrateRecord,
  adjudicationSignature,
  regenerationDocument
} from "@/api/awardManagement/awardManagement";
export default {
  props: ["openDialog", "title", "flag", "detailform"],
  data() {
    return {
      // key: value
      form: {},
      arbitrateRecord: {
        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",
          },
        ],
      },
      applicantFileArr: [], //申请人
      applicantPathArr: [], //申请人
      respondentFileArr: [], //被申请人
      respondenPathArr: [], //被申请人
      awardFileArr: [], //庭审笔录文件
      awardPathArr: [], //庭审笔录路径
      bookFileArr: [], //裁决书文件
      bookPathArr: [], //庭审笔录路径
    };
  },
  watch: {
    detailform: {
      handler(val) {
        if (val) {
          (this.applicantFileArr = []), //申请人
            (this.applicantPathArr = []), //申请人
            (this.respondentFileArr = []), //被申请人
            (this.respondenPathArr = []), //被申请人
            (this.awardFileArr = []), //裁决书
            (this.awardPathArr = []), //裁决书
            setTimeout(() => {
              this.form = this.detailform;
              console.log(this.detailform)
              if(this.form.arbitrateRecord.checkOpinion == null ){
                this.form.arbitrateRecord.checkOpinion = '同意'
              }
              this.arbitrateRecord = this.form.arbitrateRecord;
              this.evidenceFile(this.form);
            }, 1000);
        }
      },
    },
  },
  methods: {
    // 申请人裁决书
    evidenceFile(val) {
      if (val) {
        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 = [];
            this.awardPathArr = [];
            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) {
      verificationArbitrateRecord(parms).then((res) => {
        this.cancel();
        this.$emit("updataList");
        this.$message("成功");
      });
    },
    //审核裁决书
    checkArbitrateRecordFn(parms) {
      checkArbitrateRecord(parms).then((res) => {
        this.cancel();
        this.$emit("updataList");
        this.$message("成功");
      });
    },
    // 签名
    adjudicationSignatureFn(parms) {
      adjudicationSignature(parms).then((res) => {
        this.cancel();
        this.$emit("updataList");
        this.$message("成功");
      })
    },
    // 重新生成裁决书
    getRegenerationDocument(params) {
      regenerationDocument(params).then(res => {
        console.log(res.data, "重新生成裁决书");
        this.awardFileArr = [];
        this.awardPathArr = [];
        this.awardFileArr[0] = res.data.annexName;
        this.awardPathArr[0] = res.data.annexPath;
      })
    },
    submitForm(parms) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (parms == 0) {
            this.verificationArbitrateRecordFn({
              id: this.form.id,
              arbitrateRecord: this.arbitrateRecord,
            });
          } else if (parms == 1) {
            this.checkArbitrateRecordFn({
              id: this.form.id,
              agreeOrNotCheck: 1,
              arbitrateRecord: this.arbitrateRecord,
            });
          } else if (parms == 2) {
            this.checkArbitrateRecordFn({
              id: this.form.id,
              agreeOrNotCheck: 2,
              arbitrateRecord: this.arbitrateRecord,
            });
          } else if (parms == 3) {
            this.adjudicationSignatureFn({
              id: this.form.id,
            });
          } else if (parms == 4) {
            let paramsData = {};
            paramsData.arbitrateRecord = this.arbitrateRecord;
            paramsData.id = this.form.id
            this.getRegenerationDocument(paramsData)
          }
        }
      });
    },
    cancel() {
      this.$emit("cancelpaymentdetails");
    },
    // 清除画布
    handleReset() {
      this.$refs["esign"].reset(); //清空画布
    },
  },
};
</script>

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

.esign {
  border: 1px solid;
}

a {
  color: blue;
}
</style>