<template>
  <div class="eibs-tab">
    <!-- ----------左 ---------->
    <c-col :span="12" class="col-left">
      <c-col :span="24">
        <el-form-item :label="$t('statics.机构代码')" prop="branch">
          <c-input v-model="model.branch" style="width: 100%" >
          </c-input>
        </el-form-item>
      </c-col>
    </c-col>
    <c-col :span="12" class="col-left">
      <c-col :span="24">
        <el-form-item :label="$t('statics.机构名称')" prop="bchname">
          <c-input v-model="model.bchname"  filterable></c-input>
        </el-form-item>
      </c-col>
    </c-col>

    <c-col :span="24">
      <span style="float: left">
        <el-button type="primary" size="small" ref="bchInsertBtn" v-if="true" @click="bchAdd">{{ $t('statics.新增') }}</el-button>
      </span>
      <span style="float: right">
        <el-button size="small" @click="handleReset">{{ $t('statics.重置') }}</el-button>
        <el-button type="primary" icon="el-icon-search" size="small" :loading="searchLoading" @click="onSearch()">{{ $t('statics.查询') }}
        </el-button>
      </span>
    </c-col>

    <c-col :span="24">
      <c-paging-table :data="bchData" :columns="bchColumns" :pageNumber="model.pageNum" :pageSize="model.pageSize"
        :total="model.total" v-on:queryFunc="queryFunc" :border="true" @sort-change="sortChange" sortable="custom">
        <c-table-column fixed="right" prop="op" :label="$t('statics.操作')" width="240px">
          <template slot-scope="{ scope }">
            <c-button style="margin-left: 5px" type='' size="small" ref="bchDisplayBtn" @click="bchInfo(scope.row)">
              {{ $t('statics.详情') }}</c-button>
            <c-button style="margin-left: 5px" type='primary' size="small" v-if="true" ref="bchUpdateBtn"
              @click="bchEdit(scope.row)">{{ $t('statics.修改') }}</c-button>
            <c-button style="margin-left: 5px" type='primary' size="small" v-if="true" ref="bchDeleteBtn"
              @click="bchDelete(scope.row)">{{ $t('statics.删除') }}</c-button>
          </template>
        </c-table-column>
      </c-paging-table>
    </c-col>

    <el-dialog :title="dialogNam[operaTyp]" :visible.sync="dialogFlg" :modal-append-to-body="false"
      :close-on-click-modal="false" width="70%">
      <el-form :model="dataForm" :rules="rules" ref="dataForm" label-suffix=":" :disabled="isDisabled">
        
        <c-col :span="12" style="padding-right: 20px;">

          <c-col :span="20">
            <el-form-item :label="$t('statics.核心机构编码')" :label-width="formLabelWidth" prop="accbch">
              <c-input v-model="dataForm.accbch" autocomplete="off" :disabled="operaTyp == 'edit'">
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.机构类型')" :label-width="formLabelWidth" prop="bchtyp">
              <c-select v-model="dataForm.bchtyp" autocomplete="off" dbCode="bchtyp" :isCache="false"></c-select>
            </el-form-item>
          </c-col>

           <c-col :span="20">
            <el-form-item :label="$t('statics.机构中文名称')" :label-width="formLabelWidth" prop="bchname">
              <c-input v-model="dataForm.bchname" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

           <c-col :span="20">
            <el-form-item :label="$t('statics.业务编号标识')" :label-width="formLabelWidth" prop="bchflg">
              <c-input v-model="dataForm.bchflg" autocomplete="off" dbCode="state"  clearable >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.电话')" :label-width="formLabelWidth" prop="tel">
              <c-input v-model="dataForm.tel" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.中文地址')" :label-width="formLabelWidth" prop="adr">
              <c-input v-model="dataForm.adr" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.英文地址')" :label-width="formLabelWidth" prop="adren">
              <c-input v-model="dataForm.adren" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.联行行号')" :label-width="formLabelWidth" prop="lhanum">
              <c-input v-model="dataForm.lhanum" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.电证联行行号')" :label-width="formLabelWidth" prop="dzlhhh">
              <c-input v-model="dataForm.dzlhhh" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.核心记账柜员')" :label-width="formLabelWidth" prop="bchusr">
              <c-input v-model="dataForm.bchusr" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.自贸区标志')" :label-width="formLabelWidth" prop="zmqflg">
              <c-select v-model="dataForm.zmqflg" dbCode="zmqflg" autocomplete="off" placeholder="" >
              </c-select>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.机构负责人')" :label-width="formLabelWidth" prop="bchfzr">
              <c-input v-model="dataForm.bchfzr" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.业务编号机构编码')" :label-width="formLabelWidth" prop="bchref">
              <c-input v-model="dataForm.bchref" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>
        </c-col>

        <c-col :span="12" style="padding-right: 20px;">

          <c-col :span="20">
            <el-form-item :label="$t('statics.国结机构编码')" :label-width="formLabelWidth" prop="branch">
              <c-input v-model="dataForm.branch" autocomplete="off" :disabled="operaTyp == 'edit'">
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.机构级别')" :label-width="formLabelWidth" prop="lev">
              <c-select v-model="dataForm.lev" autocomplete="off" dbCode="bchlev" :isCache="false"></c-select>
            </el-form-item>
          </c-col>

           <c-col :span="20">
            <el-form-item :label="$t('statics.机构英文名称')" :label-width="formLabelWidth" prop="namen">
              <c-input v-model="dataForm.namen" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

           <c-col :span="20">
            <el-form-item :label="$t('statics.金融机构编码')" :label-width="formLabelWidth" prop="decnum">
              <c-input v-model="dataForm.decnum" autocomplete="off" placeholder="" dbCode="state"  clearable >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.传真')" :label-width="formLabelWidth" prop="fax">
              <c-input v-model="dataForm.fax" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.中文地址2')" :label-width="formLabelWidth" prop="adr2">
              <c-input v-model="dataForm.adr2" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.英文地址2')" :label-width="formLabelWidth" prop="adren2">
              <c-input v-model="dataForm.adren2" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.联行名称')" :label-width="formLabelWidth" prop="lhanam">
              <c-input v-model="dataForm.lhanam" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.电证联行名称')" :label-width="formLabelWidth" prop="dzlhmc">
              <c-input v-model="dataForm.dzlhmc" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.核心记账柜员2')" :label-width="formLabelWidth" prop="bchusr2">
              <c-input v-model="dataForm.bchusr2" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.所属全辖机构')" :label-width="formLabelWidth" prop="qxbranch" :rules="[{required: dataForm.branch != dataForm.upbranch, message: this.$t('components.必输项') }]">
              <c-input v-model="dataForm.qxbranch"  autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.外汇局印单代码')" :label-width="formLabelWidth" prop="ydjcod">
              <c-input v-model="dataForm.ydjcod" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>

          <c-col :span="20">
            <el-form-item :label="$t('statics.BIC编码')" :label-width="formLabelWidth" prop="swfcod">
              <c-input v-model="dataForm.swfcod" autocomplete="off" placeholder="" >
              </c-input>
            </el-form-item>
          </c-col>
        </c-col>
        

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="commitAdd" v-if="operaTyp == 'add'">{{ $t('statics.提 交') }}</el-button>
        <el-button type="primary" @click="commitEdit" v-if="operaTyp == 'edit'">{{ $t('statics.提 交') }}</el-button>
        <el-button @click="dialogFlg = false" v-if="operaTyp != 'info'">{{ $t('statics.取 消') }}</el-button>
        <el-button @click="dialogFlg = false" v-if="operaTyp == 'info'">{{ $t('statics.返 回') }}</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
  import codes from "~/config/CodeTable";
  import Bch, {
    Pattern
  } from "./Bch.js";

  import commonFunctions from "~/mixin/commonFunctions.js";
  import Api from "~/service/Api";

  export default {
    mixins: [commonFunctions],
    props: ["data"],
    inject: ["root"],
    data() {
      return {
        rules: Pattern,
        model: new Bch().data,

        bchData: [],
        bchColumns: [{
            label: '机构代码',
            prop: 'branch',
            width: 'auto'
          },
          {
            label: '机构中文名称',
            prop: 'bchname',
            width: 'auto'
          },
          {
            label: '机构英文名称',
            prop: 'namen',
            width: 'auto'
          },
          {
            label: '机构级别',
            prop: 'lev',
            width: 'auto'
          },
          {
            label: '机构类型',
            prop: 'bchtyp',
            width: 'auto'
          },
          {
            label: '默认记账机构',
            prop: 'bchkey',
            width: 'auto'
          },
          {
            label: '上级全辖机构',
            prop: 'qxbranch',
            width: 'auto'
          },
          {
            label: '自贸区标识',
            prop: 'zmqflg',
            width: 'auto'
          },

          {
            label: '核心机构代码',
            prop: 'accbch',
            width: 'auto'
          },
         
         
        ],

        dataForm: new Bch().data,
        dialogNam: {
          'add':  '系统机构--新增',
          'edit': '系统机构--修改',
          'info': '系统机构--详情',
        },
        operaTyp: "",
        isDisabled: false,
        showDisplayBtn: true,
        showInsertBtn: true,
        showUpdateBtn: true,
        showDeleteBtn: true,
        dialogFlg: false,
        formLabelWidth: "150px",
        searchLoading: false,
      };
    },

    //初始化查询
    created() {
     
    },

    activated() {
      
    },
    mounted() {
      //this.getHiddenButtonVue();
    },
    computed: {
    
    },
    methods: {
      getHiddenButtonVue() {
        const loading = this.loading();
        this.$buttonControlService.hiddenBtnList()
          .then((res) => {
            // 【详情】按钮
            let hasDisplay = res.some((item) => {
              return item.ITEMID === 'bchDisplayBtn';
            })
            if (hasDisplay) {
              this.showDisplayBtn = false;
            } else {
              this.showDisplayBtn = true;
            }

            // 【新增】按钮
            let hasInsert = res.some((item) => {
              return item.ITEMID === 'bchInsertBtn';
            })
            if (hasInsert) {
              this.showInsertBtn = false;
            } else {
              this.showInsertBtn = true;
            }
            // 【修改】按钮
            let hasUpdate = res.some((item) => {
              return item.ITEMID === 'bchUpdateBtn';
            })
            if (hasUpdate) {
              this.showUpdateBtn = false;
            } else {
              this.showUpdateBtn = true;
            }
            // 【删除】按钮
            let hasDelete = res.some((item) => {
              return item.ITEMID === 'bchDeleteBtn';
            })
            if (hasDelete) {
              this.showDeleteBtn = false;
            } else {
              this.showDeleteBtn = true;
            }
            loading.close();
          }).catch(() => {
            loading.close();
          })
      },

     

      handleReset() {
        //this.root.$refs.modelForm.resetFields();
        this.model.branch = "";
        this.model.bchname = "";
        
      },

      onSearch() {
        this.model.pageSize = PageSize;
        this.model.pageNum = 1;
        this.onDblbchSearch();
      },

      onDblbchSearch() {
        this.searchLoading = true;
        let data={
          "rootBranch":"1000",
          "branch":this.model.branch,
          "bchname":this.model.bchname,
          "pageSize":this.model.pageSize,
          "pageNum":this.model.pageNum,
        }
        Api.post('/admin/bch/getBchListByRootWithFieldsPage',data).then(res => {
          if (res.respCode == SUCCESS) {
            const list = res.data.list
            this.bchData = list
            this.model.pageNum = res.data.pageNum
            this.model.pageSize = res.data.pageSize
            this.model.total = res.data.total
            this.searchLoading = false;
          } else {
            this.searchLoading = false;
            this.$notify.error("查询失败");
          }
        })
      },
      queryFunc(pageNumber, pageSize) {
        this.model.pageNum = pageNumber
        this.model.pageSize = pageSize
        this.onDblbchSearch()
      },

      //新增弹窗
      bchAdd() {
        this.isDisabled = false;
        this.operaTyp = "add";
        this.dialogFlg = true;
        this.dataForm = new Bch().data;
      },
      //新增提交
      commitAdd() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            Api.post('/admin/bch/addBch',this.dataForm)
              .then((res) => {
                if (res.respCode == "AAAAAA" || res.respCode == "AAAAAA") {
                  this.$notify.success("添加成功!");
                  
                  this.dialogFlg = false;
                  this.onDblbchSearch();
                } else {
                  this.$notify.error("添加失败!");
                }

              })
              .catch((err) => {
                this.$notify.error("添加失败!");
              });
          } else {
            this.$notify.error({
              title: '失败',
              message: '请检查必输项!'
            });
          }
        });
      },

      //查看弹窗
      bchInfo(row) {
        let tmp=row;
        Api.post('/admin/bch/getBchById',{"id":row.id})
          .then((res) => {
            this.isDisabled = true;
            this.operaTyp = "info"
            this.dataForm = res.data;
            this.dialogFlg = true;
          })
          .catch((err) => {
            this.$notify.error("数据查询失败");
          });
      },
      //修改弹窗
      bchEdit(row) {

        Api.post('/admin/bch/getBchById',{"id":row.id})
          .then((res) => {
            this.isDisabled = false;
            this.dataForm = res.data;
            this.operaTyp = "edit"
            this.dialogFlg = true;
          })
          .catch((err) => {
            this.$notify.error("数据查询失败");
          });
      },
      commitEdit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            Api.post('/admin/bch/updateBchById',this.dataForm)
              .then((res) => {
                if (res.errorCode == "AAAAAA" || res.respCode == "AAAAAA") {
                  this.$notify.success("修改成功!");
                  
                  this.dialogFlg = false;
                  this.onDblbchSearch();
                } else {
                  this.$notify.error("修改失败!");
                }

              })
              .catch((err) => {
                this.$notify.error("修改失败!");
              });
          } else {
            this.$notify.error(validMsg);
            return false;
          }
        });
      },

      bchDelete(row) {
        this.$confirm("是否确认删除?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            Api.post('/admin/bch/deleteBchById',{"id":row.id})
              .then((res) => {
                if (res.errorCode == "AAAAAA" || res.respCode == "AAAAAA") {
                  this.$notify.success("删除成功!");
                  
                  this.dialogFlg = false;
                  this.onDblbchSearch();
                } else {
                  this.$notify.error("删除失败!");
                }

              })
              .catch((err) => {
                this.$notify.error("删除失败.!");
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },

     

     

      //全量排序
      sortChange(obj) {
        let order = obj.order === 'descending' ? 'desc' : 'asc';
        let prop = obj.prop;
        let column = "";
        switch (prop) {
          case 'cod':
            column = "A.COD";
            break;
          case 'cur':
            column = "A.CUR";
            break;
          case 'reg':
            column = "A.REG";
            break;
          case 'bopcod':
            column = "A.BOPCOD";
            break;
          case 'txtBopparam':
            column = "B.TXT";
            break;
          case 'paramnam':
            column = "paramnam";
            break;
        }
        let orderObj = {
          order: order,
          column: column
        }
        this.model.orderStr = orderObj.order;
        this.model.columnStr = orderObj.column;

        this.onDblbchSearch()
      },

    },

  };
</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;
  }

  .el-dialog {
    min-height: 70%;
  }
</style>