<template>
  <div>
    <el-dialog
      :title="dialogtitle"
      :visible="visible"
      @close="cancel"
      width="1000px"
      append-to-body
      :destroy-on-close= true
    >
      <!-- 案件信息 -->
      <el-form
        ref="form"
        :model="formData"
        :rules="rules"
        label-width="150px"
        :disabled="flag == '0'"
      >
        <p>案件信息：</p>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件编号：" prop="caseNum">
              <el-input
                v-model="formData.caseNum"
                placeholder="请输入案件编号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="案件标的：" prop="caseSubjectAmount">
              <el-input
                v-model="formData.caseSubjectAmount"
                placeholder="请输入案件标的"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="借款开始日期：" prop="loanStartDate">
              <el-date-picker
                v-model="formData.loanStartDate"
                type="datetime"
                placeholder="借款开始日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="借款结束日期：" prop="loanEndDate">
              <el-date-picker
                v-model="formData.loanEndDate"
                type="datetime"
                placeholder="借款结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同编号：" prop="contractNumber">
              <el-input
                v-model="formData.contractNumber"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人主张欠本金：" prop="claimPrinciOwed">
              <el-input
                v-model="formData.claimPrinciOwed"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人主张欠利息：" prop="claimInterestOwed">
              <el-input
                v-model="formData.claimInterestOwed"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请人主张违约金：" prop="claimLiquidDamag">
              <el-input
                v-model="formData.claimLiquidDamag"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="申请人案件证据资料上传："
              prop="applicantEvidence"
            >
              <el-upload
                class="upload-demo"
                action="https://jsonplaceholder.typicode.com/posts/"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="3"
                :on-exceed="handleExceed"
                :file-list="fileList"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
          <!-- 仅详情展示 -->
          <el-col :span="12">
            <el-form-item
              label="被申请人案件证据资料上传："
              prop="respondentEvidence"
            >
              <el-upload
                class="upload-demo"
                action="https://jsonplaceholder.typicode.com/posts/"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :before-remove="beforeRemove"
                multiple
                :limit="3"
                :on-exceed="handleExceed"
                :file-list="fileList"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 申请人主体信息 -->
      <el-form
        ref="form2"
        :model="form2"
        label-width="150px"
        :disabled="flag == '0'"
      >
        <div v-for="(item, index) in form2.paymentArr" :key="index">
          <div style="display: flex; justify-content: space-between">
            <p>申请人主体信息：</p>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteData(index)"
              v-if="form2.paymentArr.length > 1 && flag != '0'"
            ></el-button>
          </div>
          <el-divider></el-divider>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="申请人姓名"
                :prop="'paymentArr.' + index + '.name'"
                :rules="[
                  {
                    required: true,
                    message: '申请人不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.name" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="身份证号："
                :prop="'paymentArr.' + index + '.identityNum'"
                :rules="[
                  {
                    required: true,
                    message: '身份证号不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.identityNum" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系电话："
                :prop="'paymentArr.' + index + '.contactTelphone'"
                :rules="[
                  {
                    required: true,
                    message: '联系电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.contactTelphone" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="单位地址："
                :prop="'paymentArr.' + index + '.workAddress'"
                :rules="[
                  {
                    required: true,
                    message: '单位地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.workAddress" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="单位电话："
                :prop="'paymentArr.' + index + '.workTelphone'"
                :rules="[
                  {
                    required: true,
                    message: '单位电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.workTelphone" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系地址："
                :prop="'paymentArr.' + index + '.contactAddress'"
                :rules="[
                  {
                    required: true,
                    message: '联系地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.contactAddress" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>
          <p>代理人信息：</p>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="姓名："
                :prop="'paymentArr.' + index + '.nameAgent'"
                :rules="[
                  {
                    required: true,
                    message: '姓名不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="item.nameAgent" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="身份证号："
                :prop="'paymentArr.' + index + '.identityNumAgent'"
                :rules="[
                  {
                    required: true,
                    message: '身份证号不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  v-model="item.identityNumAgent"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系电话："
                :prop="'paymentArr.' + index + '.contactTelphoneAgent'"
                :rules="[
                  {
                    required: true,
                    message: '联系电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  v-model="item.contactTelphoneAgent"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系地址："
                :prop="'paymentArr.' + index + '.contactAddressAgent'"
                :rules="[
                  {
                    required: true,
                    message: '联系地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  v-model="item.contactAddressAgent"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <el-button
        v-if="flag == 1 || flag == 2"
        type="primary"
        plain
        @click="generateForm"
        >新增申请人主体信息</el-button
      >
      <!-- 被申请人主体信息 -->
      <el-form
        ref="form3"
        label-width="150px"
        :disabled="flag == '0'"
        :model="form3"
      >
        <div
          v-for="(itm, index) in form3.paymentArr1"
          :key="index + form2.paymentArr.length"
        >
          <div style="display: flex; justify-content: space-between">
            <p>被申请人主体信息：</p>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteData1(index)"
              v-if="form3.paymentArr1.length > 1 && flag != '0'"
            ></el-button>
          </div>
          <el-divider></el-divider>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="被申请人姓名"
                :prop="'paymentArr1.' + index + '.name'"
                :rules="[
                  {
                    required: true,
                    message: '被申请人不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.name" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="身份证号："
                :prop="'paymentArr1.' + index + '.identityNum'"
                :rules="[
                  {
                    required: true,
                    message: '身份证号不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.identityNum" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系电话："
                :prop="'paymentArr1.' + index + '.contactTelphone'"
                :rules="[
                  {
                    required: true,
                    message: '联系电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.contactTelphone" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="单位地址："
                :prop="'paymentArr1.' + index + '.workAddress'"
                :rules="[
                  {
                    required: true,
                    message: '单位地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.workAddress" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="单位电话："
                :prop="'paymentArr1.' + index + '.workTelphone'"
                :rules="[
                  {
                    required: true,
                    message: '单位电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.workTelphone" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系地址："
                :prop="'paymentArr1.' + index + '.contactAddress'"
                :rules="[
                  {
                    required: true,
                    message: '联系地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.contactAddress" placeholder="请输入" />
              </el-form-item>
            </el-col>
          </el-row>
          <p>代理人信息：</p>
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="姓名："
                :prop="'paymentArr1.' + index + '.nameAgent'"
                :rules="[
                  {
                    required: true,
                    message: '姓名不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.nameAgent" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="身份证号："
                :prop="'paymentArr1.' + index + '.identityNumAgent'"
                :rules="[
                  {
                    required: true,
                    message: '身份证号不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input v-model="itm.identityNumAgent" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系电话："
                :prop="'paymentArr1.' + index + '.contactTelphoneAgent'"
                :rules="[
                  {
                    required: true,
                    message: '联系电话不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  v-model="itm.contactTelphoneAgent"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="联系地址："
                :prop="'paymentArr1.' + index + '.contactAddressAgent'"
                :rules="[
                  {
                    required: true,
                    message: '联系地址不能为空',
                    trigger: 'blur',
                  },
                ]"
              >
                <el-input
                  v-model="itm.contactAddressAgent"
                  placeholder="请输入"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <el-button
        v-if="flag == 1 || flag == 2"
        type="primary"
        plain
        @click="generateForm1()"
        >新增被申请人主体信息</el-button
      >
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="flag != 0"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import moment from "moment"; //导入文件
import { addCaseApply, editCaseApply } from "@/api/caseAccess/caseEntry";
export default {
  props: [
    "visible",
    "dialogtitle",
    "form",
    "getcaseApply",
    "flag",
    "initpaymentArr",
    "initpaymentArr1",
  ],
  data() {
    return {
      formData: this.form,
      form2: {
        paymentArr: [
          {
            identityType: 1,
            name: "",
            identityNum: "",
            contactTelphone: "",
            workAddress: "",
            workTelphone: "",
            contactAddress: "",
            nameAgent: "",
            contactTelphoneAgent: "",
            contactAddressAgent: "",
          },
        ],
      }, //申请人主体信息
      form3: {
        paymentArr1: [
          {
            identityType: 2,
            name: "",
            identityNum: "",
            contactTelphone: "",
            workAddress: "",
            workTelphone: "",
            contactAddress: "",
            nameAgent: "",
            contactTelphoneAgent: "",
            contactAddressAgent: "",
          },
        ],
      }, //被申请人主体信息
      // 表单校验
      rules: {
        caseNum: [
          {
            required: true,
            message: "案件编号不能为空",
            trigger: ["change", "blur"],
          },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        caseSubjectAmount: [
          {
            required: true,
            message: "案件标的不能为空",
            trigger: ["change", "blur"],
          },
          // { type: "Number", message: "案件标的必须为数字值" },
        ],
        loanStartDate: [
          {
            required: true,
            message: "借款开始日期不能为空",
            trigger: "blur",
          },
        ],
        loanEndDate: [
          {
            required: true,
            message: "借款结束日期不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "申请人姓名不能为空",
            trigger: "blur",
          },
        ],
        contractNumber: [
          {
            required: true,
            message: "合同编号不能为空",
            trigger: "blur",
          },
        ],
        claimPrinciOwed: [
          {
            required: true,
            message: "申请人主张欠本金不能为空",
            trigger: "blur",
          },
          // { type: 'number', message: '申请人主张欠本金必须为数字值'}
        ],
        claimInterestOwed: [
          {
            required: true,
            message: "申请人主张欠利息不能为空",
            trigger: "blur",
          },
          // { type: 'number', message: '申请人主张欠利息必须为数字值'}
        ],
        Overduedays: [
          {
            required: true,
            message: "逾期天数不能为空",
            trigger: "blur",
          },
          // { type: 'number', message: '逾期天数必须为数字值'}
        ],
        claimLiquidDamag: [
          {
            required: true,
            message: "申请人主张违约金不能为空",
            trigger: "blur",
          },
          // { type: 'number', message: '申请人主张违约金必须为数字值'}
        ],
      },
      fileList: [],
    };
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.formData = this.form;
          if (this.flag == "1" || this.flag == "0") {
            setTimeout(() => {
              this.form2.paymentArr = this.initpaymentArr;
              this.form3.paymentArr1 = this.initpaymentArr1;
            }, 1000);
          }
        }
      },
      deep: true,
    },
  },
  methods: {
    handleRemove(file, fileList) {},
    handlePreview(file) {},
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 取消
    cancel() {
      this.$emit("cancel");
    },

    // 新增申请人主体信息
    generateForm() {
      this.form2.paymentArr.push({
        identityType: 1,
        name: "",
        identityNum: "",
        contactTelphone: "",
        workAddress: "",
        workTelphone: "",
        contactAddress: "",
        nameAgent: "",
        contactTelphoneAgent: "",
        contactAddressAgent: "",
      });
    },
    // 新增被申请人主体信息
    generateForm1() {
      this.form3.paymentArr1.push({
        identityType: 2,
        name: "",
        identityNum: "",
        contactTelphone: "",
        workAddress: "",
        workTelphone: "",
        contactAddress: "",
        nameAgent: "",
        contactTelphoneAgent: "",
        contactAddressAgent: "",
      });
    },
    // 删除申请人主体信息
    deleteData(index) {
      this.form2.paymentArr.splice(index, 1);
    },
    // 删除被申请人主体信息
    deleteData1(index) {
      this.form3.paymentArr1.splice(index, 1);
    },
    // 提交立案申请 addCaseApply
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.formData.loanStartDate = moment(
          this.formData.loanStartDate
        ).format("YYYY-MM-DD HH:mm:ss");
        this.formData.loanEndDate = moment(this.formData.loanEndDate).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        if (valid) {
          this.$refs["form2"].validate((valid) => {
            if (valid) {
              this.$refs["form3"].validate((vail) => {
                if (vail) {
                  this.formData.caseAffiliates = [
                    ...this.form2.paymentArr,
                    ...this.form3.paymentArr1,
                  ];
                  if (this.formData.id) {
                    editCaseApply({
                      ...this.formData,
                      caseAffiliates: this.formData.caseAffiliates,
                    }).then((res) => {
                      this.$modal.msgSuccess("修改成功");
                      this.$emit("cancel");
                      this.getcaseApply();
                    });
                  } else {
                    addCaseApply({
                      ...this.formData,
                      caseAffiliates: this.formData.caseAffiliates,
                    }).then((response) => {
                      this.$modal.msgSuccess("新增成功");
                      this.$emit("cancel");
                      this.getcaseApply();
                    });
                  }
                  // this.$emit("cancel");
                  // this.getcaseApply();
                }
              });
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 700px !important;
  overflow: auto !important;
}
</style>