·<template>
  <div class="eibs-tab">
    <c-col :span="12" style="padding-right: 20px">
      <c-col :span="24">
        <el-form-item label="参数类型" prop="rec.pratyp">
          <c-select v-model="model.rec.pratyp" dbCode="PRATYP" @change="changePratyp" style="width: 100%" placeholder="请选择参数类型"> </c-select>
        </el-form-item>
      </c-col>
      <c-col :span="24">
        <el-form-item label="优惠类型" prop="rec.rattyp">
          <c-select v-model="model.rec.rattyp" dbCode="YOHTYP" disabled style="width: 100%" placeholder="请选择优惠类型"> </c-select>
        </el-form-item>
      </c-col>
    </c-col>

    <c-col :span="12" style="padding-left: 20px" v-if="model.rec.pratyp=='2'">
      <c-col :span="24">
        <c-col :span="12">
          <el-form-item label="Party No." prop="rec.ecifno">
            <c-input v-model="model.rec.ecifno" @keyup.enter.native="showPtapDialog()" @blur="handleExtkeyBlur()" @clear="clear()" @input="extkeyInput()" style="width: 100%" placeholder="请输入Party No."> </c-input>
          </el-form-item>
        </c-col>
        <c-col :span="12">
          <el-form-item label-width="5px" prop="rec.cusnam">
            <c-input v-model="model.rec.cusnam" disabled style="width: 100%" placeholder="请输入"> </c-input>
          </el-form-item>
        </c-col>
      </c-col>
      <c-col :span="24">
        <el-form-item label="是否电子购汇签约" prop="pty.sigflg">
          <c-select v-model="model.pty.sigflg" dbCode="SIGFLG" disabled style="width: 100%" placeholder="请选择是否电子购汇签约"> </c-select>
        </el-form-item>
      </c-col>
    </c-col>

    <c-col :span="24">
      <span style="float: left">
        <el-button type="primary" size="small" @click="sefratAdd">新增</el-button>
      </span>
      <span style="float: right">
        <el-button size="small" @click="handleReset">重置</el-button>
        <el-button type="primary" icon="el-icon-search" size="small" :loading="searchLoading" @click="onSearch">查询
        </el-button>
      </span>
    </c-col>

    <c-col :span="24">
      <c-table :list="sefratData" :border="true">
        <el-table-column label="币种" prop="sefhepcur" sortable width="auto">
          <template slot-scope="scope">
            <span> {{getCodelabel(scope.row.sefhepcur,'curtxt') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="优惠类型" prop="sefheptyp" sortable width="auto">
          <template slot-scope="scope">
            <span> {{getCodelabel(scope.row.sefheptyp,'heptyp') }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="优惠比例/点数" prop="sefheprat" sortable width="auto">
        </el-table-column>
        <el-table-column label="有效期起始日" prop="sefhepdatfrm" sortable width="auto">
        </el-table-column>
        <el-table-column label="有效期截止日" prop="sefhepdatend" sortable width="auto">
        </el-table-column>
        <el-table-column label="提前申请时限" prop="sefhepappday" sortable width="auto">
        </el-table-column>
        <el-table-column label="单笔限额" prop="sefheplmtamt" sortable width="auto">
        </el-table-column>
        <el-table-column label="开始时间(HH:MM:SS)" prop="sefheptimfrm" sortable width="auto">
        </el-table-column>
        <el-table-column label="结束时间(HH:MM:SS)" prop="sefheptimend" sortable width="auto">
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button @click="sefratInfo(scope.$index, scope.row)" type="" size="mini">详情</el-button>
            <c-button style="margin-left: 5px" size="small" type="primary" @click="sefratEdit(scope.$index, scope.row)">修改</c-button>
            <c-button size="small" style="margin-left: 5px" @click="sefratDelete(scope.$index, scope.row)">删除</c-button>
          </template>
        </el-table-column>
      </c-table>
    </c-col>

    <el-dialog :title="
        '购汇信息:' +
        (operate === 'details' ? '详情' : operate === 'edit' ? '修改' : '新增')
      " :visible.sync="sefratDialog" top="10vh" width="80%" :destroy-on-close="true" :before-close="handleClose">
      <el-form ref="sefratForm" :model="model" :rules="rules" label-width="170px" label-position="right" :disabled="operate === 'details'">
        <c-col :span="12" style="padding-right: 20px;">
          <c-col :span="24">
            <el-form-item label="参数类型" prop="rec.pratyp">
              <c-select v-model="model.rec.pratyp" :disabled="operate === 'edit'" @change="changePratyp" dbCode="PRATYP" style="width: 100%" placeholder="请选择参数类型"> </c-select>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="优惠类型" prop="rec.rattyp">
              <c-select v-model="model.rec.rattyp" dbCode="YOHTYP" disabled style="width: 100%" placeholder="请选择优惠类型"> </c-select>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <c-col :span="20">
              <el-form-item label="自助售汇提前申请时限" prop="rec.sefhepappday">
                <c-input v-model="model.rec.sefhepappday" maxlength="2" :disabled="model.rec.pratyp !='0'" style="width: 100%" placeholder="请输入请输入自助售汇提前申请时限"> </c-input>
              </el-form-item>
            </c-col>
            <c-col :span="4" v-if="model.rec.pratyp=='2'">
              <el-form-item label-width="0px" prop="rec.allowlistflg">
                <c-checkbox v-model="model.rec.allowlistflg" @change="changeAllowlistflg" :disabled="model.sigflg!='0'" style="width: 100%" true-label="X" false-label="" >白名单标识</c-checkbox>
              </el-form-item>
            </c-col>
          </c-col>
          <c-col :span="24">
            <el-form-item label="售汇交易开始时间(HH:MM:SS)" prop="rec.sefheptimfrm">
              <c-input v-model="model.rec.sefheptimfrm" maxlength="8" :disabled="model.rec.pratyp !='0' " style="width: 100%" placeholder="请输入售汇交易开始时间(HH:MM:SS)"> </c-input>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="售汇交易截止时间(HH:MM:SS)" prop="rec.sefheptimend">
              <c-input v-model="model.rec.sefheptimend" maxlength="8" :disabled="model.rec.pratyp !='0' " style="width: 100%" placeholder="请输入售汇交易截止时间(HH:MM:SS)"> </c-input>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="售汇单笔申请限额" prop="rec.sefheplmtamt">
              <c-input-currency v-model="model.rec.sefheplmtamt" :disabled="model.rec.pratyp !='0' " style="width: 100%" placeholder="请输入售汇单笔申请限额"> </c-input-currency>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="自助售汇优惠币种" prop="rec.sefhepcur">
              <c-select v-model="model.rec.sefhepcur" :disabled="model.rec.pratyp !='0' || operate === 'edit'" dbCode="CURTXT" :isShowKeyAndLabel="true" :filterKey="['AUD','CAD','CHF','DKK','EUR','GBP','JPY','HKD','MOP','NOK','NZD','SEK','SGD','USD']" style="width: 100%" placeholder="请选择自助售汇优惠币种"> </c-select>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="自助售汇优惠类型" prop="rec.sefheptyp">
              <c-select v-model="model.rec.sefheptyp" @change="changeSefheptyp" :disabled="model.rec.pratyp !='0' " dbCode="HEPTYP" style="width: 100%" placeholder="请选择自助售汇优惠类型"> </c-select>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="自助售汇优惠比例/点数" prop="rec.sefheprat">
              <c-input-currency v-model="model.rec.sefheprat" :disabled="model.rec.sefheptyp=='' || operate === 'edit'" style="width: 100%" placeholder="请输入自助售汇优惠比例/点数"> </c-input-currency>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="自助售汇优惠有效期起始日" prop="rec.sefhepdatfrm">
              <c-date-picker v-model="model.rec.sefhepdatfrm" :disabled="model.rec.sefheptyp=='' || operate === 'edit'" style="width: 100%" placeholder="请输入自助售汇优惠有效期起始日"> </c-date-picker>
            </el-form-item>
          </c-col>
          <c-col :span="24">
            <el-form-item label="自助售汇优惠有效期截止日" prop="rec.sefhepdatend">
              <c-date-picker v-model="model.rec.sefhepdatend" :disabled="model.rec.sefheptyp=='' || operate === 'edit'" style="width: 100%" placeholder="请输入自助售汇优惠有效期截止日"> </c-date-picker>
            </el-form-item>
          </c-col>
        </c-col>

        <c-col :span="12" style="padding-right: 20px;">
          <c-col :span="24" v-if="model.rec.pratyp=='2'">
            <c-col :span="12">
              <el-form-item label="Party No." prop="rec.ecifno">
                <c-input v-model="model.rec.ecifno" @keyup.enter.native="showPtapDialog()" @blur="handleExtkeyBlur()" @clear="clear()" @input="extkeyInput()" style="width: 100%" placeholder="请输入Party No."> </c-input>
              </el-form-item>
            </c-col>
            <c-col :span="12">
              <el-form-item label-width="5px" prop="rec.cusnam">
                <c-input v-model="model.rec.cusnam" disabled style="width: 100%" placeholder="请输入"> </c-input>
              </el-form-item>
            </c-col>
          </c-col>
          <c-col :span="24">
            <c-col :span="12">
              <el-form-item label="币种" prop="rec.sefhepcur">
                <c-select v-model="model.rec.sefhepcur" :disabled="operate === 'edit'" @change="changeSefhepcur" dbCode="CURTXT" :isShowKeyAndLabel="true" :filterKey="['AUD','CAD','CHF','DKK','EUR','GBP','JPY','HKD','MOP','NOK','NZD','SEK','SGD','USD']" style="width: 100%" placeholder="请选择币种"> </c-select>
              </el-form-item>
            </c-col>
            <c-col :span="12" v-if="model.rec.pratyp=='2'">
              <el-form-item label="是否电子购汇签约" prop="sigflg">
                <c-select v-model="model.sigflg" dbCode="SIGFLG" style="width: 100%" placeholder="请选择是否电子购汇签约"> </c-select>
              </el-form-item>
            </c-col>
          </c-col>
          <c-col :span="24" v-if="model.rec.pratyp=='0' && model.rec.sefhepcur=='USD'">
            <c-col :span="12">
              <el-form-item label="全局参数提前申请时限最小值" prop="rec.minappday">
                <c-input v-model="model.rec.minappday" maxlength="2" style="width: 100%" placeholder="请输入最小值"> </c-input>
              </el-form-item>
            </c-col>
            <c-col :span="12">
              <el-form-item label="最大值" prop="rec.maxappday">
                <c-input v-model="model.rec.maxappday" maxlength="2" style="width: 100%" placeholder="请输入最大值"> </c-input>
              </el-form-item>
            </c-col>
          </c-col>
          <c-col :span="24" v-if="model.rec.pratyp=='0' && model.rec.sefhepcur=='USD'">
            <el-form-item label="全局参数交易开始时间" prop="rec.mintim">
              <c-input v-model="model.rec.mintim" maxlength="8" style="width: 100%" placeholder="请输入全局参数交易开始时间"> </c-input>
            </el-form-item>
          </c-col>
          <c-col :span="24" v-if="model.rec.pratyp=='0' && model.rec.sefhepcur=='USD'">
            <el-form-item label="全局参数交易截止时间" prop="rec.maxtim">
              <c-input v-model="model.rec.maxtim" maxlength="8" style="width: 100%" placeholder="请输入全局参数交易截止时间"> </c-input>
            </el-form-item>
          </c-col>
          <c-col :span="24" v-if="model.rec.allowlistflg=='X' || model.rec.pratyp=='0'">
            <el-form-item label="单笔申请限额最大值" prop="rec.maxlmtamt">
              <c-input-currency v-model="model.rec.maxlmtamt" :disabled="model.rec.allowlistflg=='X'" style="width: 100%" placeholder="请输入单笔申请限额最大值"> </c-input-currency>
            </el-form-item>
          </c-col>
        </c-col>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <button class="el-button el-button--default el-button--small" style="margin-left: 0" size="small" :disabled="false" @click.prevent="cancel">
          <span>取 消</span>
        </button>
        <c-button type="primary" @click="cancel" v-if="operate === 'details'">确 定</c-button>
        <c-button type="primary" @click="editSefrat" v-if="operate === 'edit'">保 存</c-button>
        <c-button type="primary" @click="saveSefrat" v-if="operate === 'add'">保 存</c-button>
      </span>
    </el-dialog>

    <!-- 客户信息弹窗 -->
		<el-dialog title="客户信息详情" v-dialogDrag width="64%" :visible.sync="dialogTableVisible" destroy-on-close :close-on-click-modal="false" :close-on-press-escape="false" :show-close="true" v-if="dialogTableVisible" :modal-append-to-body="false" :append-to-body="true">
			<div v-if="!tableLoading && tableList.length === 0">暂无数据</div>
			<div v-else style="width: 100%;height: 100%;">
				<el-table id='tableRef' height="calc(100% - 32px)" style="width: 100%;" v-loading="tableLoading" :data="tableList" @row-dblclick="dbClickRow" :before-close="beforeClose">
					<el-table-column label="Party Number" prop="extkey" width="auto">
					</el-table-column>
					<el-table-column label="Name 1" prop="nam" width="auto">
					</el-table-column>
          <el-table-column label="Chinese Name" prop="nam1" width="auto">
					</el-table-column>
					<el-table-column label="Type" prop="ptytyp" width="auto">
					</el-table-column>
				</el-table>
				<el-pagination class="eContainer-pagination" layout="prev, pager, next, jumper, ->, sizes, total" :page-sizes="pageSizes" :page-size="pageSize" :current-page="currentPage" :total="total" @size-change="sizeChange" @current-change="currentChange"></el-pagination>
			</div>
		</el-dialog>

  </div>
</template>

<script>
import Sefrat, { Pattern } from "./Sefrat.js";
import codes from "~/config/CodeTable";
import commonFunctions from "~/mixin/commonFunctions.js";
import commonDepend from "~/mixin/commonDepend.js";
import Api from "~/service/Api";

export default {
  inject: ["root"],
  mixins: [commonFunctions, commonDepend],
  props: ["model"],
  data() {
    return {
      sefratData: [],
      searchLoading: false,
      sefratDialog: false,
      sefrat: new Sefrat().data,
      operate: "",
      operateIdx: 0,
      dialogTableVisible: false, // 控制弹框的展示和隐藏
      tableLoading: false,
      dbCodes: {
        heptyp: [],
        curtxt: [],
      },
      total: 0,
      currentPage: 1, // 页数
      pageSizes: [5, 10, 20, 30, 40, 50, 100],
      pageSize: 10, // 条数
      tableList: [],
      markExtkey: "",
      rules: {
        "rec.pratyp": [{
            required: true,message: "必输项"
          },
        ],
        "rec.ecifno": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == "2") {
                if (!value) {
                  callback(new Error("必输项"));
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefhepcur": [{
            required: true,message: "必输项"
          },
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == "2") {
                if (this.model.currat.inr == "" || this.model.currat.inr == null) {
                  callback(new Error("当前币种总行未维护参数,请联系管理员"));
                }
              }
              if(this.operate === 'add'){
                if (this.model.issefhepcurexit == "Y") {
                  if(this.model.rec.ecifno){
                    callback(new Error("该客户对应的币种" + value + "已新增过优惠参数"));
                  } else {
                    callback(new Error("该币种" + value + "已维护过优惠参数,请去维护交易进行修改"));
                  }
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefhepappday": [
          {
            required: true,
            message: "必输项"
          },
          {
            validator: (rule, value, callback) => {
              if (Number(value)) {
                if (
                  this.model.rec.pratyp == "0" &&
                  this.model.rec.sefhepcur == "USD"
                ) {
                  if (
                    Number(value) > Number(this.model.rec.maxappday) ||
                    Number(value) < Number(this.model.rec.minappday)
                  ) {
                    callback(
                      new Error(
                        "提前申请时限范围为" +
                          this.model.rec.minappday +
                          "-" +
                          this.model.rec.maxappday +
                          "天"
                      )
                    );
                  }
                } else {
                  if (
                    Number(value) < Number(this.model.globalrat.minappday) ||
                    Number(value) > Number(this.model.globalrat.maxappday)
                  ) {
                    callback(
                      new Error(
                        "提前申请时限范围为" +
                          this.model.globalrat.minappday +
                          "-" +
                          this.model.globalrat.maxappday +
                          "天"
                      )
                    );
                  }
                }
              } else {
                callback(new Error("必输项"));
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefheptimfrm": [
          {
            required: true,
            message: "必输项"
          },
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.sefheptimfrm) {
                let frmtim = this.model.rec.sefheptimfrm;
                let endtim = this.model.rec.sefheptimend;
                let HH = frmtim.slice(0,2);
                let MM = frmtim.slice(3,5);
                let SS = frmtim.slice(6,8);
                let mintim = "";
                let maxtim = "";
                if (
                  this.model.rec.pratyp == "0" &&
                  this.model.rec.sefhepcur == "USD"
                ) {
                  mintim = this.model.rec.mintim;
                  maxtim = this.model.rec.maxtim;
                } else {
                  if (
                    this.model.rec.pratyp == "2" &&
                    this.model.rec.allowlistflg == ""
                  ) {
                    mintim = this.model.currat.sefheptimfrm;
                    maxtim = this.model.currat.sefheptimend;
                  } else {
                    mintim = this.model.globalrat.mintim;
                    maxtim = this.model.globalrat.maxtim;
                  }
                }
                let minHH = mintim.slice(0,2);
                let minMM = mintim.slice(3,5);
                let minSS = mintim.slice(6,8);
                let maxHH = maxtim.slice(0,2);
                let maxMM = maxtim.slice(3,5);
                let maxSS = maxtim.slice(6,8);
                let endHH = endtim.slice(0,2);
                let endMM = endtim.slice(3,5);
                let endSS = endtim.slice(6,8);
                //输入格式校验
                if (HH > 23 || MM > 59 || SS > 59 || HH.includes(':') || MM.includes(':') || SS.includes(':') || HH.length != 2 || MM.length != 2 || SS.length != 2) {
                  callback(new Error("输入格式应为HH:MM:SS,输入不合法"));
                } else {
                  //最大时间限制校验
                  let timerr = 0;
                  if (HH > maxHH) {
                    callback(new Error("起止时间为:" + mintim + "-" + maxtim));
                    timerr = 1;
                  } else {
                    if (HH == maxHH && MM > maxMM) {
                      callback(
                        new Error("起止时间为:" + mintim + "-" + maxtim)
                      );
                      timerr = 1;
                    } else {
                      if (HH == maxHH && MM == maxMM && SS > maxSS) {
                        callback(
                          new Error("起止时间为:" + mintim + "-" + maxtim)
                        );
                        timerr = 1;
                      }
                    }
                  }
                  //最小时间限制校验
                  if (HH < minHH) {
                    callback(new Error("起止时间为:" + mintim + "-" + maxtim));
                    timerr = 1;
                  } else {
                    if (HH == minHH && MM < minMM) {
                      callback(
                        new Error("起止时间为:" + mintim + "-" + maxtim)
                      );
                      timerr = 1;
                    } else {
                      if (HH == minHH && MM == minMM && SS < minSS) {
                        callback(
                          new Error("起止时间为:" + mintim + "-" + maxtim)
                        );
                        timerr = 1;
                      }
                    }
                  }
                  //大于截止时间校验
                  if (this.model.rec.sefheptimend) {
                    if (timerr == 0) {
                      if (HH > endHH) {
                        callback(new Error("开始时间不能大于结束时间"));
                      } else {
                        if (HH == endHH && MM > endMM) {
                          callback(new Error("开始时间不能大于结束时间"));
                        } else {
                          if (HH == endHH && MM == endMM && SS > endSS) {
                            callback(new Error("开始时间不能大于结束时间"));
                          }
                        }
                      }
                    }
                  }
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefheptimend": [
          {
            required: true,
            message: "必输项"
          },
          {
            validator: (rule, value, callback) => {
              if (value) {
                let endtim = value;
                let HH = endtim.slice(0,2);
                let MM = endtim.slice(3,5);
                let SS = endtim.slice(6,8);
                let mintim = "";
                let maxtim = "";
                if (
                  this.model.rec.pratyp == "0" &&
                  this.model.rec.sefhepcur == "USD"
                ) {
                  mintim = this.model.rec.mintim;
                  maxtim = this.model.rec.maxtim;
                } else {
                  if (
                    this.model.rec.pratyp == "2" &&
                    this.model.rec.allowlistflg == ""
                  ) {
                    mintim = this.model.currat.sefheptimfrm;
                    maxtim = this.model.currat.sefheptimend;
                  } else {
                    mintim = this.model.globalrat.mintim;
                    maxtim = this.model.globalrat.maxtim;
                  }
                }
                let minHH = mintim.slice(0,2);
                let minMM = mintim.slice(3,5);
                let minSS = mintim.slice(6,8);
                let maxHH = maxtim.slice(0,2);
                let maxMM = maxtim.slice(3,5);
                let maxSS = maxtim.slice(6,8);
                //输入格式校验
                if (HH > 23 || MM > 59 || SS > 59 || HH.includes(':') || MM.includes(':') || SS.includes(':') || HH.length != 2 || MM.length != 2 || SS.length != 2) {
                  callback(new Error("输入格式应为HH:MM:SS,输入不合法"));
                } else {
                  //最大时间限制校验
                  if (HH > maxHH) {
                    callback(new Error("起止时间为:" + mintim + "-" + maxtim));
                  } else {
                    if (HH == maxHH && MM > maxMM) {
                      callback(
                        new Error("起止时间为:" + mintim + "-" + maxtim)
                      );
                    } else {
                      if (HH == maxHH && MM == maxMM && SS > maxSS) {
                        callback(
                          new Error("起止时间为:" + mintim + "-" + maxtim)
                        );
                      }
                    }
                  }
                  //最小时间限制校验
                  if (HH < minHH) {
                    callback(new Error("起止时间为:" + mintim + "-" + maxtim));
                  } else {
                    if (HH == minHH && MM < minMM) {
                      callback(
                        new Error("起止时间为:" + mintim + "-" + maxtim)
                      );
                    } else {
                      if (HH == minHH && MM == minMM && SS < minSS) {
                        callback(
                          new Error("起止时间为:" + mintim + "-" + maxtim)
                        );
                      }
                    }
                  }
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefheplmtamt": [
          {
            required: true,
            message: "必输项"
          },
          {
            validator: (rule, value, callback) => {
              if (Number(value)) {
                let cur = this.model.rec.sefhepcur;
                if (this.model.rec.pratyp == "0") {
                  if (Number(value) > Number(this.model.rec.maxlmtamt)) {
                    callback(
                      new Error(
                        "单笔限额必须小于等于总行设置最大值(" +
                          cur +
                          ")" +
                          this.model.rec.maxlmtamt
                      )
                    );
                  }
                } else {
                  if (this.model.rec.allowlistflg == "X") {
                    if (Number(value) > Number(this.model.currat.maxlmtamt)) {
                      callback(
                        new Error(
                          "白名单客户单笔限额必须小于等于总行设置最大值(" +
                            cur +
                            ")" +
                            this.model.currat.maxlmtamt
                        )
                      );
                    }
                  } else {
                    if (Number(value) > Number(this.model.currat.maxlmtamt)) {
                      callback(
                        new Error(
                          "非白名单,应小于等于总行设置的单笔限额(" +
                            cur +
                            ")" +
                            this.model.currat.maxlmtamt
                        )
                      );
                    }
                  }
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefhepdatend": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.sefheptyp) {
                if (!value) {
                  callback(new Error("必输项"));
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefhepdatend": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.sefheptyp) {
                if (!value) {
                  callback(new Error("必输项"));
                } else {
                  if (value < this.model.rec.sefhepdatfrm) {
                    callback(new Error("有效期截至日不能早于起始日"));
                  } else {
                    if (new Date(value) < new Date()) {
                      callback(new Error("有效期截止日不能早于当日"));
                    }
                  }
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.sefheprat": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.sefheptyp) {
                if (!Number(value)) {
                  callback(new Error("必输项"));
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.minappday": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == '0' && this.model.rec.sefhepcur == 'USD') {
                if (!Number(value)) {
                  callback(new Error("必输项"));
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.maxappday": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == '0' && this.model.rec.sefhepcur == 'USD') {
                if (!Number(value)) {
                  callback(new Error("必输项"));
                } else if(Number(value) > 22){
                  callback(new Error("最大时限不能超过22天"));
                }
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.mintim": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == '0' && this.model.rec.sefhepcur == 'USD') {
                if (!value) {
                  callback(new Error("必输项"));
                } 
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.maxtim": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == '0' && this.model.rec.sefhepcur == 'USD') {
                if (!value) {
                  callback(new Error("必输项"));
                } 
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
        "rec.maxlmtamt": [
          {
            validator: (rule, value, callback) => {
              if (this.model.rec.pratyp == '0') {
                if (!Number(value)) {
                  callback(new Error("必输项"));
                } 
              }
              callback();
            },
            trigger: ["blur"]
          }
        ],
      },
    };
  },
  computed: {},
  created() {},
  watch: {},
  mounted() {
    this.getdbCode("heptyp", "CN", "heptyp");
    this.getdbCode("curtxt", "CN", "curtxt");
  },
  methods: {
    getCodelabel(value, codenam) {
      const codeobj = this.dbCodes[codenam].find(obj => obj.value === value);
      return codeobj ? codeobj.label : value;
    },

    async getdbCode(codeType, uil, codeNam) {
      let params = {
        codeType: codeType,
        uil: uil ? uil : "EN"
      };
      let rtnmsg = await Api.post("/manager/dic/listDicInfo", params);
      if (rtnmsg.respCode === SUCCESS) {
        let curList = rtnmsg.data.map(item => ({
          value: item.codeValue,
          label: item.codeName
        }));
        this.dbCodes[codeNam] = curList;
      }
    },

    changePratyp() {
      this.sefratData = [];
      if(this.model.rec.pratyp == '0'){
        this.model.rec.ecifno = "";
        this.model.rec.cusnam = "";
        this.model.pty.sigflg = "";
        this.model.sigflg = "";
        this.model.rec.ptyinr = "";
      }
      if(this.model.rec.pratyp == '2'){
        this.model.rec.sefhepcur = "";
        this.model.rec.sefhepappday = '0';
        this.model.rec.sefheptimfrm = "";
        this.model.rec.sefhepdatend = "";
        this.model.rec.sefheptimend = "";
        this.model.rec.sefheptyp = "";
        this.model.rec.sefheprat = "";
        this.model.rec.sefhepdatfrm = "";
        this.model.rec.sefhepdatend = "";
      }
    },
    //重置
    handleReset() {
      this.model.rec.pratyp = "";
      this.model.rec.ecifno = "";
      this.model.rec.cusnam = "";
      this.model.pty.sigflg = "";
    },

    async onSearch() {
      if(!this.model.rec.pratyp){
        this.$notify.error({ title: '错误', message: '请先选择参数类型' });
        return;
      } else if(this.model.rec.pratyp == '2'){
        if(!this.model.rec.ecifno){
          this.$notify.error({ title: '错误', message: 'Party No.必输' });
          return;
        }
      }
      this.searchLoading = true;
      let rtnmsg = await Api.post("/manager/exr/querySefratlst", this.model);
      if (rtnmsg.respCode == "AAAAAA") {
        this.sefratData = rtnmsg.data.sefratlst;
        this.model.rec.ptyinr = rtnmsg.data.rec.ptyinr;
        this.model.globalrat = rtnmsg.data.globalrat;
        this.searchLoading = false;
      } else {
        this.$notify.error("未查询到购汇汇率信息");
      }
      this.searchLoading = false;
    },

    sefratAdd() {
      this.model.rec.sefhepcur = "";
      this.model.rec.allowlistflg = "";
      this.model.rec.sefheptimfrm = "";
      this.model.rec.sefheptimend = "";
      this.model.rec.sefheplmtamt = "";
      this.model.rec.sefheptyp = "";
      this.model.rec.sefheprat = "";
      this.model.rec.sefhepdatfrm = "";
      this.model.rec.sefhepdatend = "";
      this.model.rec.mintim = "";
      this.model.rec.maxtim = "";
      this.model.rec.maxlmtamt = "";
      this.operate = "add";
      this.sefratDialog = true;
    },

    //新增保存
    async saveSefrat() {
      this.$refs.sefratForm.validate(async validated => {
        if (validated) {
          if(this.model.rec.pratyp == '2'){
            this.model.pty.inr = this.model.rec.ptyinr;
            this.model.rec.maxlmtamt = "";
            this.model.pty.sigflg = this.model.sigflg;
          }
          let rtnmsg = await Api.post("/manager/exr/saveSefrat", this.model);
          if (rtnmsg.respCode == "AAAAAA") {
            this.$notify.success("新建成功");
            this.handleClose();
            this.onSearch();
          } else {
            this.$notify.error("新建失败");
            this.handleClose();
          }
        }
      });
    },

    //详情
    async sefratInfo(index, row) {
      this.model.rec = { ...row };
      this.model.sigflg = this.model.pty.sigflg;
      await this.getCurrat(this.model.rec.sefhepcur);
      this.model.rec.maxlmtamt = this.model.currat.maxlmtamt;
      this.operate = "details";
      this.operateIdx = index;
      this.sefratDialog = true;
    },

    //修改
    async sefratEdit(index, row) {
      this.model.issefhepcurexit = "N";
      this.model.rec = { ...row };
      this.model.sigflg = this.model.pty.sigflg;
      await this.getCurrat(this.model.rec.sefhepcur);
      this.model.rec.maxlmtamt = this.model.currat.maxlmtamt;
      this.operate = "edit";
      this.operateIdx = index;
      this.sefratDialog = true;
    },

    //修改保存
    async editSefrat() {
      this.$refs.sefratForm.validate(async validated => {
        if (validated) {
          if(this.model.rec.pratyp == '2'){
            this.model.pty.inr = this.model.rec.ptyinr;
            this.model.rec.maxlmtamt = "";
            this.model.pty.sigflg = this.model.sigflg;
          }
          let rtnmsg = await Api.post("/manager/exr/editSefrat", this.model);
          if (rtnmsg.respCode == "AAAAAA") {
            this.$notify.success("修改成功");
            this.handleClose();
            this.onSearch();
          } else {
            this.$notify.error("新建失败");
            this.handleClose();
          }
        }
      });
    },

    //删除
    async sefratDelete(index, row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async (res) => {
        this.model.rec = { ...row };
        let rtnmsg = await Api.post("/manager/exr/sefratDelete", this.model);
        if (rtnmsg.respCode == SUCCESS) {
          this.$notify.success("删除成功!");
          this.onSearch();
        } else {
          this.$notify.error("删除失败!");
        }
      }).catch((action) => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },

    //切换币种
    async changeSefhepcur() {
      this.model.issefhepcurexit = "N";
      let rtnmsg = await Api.post("/manager/exr/changeSefhepcur", this.model);
      if (rtnmsg.respCode === SUCCESS) {
        if(this.model.rec.pratyp == '0'){
          this.model.issefhepcurexit = rtnmsg.data.issefhepcurexit;
          this.model.globalrat = rtnmsg.data.globalrat;
        } else if(this.model.rec.pratyp == '2'){
          this.model.rec.sefheptimfrm = rtnmsg.data.rec.sefheptimfrm
          this.model.rec.sefheptimend = rtnmsg.data.rec.sefheptimend
          this.model.rec.sefhepappday = rtnmsg.data.rec.sefhepappday
          this.model.rec.sefheplmtamt = rtnmsg.data.rec.sefheplmtamt
          this.model.rec.maxlmtamt = rtnmsg.data.currat.maxlmtamt;
          this.model.currat = rtnmsg.data.currat;
          this.model.ptyrat = rtnmsg.data.ptyrat;
          this.model.globalrat = rtnmsg.data.globalrat;
          this.model.issefhepcurexit = rtnmsg.data.issefhepcurexit;
        }
      }
    },

    changeAllowlistflg() {
      this.changeSefhepcur();
    },

    //查询总行全局参数
    async getCurrat(data) {
      let params = {
        rec: {
          sefhepcur: data
        }
      };
      let rtnmsg = await Api.post("/manager/exr/getCurrat", params);
      if (rtnmsg.respCode === SUCCESS) {
        this.model.currat = rtnmsg.data.currat;
      }
    },

    //切换优惠类型
    changeSefheptyp() {
      if (this.model.rec.sefheptyp === "") {
        this.model.rec.sefheprat = "";
        this.model.rec.sefhepdatfrm = "";
        this.model.rec.sefhepdatend = "";
      }
    },

    cancel() {
      this.handleClose();
    },
    handleClose(done) {
      this.sefratDialog = false;
      if (done && typeof done === "function") {
        done();
      }
    },

    // extkey输入框失焦
    handleExtkeyBlur(e) {
      let extkey = this.model.rec.ecifno;
      if (extkey && extkey !== this.markExtkey) {
        this.showPtapDialog();
      }
    },

    // 弹框
    async showPtapDialog() {
      if (this.tableLoading || this.dialogTableVisible) {
        return;
      }
      const value = this.model.rec.ecifno;
      // 分层取值
      await this.getTableData(value);
      if (this.total == 1) {
        this.dbClickRow(this.tableList[0]);
      } else {
        // 设置弹框标题
        this.dialogTableVisible = true;
        this.$nextTick(() => {
          this.root.$refs["modelForm"].validateField([`rec.ecifno`]);
        });
      }
    },

    // 获取弹框内的table数据
    async getTableData(value) {
      return new Promise(async resolve => {
        this.tableLoading = true;
        this.tableList = [];
        // 获取table的表格数据
        let params = {
          extkey: value || "",
          bchbranch: JSON.parse(window.sessionStorage.accbch).branch,
          pageNum: this.currentPage,
          pageSize: this.pageSize
        };
        const loading = this.loading();
        const res = await Api.post("/manager/exr/querySefratInfoByExtkey", params);
        loading.close();
        if (res.respCode == SUCCESS) {
          if (res.data && res.data.list) {
            this.total = res.data.total;
            this.tableList = res.data.list;
            resolve(this.tableList);
          }
        }
        this.tableLoading = false; //接口掉完变成false
      });
    },

    async dbClickRow(row, column, event) {
      this.markExtkey = row.extkey;
      this.model.rec.ecifno = row.extkey;
      this.model.rec.cusnam = row.nam;
      this.model.rec.ptyinr = row.inr;
      this.model.pty.sigflg = row.sigflg;
      this.model.sigflg = row.sigflg;
      this.$nextTick(() => {
        this.root.$refs["modelForm"].validateField([`rec.ecifno`]);
      });
      this.dialogTableVisible = false;
      this.changeSefhepcur();
    },

    //extkey清空时
    extkeyInput() {
      if (!this.model.rec.ecifno) {
        this.model.rec.cusnam = "";
        this.markExtkey = "";
        this.clear();
      }
    },

    // 清除
    clear() {
      this.$emit("clear");
      this.$nextTick(() => {
        this.root.$refs["modelForm"].validateField([`rec.ecifno`]);
      });
    },

    beforeClose(done) {
      this.dialogTableVisible = false;
      this.currentPage = 1;
      done();
    },

    sizeChange(num) {
      this.pageSize = num;
      const value = this.model.rec.ecifno;
      this.getTableData(value);
    },

    currentChange(num) {
      this.currentPage = num;
      const value = this.model.rec.ecifno;
      this.getTableData(value);
    },
  }
};
</script>

<style scoped>
</style>