<template>
  <div class="currentPage">
    <el-form :model="trnAuthority" ref="trnAuthorityForm" label-width="150px" label-position="right" label-suffix=":"
      size="small">
      <c-row>
        <c-col :span="11">
          <c-form-item label="交易代码" prop="trn">
            <c-select dbCode="ATPTXT" :isShowKeyAndLabel="true" filterable event-render="loadText"
              v-model="trnAuthority.trn" type="text" />
          </c-form-item>
        </c-col>
        <c-col :span="11">
          <c-form-item label="授权类型" prop="relflg">
            <c-select v-model="trnAuthority.relflg" style="width: 100%" dbCode="RELTXT">
            </c-select>
          </c-form-item>
        </c-col>
      </c-row>
      <c-row>
        <c-col :span="11">
          <c-form-item label="授权币种" prop="relcur">
            <c-select v-model="trnAuthority.relcur" style="width: 100%" dbCode="CURTXT">
            </c-select>
          </c-form-item>
        </c-col>
        <c-col :span="11">
          <c-form-item label="允许授权的最大金额" prop="relamt">
            <c-input event-render="loadText" v-model.number="trnAuthority.relamt" type="text" />
          </c-form-item>
        </c-col>
      </c-row>
      <div class="text-center">
        <c-button type="primary" icon="el-icon-search" :loading="searchLoading" style="margin-right:50px;margin-bottom:25px"
          @click="searchTable()">
          搜索
        </c-button>
        <c-button type="primary" :loading="false" icon="el-icon-refresh" native-type="reset"
          @click="resetFormFields('trnAuthorityForm')">
          重置
        </c-button>
      </div>
      <div style="height:40px;margin-top:-20px;">
        <c-button type="primary" size="mini" @click="addInfo()">新增</c-button>
      </div>
    </el-form>
    <c-paging-table :data="tableData" ref="trnAuthorityTableRef" :pageSize="trnAuthority.pageSize"
      :pageNumber="trnAuthority.pageNum" :total="trnAuthority.total" stripe @queryFunc="queryFunc" :border="true">
      <el-table-column label="inr" prop="inr" v-if="false">
      </el-table-column>
      <el-table-column label="机构编码" prop="bchkey" min-width="12%">
        <template slot-scope="scope">{{scope.row.bchkey}}</template>
      </el-table-column>
      <el-table-column label="交易代码" prop="trn" min-width="12%">
        <template slot-scope="scope">{{scope.row.trn}}</template>
      </el-table-column>
      <el-table-column label="授权币种" prop="relcur" min-width="12%">
        <template slot-scope="scope">
          <span>{{scope.row.relcur}}</span>
        </template>
      </el-table-column>
      <el-table-column label="允许授权的最大金额" prop="relamt" sortable min-width="22%">
        <template slot-scope="scope">
          <span>{{scope.row.relamt}}</span>
        </template>
      </el-table-column>
      <el-table-column label="授权类型" prop="relflg" min-width="20%" :formatter="relflgFormat">
      </el-table-column>
      <!-- <el-table-column label="版本号" prop="ver" min-width="12%">
				<template slot-scope="scope">
					<span>{{scope.row.ver}}</span>
				</template>
			</el-table-column> -->
      <el-table-column label="操作" min-width="22%">
        <template slot-scope="scope">
          <el-button @click="getInfo(scope.row.inr)" type="info" size="mini">详情</el-button>
          <el-button @click="updateInfo(scope.row.inr)" type="primary" size="mini">修改</el-button>
          <el-button @click="deleteInfo(scope.row.inr)" type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </c-paging-table>
    <el-dialog :visible.sync="trnAuthorityDialogFormVisible" :title="title" :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="trnAuthorityDialogForm" :model="trnAuthorityDialog" label-width="120px" label-position="right"
        label-suffix=":" :rules="rules">
        <el-row style="margin-top:20px;">
          <el-col :span="22">
            <el-form-item label="交易代码" prop="trn">
              <c-select dbCode="ATPTXT" :isShowKeyAndLabel="true" filterable event-render="loadText" type="text"
                v-model="trnAuthorityDialog.trn" :disabled="trnAuthorityDialogFieldsDisabled" maxlength="6"></c-select>
            </el-form-item>
          </el-col>

        </el-row>


        <el-row>
          <el-col :span="22">
            <el-form-item label="授权类型" prop="relflg">
              <c-select v-model="trnAuthorityDialog.relflg" style="width: 100%"
                :disabled="trnAuthorityDialogFieldsDisabled" dbCode="RELTXT">
              </c-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
            <el-form-item label="授权币种" prop="relcur">
              <c-select v-model="trnAuthorityDialog.relcur" style="width: 100%"
                :disabled="trnAuthorityDialogFieldsDisabled" dbCode="CURTXT">
              </c-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>

          <el-col :span="22">
            <el-tooltip effect="dark" content="允许授权的最大金额" placement="bottom-start">
              <el-form-item label="授权金额" prop="relamt">
                <el-input event-render="loadText" type="text" v-model.number="trnAuthorityDialog.relamt"
                  :disabled="trnAuthorityDialogFieldsDisabled" maxlength="15"></el-input>
              </el-form-item>
            </el-tooltip>
          </el-col>
        </el-row>
        <!-- <el-row>
					<el-col :span="11">
						<el-form-item label="版本号" prop="ver">
							<el-input event-render="loadText" type="text" v-model="trnAuthorityDialog.ver" :disabled="trnAuthorityDialogFieldsDisabled" maxlength="4"></el-input>
						</el-form-item>
					</el-col>
				</el-row> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="submitDialog" v-show="dialogSubBtnVisible" :loading="loadingStatus">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import DeptModel, {
    Pattern
  } from "./DeptModel.js";
  import {
    queryTrnAuthorityByPage,
    getTrnAuthorityInfo,
    saveTrnAuthorityInfo,
    deleteTrnAuthorityInfo,
    getBchInfo
  } from "~/service/manage/dept.js";
  import Utils from "~/utils";
  export default {
    props: {
      orgBranch: String
    },
    data() {
      return {
        tableData: null,
        deleteInr: null,
        branch: null,
        title: "查看交易权限",
        rules: Pattern,
        trnAuthority: new DeptModel().data.trnAuthority,
        trnAuthorityDialog: new DeptModel().data.trnAuthority,
        trnAuthorityDialogFormVisible: false,
        dialogSubBtnVisible: true,
        trnAuthorityDialogFieldsDisabled: false,
        loadingStatus: false,
        searchLoading: false
      }
    },
    watch: {
      // 会出现父组件传真,子组件更新不及时的情况
      orgBranch: {
        handler: function (newValue, oldValue) {
          if (newValue != '' && newValue != null && newValue != 'undefined') {
            this.branch = this.orgBranch;
            this.searchTable();
          }
        },
        // 强制立即执行回调
        immediate: true
      }
    },
    methods: {
      // 【搜索】按钮
      searchTable() {
        this.searchLoading = true;
        this.trnAuthority.bchkey = this.branch;
        queryTrnAuthorityByPage(this.trnAuthority)
          .then((res) => {
            this.tableData = res.data.list;
            this.trnAuthority.total = res.data.total;
            this.trnAuthority.pageNum = res.data.pageNumber;
            this.trnAuthority.pageSize = res.data.pageSize;
            this.searchLoading = false;
          })
          .catch((err) => {
            this.searchLoading = false;
            this.$notify.error("查询失败!");
          });
      },

      // 【重置】按钮
      resetFormFields(formName) {
        this.$refs[formName].resetFields();
      },

      // 数据格式化
      relflgFormat(row, column) {
        if (row.relflg == '1') {
          //return '经办行经办、复核、总经理,总行经办、复核';
          return '无论金额大小均须总行看,经办行为经办、复核、总经理三人操作,总行为经办、复核两人操作';
        }
        if (row.relflg == '2') {
          //return '经办行经办、复核、总经理,(总行经办、复核)';
          return '超过一定金额一定须总行看,不超过一定金额不须总行看, 但是分行总经理需要授权';
        }
        if (row.relflg == '3') {
          //return '经办行经办、复核、(总经理)';
          return '无须总行看,未超过一定金额只需经办、复核两人操作,超过一定金额需经办、复核、总经理三人操作';
        }
        if (row.relflg == '4') {
          //return 'OB两人';
          return '需要OB两人即可授权业务';
        }
        if (row.relflg == '5') {
          //return '经办行经办、复核,总行经办、复核';
          return '无论金额大小均须总行看,经办行为经办、复核两人操作,总行为经办、复核两人操作';
        }
        if (row.relflg == '' || row.relflg == ' ' || row.relflg == 'Empty') {
          //return '经办行经办、总经理';
          return '无需总行看,经办行为经办、总经理两人操作OA';
        }
      },

      // 【新增】按钮
      addInfo() {
        // 选中节点后操作
        if (!(this.branch != '' && this.branch != null)) {
          this.$message({
            message: '请选中具体分行、支行再操作!',
            center: true,
            type: 'warning'
          });
          return;
        }
        getBchInfo(this.branch)
          .then((res) => {
            if (res.respCode == "AAAAAA") {
              var bch = res.data;
              if (bch == null) {
                this.$message({
                  message: '请先在【国结机构信息】页签中新增该机构信息!',
                  center: true,
                  type: 'warning'
                });
                return;
              }
              if (bch.lev == "0" || bch.lev == "A" || bch.lev == "B") {
                this.$message({
                  message: '请选中具体的分行、支行再操作!',
                  center: true,
                  type: 'warning'
                });
                return;
              }
              this.title = "新增交易权限信息";
              this.trnAuthorityDialogFormVisible = true;
              this.dialogSubBtnVisible = true;
              this.trnAuthorityDialogFieldsDisabled = false;
              this.clearTrnAuthorityDialogModel();
            } else {
              this.$notify.error("获取机构号:[" + this.branch + "]机构信息失败!");
              return;
            }
          })
          .catch((res) => {
            this.$notify.error("获取机构号:[" + this.orgBranch + "]机构信息失败!");
          });

      },

      // 清空model数据
      clearTrnAuthorityDialogModel() {
        this.trnAuthorityDialog.inr = '';
        this.trnAuthorityDialog.bchkey = '';
        this.trnAuthorityDialog.trn = '';
        this.trnAuthorityDialog.relcur = '';
        this.trnAuthorityDialog.relamt = 0;
        this.trnAuthorityDialog.ver = '';
        this.trnAuthorityDialog.relflg = '';
      },

      // 【详情】按钮
      getInfo(inr) {
        this.title = "查看交易权限详情信息";
        this.trnAuthorityDialogFormVisible = true;
        this.dialogSubBtnVisible = false;
        this.trnAuthorityDialogFieldsDisabled = true;
        this.getDetailInfo(inr);
      },

      // 【修改】按钮
      updateInfo(inr) {
        this.title = "修改交易权限信息";
        this.trnAuthorityDialogFormVisible = true;
        this.dialogSubBtnVisible = true;
        this.trnAuthorityDialogFieldsDisabled = false;
        this.getDetailInfo(inr);
      },

      // 查看详情信息
      getDetailInfo(inr) {
        this.trnAuthority.inr = inr;
        getTrnAuthorityInfo(this.trnAuthority)
          .then((res) => {
            this.trnAuthorityDialog.inr = res.data.inr;
            this.trnAuthorityDialog.bchkey = res.data.bchkey;
            this.trnAuthorityDialog.trn = res.data.trn;
            this.trnAuthorityDialog.relcur = res.data.relcur;
            this.trnAuthorityDialog.relamt = res.data.relamt;
            this.trnAuthorityDialog.ver = res.data.ver;
            if (res.data.relflg == '' || res.data.relflg == ' ' || res.data.relflg == 'Empty') {
              this.trnAuthorityDialog.relflg = ' ';
            } else {
              this.trnAuthorityDialog.relflg = res.data.relflg;
            }
          })
          .catch((err) => {
            this.$notify.error("查询失败!");
          });
      },

      // 【删除】按钮
      deleteInfo(inr) {
        this.deleteInr = inr;
        this.$confirm("是否确认删除?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            this.deleteDialogInfo();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },

      // 【取消】按钮
      cancelDialog() {
        // 关闭弹框
        this.trnAuthorityDialogFormVisible = false;
        this.clearTrnAuthorityDialogModel();
        this.$refs['trnAuthorityDialogForm'].resetFields();
      },

      // 【提交】按钮
      submitDialog() {
        this.$refs.trnAuthorityDialogForm.validate((validated) => {
          if (validated) {
            this.loadingStatus = true;
            this.trnAuthorityDialog.bchkey = this.branch;
            saveTrnAuthorityInfo(this.trnAuthorityDialog)
              .then((res) => {
                if (res.respCode == 'AAAAAA') {
                  this.$notify.success("保存成功!");
                } else {
                  this.$notify.error(res.respMsg);
                }
                // 关闭弹框
                this.cancelDialog();
                // 刷新列表
                this.searchTable();
                this.loadingStatus = false;
              })
              .catch((err) => {
                this.loadingStatus = false;
                this.$notify.error("保存失败!");
              });
          } else {
            Utils.formValidateTips(this.$refs.trnAuthorityDialogForm.fields)
          }
        })
      },

      // 获取分页组件数据,为请求数据赋值
      queryFunc(pageNumber, pageSize) {
        this.trnAuthority.pageNum = pageNumber;
        this.trnAuthority.pageSize = pageSize;
        this.searchTable();
      },

      // 【删除弹窗-确定】按钮
      deleteDialogInfo() {
        this.trnAuthorityDialog.inr = this.deleteInr;
        deleteTrnAuthorityInfo(this.trnAuthorityDialog)
          .then((res) => {
            this.$notify.success("删除成功!");
            // 刷新列表
            this.searchTable();
            // 清空待删除的ID
            this.deleteInr = null;
          })
          .catch((err) => {
            this.$notify.error("删除失败!");
          });
      }
    }
  }
</script>
<style lang="less" scoped>
  .currentPage {
    .el-table--enable-row-hover .el-table__body tr:hover>td {
      background-color: #c694dc !important;
    }
  }
</style>