<template>
  <div class="eibs-tab">
    <c-col :span="12" class="col-left">
      <c-col :span="18">

        <el-form-item label="业务编号" prop="ownref">
          <c-input v-model="model.ownref" placeholder=""></c-input>
        </el-form-item>

      </c-col>
    </c-col>
    <!--=======- 右 ========-->
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="报文类型MT">
          <c-input v-model="model.msgtyp" placeholder=""></c-input>
        </el-form-item>
      </c-col>
    </c-col>

    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="收报行BIC">
          <c-input v-model="model.biccod" placeholder=""></c-input>
        </el-form-item>
      </c-col>
      <c-col :span="6" style="height: 32px;line-height: 32px;font-size: 12px;">(11位)</c-col>
    </c-col>
    <!--======= 右 ========= -->
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="超长报的第">
          <c-input v-model="model.ord" placeholder=""></c-input>
        </el-form-item>
      </c-col>
      <c-col :span="6" style="height: 32px;line-height: 32px;font-size: 12px;">份报文</c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="报文流水号">
          <c-input v-model="model.msgref" placeholder=""></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="查询类型">
          <c-select v-model="model.msgsta" placeholder="" :code="getCodesByKey('msgsta1')"></c-select>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="业务授权时间">
          <el-date-picker type="date" v-model="model.begdat" placeholder="" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="至">
          <el-date-picker type="date" v-model="model.enddat" placeholder="" value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="24">
      <c-col :span="12" class="col-left">
        <c-col :span="18">
          <el-form-item label="发送渠道">
            <c-select  v-model="model.chl"  placeholder="" :code="getCodesByKey('gimchl')"></c-select>
          </el-form-item>
        </c-col>
    </c-col>
    </c-col>
    <c-col :span="24">
      <c-col style="text-align: 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>
      </c-col>
    </c-col>
    <c-col :span="24">
      <el-table ref="table" :data="emeData" style="width: 100%" class="eContainer-table" :header-cell-style="{
            background: 'rgb(235, 235, 235)',
            color: 'rgb(51, 51, 51)',
        }" :highlight-current-row="true" :border="true" max-height="380">
        <c-table-column v-for="(item, key) in emeColumns" :key="key" :prop="item.prop" :label="item.label"
          :width="item.width" sortable>
          <template v-slot="{ scope }">
            <span>{{ scope.row[item.prop] }}</span>
          </template>
        </c-table-column>
        <c-table-column fixed="right" prop="op" label="操作" width="120px">
          <template slot-scope="{ scope }">
            <c-button style="margin-left: 5px" size="small" type="primary" @click="rptInfo(scope.row)">详情</c-button>
          </template>
        </c-table-column>
        <slot></slot>
      </el-table>
    </c-col>
    <span>详细数据:</span>
    <el-divider></el-divider>
    <c-col :span="24">
      <c-col :span="12" style="text-align: left">
      </c-col>
      <c-col :span="12" style="text-align: right">
        <el-button size="small" @click="comfirm" ref="dspConfirmBtn" :loading="conLoading" v-if="isShowConBtn" :disabled="isDisabled">确认
        </el-button>
        <el-button type="primary" size="small" ref="dspTransGispBtn" :loading="trnGispLoading" v-if="isShowTrnGispBtn" @click="transformGisp"
          :disabled="isDisabledGisp">转发GISP</el-button>
        <el-button type="primary" size="small" ref="dspTransBtn" :loading="trnLoading" v-if="isShowTrnBtn" @click="transform"
          :disabled="isDisabled2">转发SWIFT</el-button>
        
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="INR">
          <c-input v-model="model2.inr" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    
    <!--======= 右 -=======-->
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="报文类型MT">
          <c-input v-model="model2.msgtyp" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="业务编号">
          <c-input v-model="model2.ownref" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <!--======= 右 =======-->
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="发报时间">
          <c-input v-model="model2.credat" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="银行BIC">
          <c-input v-model="model2.biccod" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <!-- ====== 右 ======= -->
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="报文流水号">
          <c-input v-model="model2.msgref" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="15">
        <el-form-item label="第">
          <c-input v-model="model2.ord" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
      <c-col :span="9" style="height: 32px;line-height: 32px;font-size: 12px;">份报</c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="15">
        <el-form-item label="共">
          <c-input v-model="model2.tot" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
      <c-col :span="9" style="height: 32px;line-height: 32px;font-size: 12px;">份报</c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="银行BIC">
          <c-input v-model="model2.biccod" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="发报时间">
          <c-input v-model="model2.credat" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="18">
        <el-form-item label="机构名称">
          <c-input v-model="model2.bchname" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-right">
      <c-col :span="18">
        <el-form-item label="报文状态">
          <c-input v-model="model2.msgsta" placeholder="" :disabled="true"></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="24">
      <c-col :span="20">
        <el-form-item label="操作备注">
          <c-input v-model="model2.rmktxt" placeholder=""></c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="24">
      <el-table ref="table" :data="emeData2" style="width: 100%" class="eContainer-table" :header-cell-style="{
            background: 'rgb(235, 235, 235)',
            color: 'rgb(51, 51, 51)',
        }" :highlight-current-row="true" :border="true" max-height="380">
        <c-table-column v-for="(item, key) in emeColumns2" :key="key" :prop="item.prop" :label="item.label"
          :width="item.width" sortable>
          <template v-slot="{ scope }">
            <span>{{ scope.row[item.prop] }}</span>
          </template>
        </c-table-column>
        <slot></slot>
      </el-table>
    </c-col>

    <el-dialog :visible.sync="dialogAdd" :title=dialogNam[ofctyp] :modal-append-to-body="false" :lock-scroll="false"
      :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" v-dialogDrag>
      <el-form ref="form" :model="form" label-suffix=":">
        <c-col :span="24">
          <c-col :span="12">
            <el-form-item label="业务编号" :label-width="formLabelWidth">
              <c-input v-model="form.biccod" autocomplete="off" placeholder="" :disabled="true"></c-input>
            </el-form-item>
          </c-col>
          <c-col :span="12">
            <el-form-item label="报文类型" :label-width="formLabelWidth">
              <c-input v-model="form.msgtyp" placeholder="" :disabled="true"></c-input>
            </el-form-item>
          </c-col>
        </c-col>
        <c-col :span="24">
          <c-col :span="12">
            <el-form-item label="交易流水号" :label-width="formLabelWidth">
              <c-input v-model="form.trninr" placeholder="" :disabled="true"></c-input>
            </el-form-item>
          </c-col>
          <c-col :span="12">
            <c-col :span="18">
              <el-form-item label="第" :label-width="formLabelWidth">
                <c-input v-model="form.ord" placeholder="" :disabled="true"></c-input>
              </el-form-item>
            </c-col>
            <c-col :span="6" style="height: 32px;line-height: 32px;font-size: 12px;"> 份报</c-col>
          </c-col>
        </c-col>
        <c-col :span="24">
          <c-col :span="12">
            <el-form-item label="报文名称" :label-width="formLabelWidth">
              <c-input v-model="form.docfil" placeholder="" :disabled="true"></c-input>
            </el-form-item>
          </c-col>
        </c-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="iscomfirm" :loading="confirmLoading" v-if="ofctyp == 'comfirm'">确 认</el-button>
        <el-button type="primary" @click="istransform" :loading="transformLoading" v-if="ofctyp == 'transform'">确 认</el-button>
        <el-button type="primary" @click="istransformGisp" :loading="transformGispLoading" v-if="ofctyp == 'transformFTG'">确 认</el-button>
        <el-button @click="dialogAdd = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    queryByPage,
    queryByMsgSnd,
    queryById,
    comfirmmsg,
    transformmsg,
    transformmsgGisp,
  } from "~/service/manage/chlrpt.js";
  import Chlrpt from "./Chlrpt.js";
  import Gimege, {
    Pattern
  } from "./Gimege.js";
  import codes from "~/config/CodeTable";
  import commonFunctions from '~/mixin/commonFunctions.js';

  export default {
    props: ["data"],
    inject: ["root"],
    mixins: [commonFunctions],
    data() {
      return {
        rules: Pattern,
        rules1: {
          ownref: [{
              required: false,
              message: '请输入业务编号',
              trigger: 'blur'
            },
            {
              max: 16,
              message: '最多16个字符',
              trigger: 'blur'
            }
          ]


        },
        model: new Chlrpt().data,
        model2: new Chlrpt().data,
        emeData: [],
        emeColumns: [{
            label: '业务编号',
            prop: 'ownref',
            width: 'auto'
          },
          {
            label: '交易流水号',
            prop: 'trninr',
            width: 'auto'
          },
          {
            label: '收报行BIC',
            prop: 'biccod',
            width: 'auto'
          },
          {
            label: '发送渠道',
            prop: 'chlflg',
            width: 'auto'
          },
          {
            label: 'MT',
            prop: 'msgtyp',
            width: 'auto'
          },
          {
            label: '报文处理状态',
            prop: 'msgsta',
            width: 'auto'
          },
          {
            label: '第几份报文',
            prop: 'ord',
            width: 'auto'
          },
          {
            label: '报文总数',
            prop: 'tot',
            width: 'auto'
          },
          {
            label: '发报时间',
            prop: 'credat',
            width: 'auto'
          },
          {
            label: '机构编号',
            prop: 'branch',
            width: 'auto'
          },
          {
            label: '机构名称',
            prop: 'bchname',
            width: 'auto'
          },
          {
            label: '报文名称',
            prop: 'docfil',
            width: 'auto'
          },
          {
            label: '操作柜员',
            prop: 'usr',
            width: 'auto'
          },
          {
            label: '交易授权时间',
            prop: 'cpldattim',
            width: 'auto'
          },
          {
            label: '关联面函信息表主键',
            prop: 'smhinr',
            width: 'auto'
          },
        ],
        emeData2: [],
        emeColumns2: [{
            label: 'INR',
            prop: 'inr',
            width: 'auto'
          },
          {
            label: '业务编号',
            prop: 'ownref',
            width: 'auto'
          },
          {
            label: '通道',
            prop: 'chlflg',
            width: 'auto'
          },
          {
            label: '报文类型',
            prop: 'msgtyp',
            width: 'auto'
          },
          {
            label: '第几份',
            prop: 'ord',
            width: 'auto'
          },
          {
            label: '共几份',
            prop: 'tot',
            width: 'auto'
          },
          {
            label: '发报时间',
            prop: 'credat',
            width: 'auto'
          },
          {
            label: '收报行BIC',
            prop: 'biccod',
            width: 'auto'
          },
          {
            label: '转发人',
            prop: 'usr',
            width: 'auto'
          },
          {
            label: '报文流水号',
            prop: 'msgref',
            width: 'auto'
          },
          {
            label: '报文名称',
            prop: 'docfil',
            width: 'auto'
          },
          {
            label: '关联面函信息表主键',
            prop: 'smhinr',
            width: 'auto'
          },
          {
            label: '原G发报流水号',
            prop: 'smhsubinr',
            width: 'auto'
          },
          {
            label: '交易流水号',
            prop: 'trninr',
            width: 'auto'
          },
        ],
        dialogNam: {
          'comfirm': '报文确认',
          'transform': '报文转发Swift',
          'transformFTG': '报文转发Gisp',
        },
        dialogAdd: false,
        searchLoading: false,
        isShowConBtn: true,
        isShowTrnBtn: true,
        isShowTrnGispBtn: true,
        form: new Chlrpt().data,
        ofctyp: "",
        formLabelWidth: "100px",
        confirmLoading: false,
        transformLoading: false,
        transformGispLoading: false,
        conLoading: false,
        trnLoading: false,
        trnGispLoading: false,
      };
    },
    computed: {
      isDisabled() {
        return this.model2.cfmflg == null || this.model2.cfmflg == "" || this.model2.cfmflg != 'Y';
      },
      isDisabled2() {
        return this.model2.fwdflg == null || this.model2.fwdflg == "" || this.model2.fwdflg != 'Y';
      },
      isDisabledGisp(){
        return this.model2.gispflg == null || this.model2.gispflg == "" || this.model2.gispflg != 'Y';
      }
    },
    created() {
      this.model.begdat = this.getNowDate() + ' ' + '00:00:00';
      this.model.enddat = this.getNowDate() + ' ' + '00:00:00';
      this.model.msgsta = "D";
    },
    methods: {
      handleReset() {
        this.model = new Chlrpt().data;
        this.model2 = new Chlrpt().data;
        this.emeData.length = 0;
        this.emeData2.length = 0;
        this.model.begdat = this.getNowDate() + ' ' + '00:00:00';
        this.model.enddat = this.getNowDate() + ' ' + '00:00:00';
        this.model.msgsta = "D";
      },
      onSearch() {
        if(this.model.chl == ""){
          this.$notify.error("发送渠道必输");
          return;
        }
        if (this.model.ownref != "" && this.model.ownref.length > 16) {
          this.$notify.error("业务编号不得超过16位");
          return;
        }

        if (this.model.msgtyp != "" && this.model.msgtyp.length > 10) {
          this.$notify.error("报文类型不得超过10位");
          return;
        }

        if (this.model.biccod != "" && (this.model.biccod.length < 8 || this.model.biccod.length > 11)) {
          this.$notify.error("收报行BIC请输入8~11位");
          return;
        }

        if (this.model.msgref != "" && this.model.msgref.length > 40) {
          this.$notify.error("报文流水号不得超过40位");
          return;
        }

        this.onGimemeSearch();
      },
      rptInfo(row) {
        //去除分页导致的脏数据查多条错误
        row.pageSize = 0;
        this.queryById(row);
        this.onGimemeSearch2(row);
      },
      queryById(row) {
        queryById(row).then(res => {
          this.model2 = res.data
        })
      },
      comfirm() {
        this.conLoading = true;
        this.dialogAdd = true;
        this.ofctyp = "comfirm";
        queryById(this.model2).then(res => {
          this.form = res.data
          this.conLoading = false;
        })
        .catch((err) => {
          this.conLoading = false;
        });
      },
      transform() {
        this.trnLoading = true;
        this.dialogAdd = true;
        this.ofctyp = "transform";
        this.model2.pageSize = 0;
        queryById(this.model2).then(res => {
          this.form = res.data;
          this.trnLoading = false;
        })
        .catch((err) => {
          this.trnLoading = false;
        });
      },
      transformGisp(){
        this.trnGispLoading = true;
        this.dialogAdd = true;
        this.ofctyp = "transformFTG";
        this.model2.pageSize = 0;
        queryById(this.model2).then(res => {
          this.form = res.data;
          this.trnGispLoading = false;
        })
        .catch((err) => {
          this.trnGispLoading = false;
        });
      },
      iscomfirm() {
        this.confirmLoading = true;
        comfirmmsg(this.form)
          .then((res) => {
            this.$notify.success("确认成功,进入可转发队列!");
            this.dialogAdd = false;
            this.clearModel2();
            this.onSearch();
            this.confirmLoading = false;
          })
          .catch((err) => {
            this.$notify.error("确认失败!");
            this.confirmLoading = false;
          });
      },
      istransformGisp(){
        this.transformGispLoading = true;
        transformmsgGisp(this.form).then((res) => {
            if (res.respCode == 'AAAAAA') {
              if (res.data.rtnCod =="Y"){
                this.$notify.success("转发Gisp成功!");
                this.dialogAdd = false;
                this.clearModel2();
              }else{
                this.$notify.error("转发Gisp失败!"+res.data.rtnMsg);
              }

              //this.$notify.success("转发SWIFT成功!");
              //this.dialogAdd = false;
              //this.clearModel2();
              this.transformGispLoading = false;
            } else {
              this.transformGispLoading = false;
            }
          })
          .catch((err) => {
            this.transformGispLoading = false;
            this.$notify.error("转发Gisp异常!");
          });

        
      },
      istransform() {
        this.transformLoading = true;
        transformmsg(this.form)
          .then((res) => {
            if (res.respCode == 'AAAAAA') {
              if (res.data.rtnCod =="Y"){
                this.$notify.success("转发SWIFT成功!");
                this.dialogAdd = false;
                this.clearModel2();
              }else{
                this.$notify.error("转发SWIFT失败!"+res.data.rtnMsg);
              }

              //this.$notify.success("转发SWIFT成功!");
              //this.dialogAdd = false;
              //this.clearModel2();
              this.transformLoading = false;
            } else {
              this.transformLoading = false;
            }
          })
          .catch((err) => {
            this.transformLoading = false;
            this.$notify.error("转发SWIFT异常!");
          });
      },
      clearModel2() {
        this.model2 = new Chlrpt().data;
      },
      getCodesByKey(key) {
        return codes[key] || [];
      },
      onGimemeSearch() {
        const loading = this.loading();
        this.searchLoading = true;
        this.clearModel2();
        if (this.model.msgsta == "") {
          this.$notify.error("查询类型为空,请选择");
        } else if (this.model.msgtyp != "" && this.model.msgtyp.length != 3) {
          this.$notify.error("请填写三位的报文类型数字码");
        } else {
          queryByPage(this.model).then(res => {
              const list = res.data.list
              this.emeData = list
              this.searchLoading = false;
              loading.close()
            })
            .catch((err) => {
              this.searchLoading = false;
              loading.close()
              this.$notify.error("查询失败!");
            });
        }
      },
      getNowDate() {
        const timeOne = new Date()
        const year = timeOne.getFullYear()
        let month = timeOne.getMonth() + 1
        let day = timeOne.getDate()
        month = month < 10 ? '0' + month : month
        day = day < 10 ? '0' + day : day
        const NOW_MONTHS_AGO = `${year}-${month}-${day}`
        return NOW_MONTHS_AGO
      },
      onGimemeSearch2(row) {
        queryByMsgSnd(row).then(res => {
          const list = res.data.list
          this.emeData2 = list
        })
      },
    },
  };
</script>

<style scoped>
  .table-button-item-list {
    padding: 0;
    margin: 0;
  }

  .table-button-item-list li {
    list-style: none;
    padding: 5px 0;
    text-align: center;
    color: #606266;
    cursor: pointer;
  }
</style>