<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
                  <el-form-item label="统一社会信用代码" prop="uniCode" label-width="200">
                    <el-input
                        v-model="queryParams.uniCode"
                        placeholder="请输入统一社会信用代码"
                        clearable
                        @keyup.enter="handleQuery"
                    />
                  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            :disabled="!xcrInstitutionalClientsList.length"
            v-hasPermi="['basicData:xcrInstitutionalClients:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="xcrInstitutionalClientsList" height="650" @selection-change="handleSelectionChange">
        <el-table-column label="统一社会信用代码" align="center" prop="uniCode" width="200"/>
                  <el-table-column label="唯一记录id" align="center" prop="ID" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="机构客户号" align="center" prop="DWBH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位名称" align="center" prop="DWMC" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位地址" align="center" prop="DWDZ" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位法人代表姓名" align="center" prop="DWFRDBXM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位法人代表证件类型" align="center" prop="DWFRDBZJLX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位法人代表证件号码" align="center" prop="DWFRDBZJHM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位隶属关系" align="center" prop="DWLSGX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位经济类型" align="center" prop="DWJJLX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位所属行业" align="center" prop="DWSSHY" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位邮编" align="center" prop="DWYB" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位电子信箱" align="center" prop="DWDZXX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位发薪日" align="center" prop="DWFXR" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人姓名" align="center" prop="JBRXM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人固定电话号码" align="center" prop="JBRGDDHHM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人手机号码" align="center" prop="JBRSJHM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人证件类型" align="center" prop="JBRZJLX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人证件号码" align="center" prop="JBRZJHM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="经办人电子信箱" align="center" prop="JBREMAIL" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="组织机构代码" align="center" prop="ZZJGDM" :show-overflow-tooltip='true' width="150"/>
              <el-table-column label="单位设立日期" align="center" prop="DWSLRQ" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.DWSLRQ, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="单位开户日期" align="center" prop="DWKHRQ" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.DWKHRQ, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
                  <el-table-column label="受托银行名称" align="center" prop="STYHMC" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="受托银行代码" align="center" prop="STYHDM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="管理机构代码" align="center" prop="JGBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="所属区县编码" align="center" prop="SSQXBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位社保账号" align="center" prop="DWSBZH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位法人代表联系电话" align="center" prop="DWFRDBLXDH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位法人代表移动电话" align="center" prop="DWFRDBYDDH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="注册住所" align="center" prop="ZCZS" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="注册资本" align="center" prop="ZCZB" :show-overflow-tooltip='true' width="150"/>
              <el-table-column label="注册登记日期" align="center" prop="ZCRQ" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.ZCRQ, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
                  <el-table-column label="营业执照登记机关" align="center" prop="YYZZDJJG" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="营业执照注册号" align="center" prop="YYYZZCH" :show-overflow-tooltip='true' width="150"/>
              <el-table-column label="登记日期" align="center" prop="DJRQ" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.DJRQ, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="登记时间" align="center" prop="DJSJ" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.DJSJ, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
                  <el-table-column label="登记操作员id" align="center" prop="DJCZYID" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="登记操作员" align="center" prop="DJCZY" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位简称" align="center" prop="DWJC" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="税务登记号" align="center" prop="SWDJH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户开户银行" align="center" prop="JBZHKHYH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户银行账号" align="center" prop="JBZHYHZH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户开户名称" align="center" prop="JBZHKHMC" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位所属区域编码" align="center" prop="DWSSQYBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位所属省市编码" align="center" prop="DWSSSSBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位所在城市编码" align="center" prop="DWSZCSBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="信息化管理部门" align="center" prop="XXHGLBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="客户类型编码" align="center" prop="KHLXBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="客户级别编码" align="center" prop="KHJBBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位外网地址" align="center" prop="DWWWDZ" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="拼音简写" align="center" prop="PYJX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="机构类别编码" align="center" prop="JGLBBM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="法人性别" align="center" prop="FRXB" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="法人国籍" align="center" prop="FRGJ" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="是否独立法人" align="center" prop="SFDLFR" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="法人证书代码" align="center" prop="FRZSDM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="实收资本" align="center" prop="SSZB" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="资质证书编号" align="center" prop="ZZZSBH" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位预算代码" align="center" prop="DWYSDM" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="登记渠道" align="center" prop="DJQD" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="组织机构类型" align="center" prop="ZZJGLX" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户开户银行2" align="center" prop="JBZHKHYH2" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户银行账号2" align="center" prop="JBZHYHZH2" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户开户银行3" align="center" prop="JBZHKHYH3" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户银行账号3" align="center" prop="JBZHYHZH3" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户开户银行4" align="center" prop="JBZHKHYH4" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="基本账户银行账号4" align="center" prop="JBZHYHZH4" :show-overflow-tooltip='true' width="150"/>
                  <el-table-column label="单位性质" align="center" prop="DWXZ" :show-overflow-tooltip='true' width="150"/>
              <el-table-column label="插入时间" align="center" prop="iTime" width="180">
                <template #default="scope">
                  <span>{{ parseTime(scope.row.iTime, '{y}-{m}-{d}') }}</span>
                </template>
              </el-table-column>
    </el-table>

    <div style="position: fixed;bottom: 20px;right: 10px;">
      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </div>

    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
      <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <el-icon class="el-icon--upload">
          <upload-filled/>
        </el-icon>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <span>仅允许导入xls、xlsx格式文件。</span>
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Clients">
  import {
    listClients,
    getClients,
    delClients,
    delClientsBatch
  } from "@/api/basicData/xcrInstitutionalClients";
  import {reactive, ref, toRaw} from "vue";
  import {getToken} from "@/utils/auth";
  import {ElMessage} from "element-plus";
  import {likeQueryMethod} from "@/utils/likeQueryMethod";

  const {proxy} = getCurrentInstance();

  const xcrInstitutionalClientsList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");

  const upload = reactive({
    // 是否显示弹出层（用户导入）
    open: false,
    // 弹出层标题（用户导入）
    title: '',
    // 是否禁用上传
    isUploading: false,
    // 设置上传的请求头部
    headers: {Authorization: getToken()},
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + '/xcrInstitutionalClients/importData'
  })
  const handleFileSuccess = (e) => {
    if (e.code == 200) {
      ElMessage({
        type: "success",
        message: e.msg
      })
      getList()
      proxy.$refs['uploadRef']?.clearFiles()
    } else {
      ElMessage({
        type: "error",
        message: e.msg
      })
      proxy.$refs['uploadRef']?.clearFiles()
    }
  }

  function submitFileForm() {
    proxy.$refs['uploadRef']?.submit();
    upload.open = false;
  }

  function handleImport() {
    upload.open = true
  }

  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 20,
                        ID: null,
                        DWBH: null,
                        DWMC: null,
                        DWDZ: null,
                        DWFRDBXM: null,
                        DWFRDBZJLX: null,
                        DWFRDBZJHM: null,
                        DWLSGX: null,
                        DWJJLX: null,
                        DWSSHY: null,
                        DWYB: null,
                        DWDZXX: null,
                        DWFXR: null,
                        JBRXM: null,
                        JBRGDDHHM: null,
                        JBRSJHM: null,
                        JBRZJLX: null,
                        JBRZJHM: null,
                        JBREMAIL: null,
                        ZZJGDM: null,
                        DWSLRQ: null,
                        DWKHRQ: null,
                        STYHMC: null,
                        STYHDM: null,
                        JGBM: null,
                        SSQXBM: null,
                        DWSBZH: null,
                        DWFRDBLXDH: null,
                        DWFRDBYDDH: null,
                        ZCZS: null,
                        ZCZB: null,
                        ZCRQ: null,
                        YYZZDJJG: null,
                        YYYZZCH: null,
                        DJRQ: null,
                        DJSJ: null,
                        DJCZYID: null,
                        DJCZY: null,
                        DWJC: null,
                        SWDJH: null,
                        JBZHKHYH: null,
                        JBZHYHZH: null,
                        JBZHKHMC: null,
                        DWSSQYBM: null,
                        DWSSSSBM: null,
                        DWSZCSBM: null,
                        XXHGLBM: null,
                        KHLXBM: null,
                        KHJBBM: null,
                        DWWWDZ: null,
                        PYJX: null,
                        JGLBBM: null,
                        FRXB: null,
                        FRGJ: null,
                        SFDLFR: null,
                        FRZSDM: null,
                        SSZB: null,
                        ZZZSBH: null,
                        DWYSDM: null,
                        DJQD: null,
                        ZZJGLX: null,
                        JBZHKHYH2: null,
                        JBZHYHZH2: null,
                        JBZHKHYH3: null,
                        JBZHYHZH3: null,
                        JBZHKHYH4: null,
                        JBZHYHZH4: null,
                        DWXZ: null,
                        iTime: null,
                        uniCode: ''
    },
    rules: {
    }
  });

  const {queryParams, form, rules} = toRefs(data);

  /** 查询机构客户信息列表 */
  function getList() {
    loading.value = true;
                                                                                              let toServerObj = likeQueryMethod('uni_code', queryParams.value.uniCode, queryParams.value.pageNum, queryParams.value.pageSize);
    listClients(toServerObj).then(response => {
            xcrInstitutionalClientsList.value = response.records;
      total.value = response.total;
      loading.value = false;
    });
  }


  // 表单重置
  function reset() {
    form.value = {
                        xhCodeId: null,
                        ID: null,
                        DWBH: null,
                        DWMC: null,
                        DWDZ: null,
                        DWFRDBXM: null,
                        DWFRDBZJLX: null,
                        DWFRDBZJHM: null,
                        DWLSGX: null,
                        DWJJLX: null,
                        DWSSHY: null,
                        DWYB: null,
                        DWDZXX: null,
                        DWFXR: null,
                        JBRXM: null,
                        JBRGDDHHM: null,
                        JBRSJHM: null,
                        JBRZJLX: null,
                        JBRZJHM: null,
                        JBREMAIL: null,
                        ZZJGDM: null,
                        DWSLRQ: null,
                        DWKHRQ: null,
                        STYHMC: null,
                        STYHDM: null,
                        JGBM: null,
                        SSQXBM: null,
                        DWSBZH: null,
                        DWFRDBLXDH: null,
                        DWFRDBYDDH: null,
                        ZCZS: null,
                        ZCZB: null,
                        ZCRQ: null,
                        YYZZDJJG: null,
                        YYYZZCH: null,
                        DJRQ: null,
                        DJSJ: null,
                        DJCZYID: null,
                        DJCZY: null,
                        DWJC: null,
                        SWDJH: null,
                        JBZHKHYH: null,
                        JBZHYHZH: null,
                        JBZHKHMC: null,
                        DWSSQYBM: null,
                        DWSSSSBM: null,
                        DWSZCSBM: null,
                        XXHGLBM: null,
                        KHLXBM: null,
                        KHJBBM: null,
                        DWWWDZ: null,
                        PYJX: null,
                        JGLBBM: null,
                        FRXB: null,
                        FRGJ: null,
                        SFDLFR: null,
                        FRZSDM: null,
                        SSZB: null,
                        ZZZSBH: null,
                        DWYSDM: null,
                        DJQD: null,
                        ZZJGLX: null,
                        JBZHKHYH2: null,
                        JBZHYHZH2: null,
                        JBZHKHYH3: null,
                        JBZHYHZH3: null,
                        JBZHKHYH4: null,
                        JBZHYHZH4: null,
                        DWXZ: null,
                        iTime: null,
                        uniCode: ''
    };
    proxy.resetForm("xcrInstitutionalClientsRef");
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }

  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.xhCodeId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }


  /** 删除按钮操作 */
  async function handleDelete(row) {
    const res = await delClients();
    if(res.code == 200){
      ElMessage({
        type:'success',
        message:'删除成功'
      })
      getList();
    }
  }
  /** 批量删除*/
  const batchDelete = (row)=>{
    const idsToS = row.xhCodeId || ids.value;
    (proxy.$modal).confirm('是否确认删除这' + idsToS.length+'条的数据项？').then(function() {
      return delClientsBatch(toRaw(idsToS));
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('basicData/xcrInstitutionalClients/export', {
      ...queryParams.value
    }, `机构客户信息_${new Date().getTime()}.xlsx`)
  }

  getList();
</script>
