<template>
  <el-dialog
    :visible.sync="initdialog"
    :title="'交易列表'"
    append-to-body
    :before-close="beforeClose"
    @opened="opened"
  >
    <div class="m-list-btns">
      <div class="busnavbar">
        <div class="busnavbar-items">
          <c-button
            style="margin-left: 7px"
            size="medium"
            type="primary"
            class="medium_bcs"
            v-for="(item, index) in navcode"
            v-bind:key="index"
            @click.native="onNarBtnClick(item.code, item.index)"
            :title="item.title"
            :disabled="item.isDis === 'N'"
            >{{ item.label }}</c-button
          >
          <div style="margin-top: 15px;margin-left: 10px;">
          <div v-for="(str,idx) in tState" :key="idx" style="margin-bottom: 5px;font-weight: bold;">
            {{str}}
          </div>
        </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import Api from "~/service/Api";
import commonProcess from "~/mixin/commonProcess";

import _ from "~/utils/Lodash"

export default {
  props: {
    ownref: {
      required: true,
    },
    trnCode: {
      required: true,
    },
    model: {
      required: true,
    },
    ownrefPath: {
      required: false,
      default: "didgrp",
    },
    tabIndex: {
      required: false,
      default: 1,
    },
  },
  components: {},
  mixins: [commonProcess], // 里面包含了Default、Check等的公共处理
  data() {
    return {
      initdialog: false,
      navcode: [
        // {code:"",label:"",isDis:"",title:""},
      ],
      tState: []
    };
  },
  methods: {
    //各入口按钮请求
    async onNarBtnClick(code, i) {
      this.model.cfgfil[`subtrn${i}`] = code;
      let rtnmsg = await this.executeRule(`cfgfil.hotsub${i}`);
      if (rtnmsg.respCode == SUCCESS) {
        this.navcode = [];
        this.$emit("onChoose", code.toLowerCase(), this.model.pageId);
      } else {
        this.$notify.error({ title: "错误", message: "服务请求失败!" });
      }
    },
    getIndex(module) {
      for (let i = 1; i <= 12; i++) {
        var temp = this.model.cfgfil[`subtxt${i}`];
        if (module == temp) {
          return i;
        }
      }
      return 1;
    },
    opened() {
      this.tState = []
      this.$emit("childmethods");
    },
    beforeClose() {
      this.navcode = [];
      this.initdialog = false;
    },
  },
  mounted() {
    this.trnName = this.trnCode;
    this.$nextTick(function () {
      this.$on("childmethods", async function () {
        let res = await this.executeRule(`cfgfil.hotreg${this.tabIndex}`);
        if (res.respCode == SUCCESS) {
          this.updateModel(res.data);
        } else {
          this.$notify.error({
            title: "错误",
            message: "服务请求失败!",
          });
        }

        this.navcode = [];
        //请求按钮数据
        let rulePath
        if (this.ownrefPath.endsWith(".ownref")) {
          _.set(this.model, this.ownrefPath, this.ownref);
          rulePath = this.ownrefPath
        } else {
          this.model[this.ownrefPath].rec.ownref = this.ownref;
          rulePath = this.ownrefPath + ".rec.ownref"
        }
        let rtnmsg = await this.executeRule(rulePath); //didgrp_rec_ownref
        if (rtnmsg.respCode == SUCCESS) {
          //重置数组
          this.navcode = [];
          this.updateModel(rtnmsg.data);
          //this.model.cfgfil.btnstm = rtnmsg.data.cfgfil_btnstm.rows
          //给inr赋值,后面弹窗里面的按钮请求会用到
          //this.model.didgrp.rec.inr = rtnmsg.data.didgrp_rec_inr
          const length = this.model.cfgfil.btnstm.rows.length;
          let btnStr = this.model.cfgfil.btnstm.rows;
          for (let i = 0; i < length; i++) {
            let arr = btnStr[i].split("\t");
            var index = this.getIndex(arr[1]);
            let newList = {
              code: arr[0],
              label: arr[1],
              isDis: arr[2],
              title: arr[3],
              index: index,
            };
            this.navcode.push(newList);
          }
          // 交易状态信息
          const selbut = this.model.selbut ?? {}
          const arr = Object.keys(selbut)
                            .filter(key => key.startsWith("dsp"))
                            .map(key => selbut[key] || "")
                            .filter(s => s.trim()!== "")
          this.tState = arr
        } else {
          this.navcode = [];
          this.$notify.error({ title: "错误", message: "服务请求失败!" });
        }
      });
    });
  },
};
</script>
<style>
</style>