<template>
  <div>
    <!-- <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px] formdata" :model="queryParams" inline>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card> -->
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button type="primary" @click="handleEdit(company_info)"
        >添加凭证</el-button
      >
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column
            label="ID"
            prop="id"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            label="公司名称"
            prop="company_name"
            width="300"
            show-overflow-tooltip
          >
            <span>{{ company_info.company_name }}</span>
          </el-table-column>
          <el-table-column
            label="充值金额"
            prop="deposit"
            width="180"
            show-overflow-tooltip
          />
          <el-table-column
            label="凭证地址"
            prop="voucher"
            show-overflow-tooltip
          />
          <el-table-column
            label="创建时间"
            prop="create_time"
            width="200"
            show-overflow-tooltip
          />
          <el-table-column
            label="更新时间"
            prop="update_time"
            width="200"
            show-overflow-tooltip
          />
          <el-table-column
            label="操作"
            align="center"
            width="100"
            fixed="right"
          >
            <template #default="{ row }">
              <div style="display: flex">
                <el-button
                  type="primary"
                  link
                  @click="openTransfer(row.voucher)"
                >
                  查看凭证
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>

    <edit-popup
      v-if="showEdit"
      ref="editRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>
  
<script lang="ts" setup name="financeCompanyLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import useUserStore from "@/stores/modules/user";
import {
  apiCompanyLists,
  apiCompanyDetail,
  getDepositRechargeTransferVoucherList,
} from "@/api/company";
import { timeFormat } from "@/utils/util";
import { dictDataLists } from "@/api/setting/dict";
import { getRoutePath } from "@/router";
import { dictContractTypeList } from "@/utils/dict.ts";
import EditPopup from "./component/voucher.vue";

const userStore = useUserStore();
// console.log(userStore.userInfo.company_id);
const route = useRoute();

// 是否显示添加框
const showEdit = ref(false);
const editRef = ref(null);
// 添加
const handleEdit = async (data: any) => {
  showEdit.value = true;
  await nextTick();
  editRef.value?.open("edit");
  editRef.value?.setFormData(data);
};

const company_info = reactive({
  id: "",
  company_name: "",
  master_name: "",
  master_phone: "",
});

const getCompanyInfo = () => {
  apiCompanyDetail({ id: queryParams.company_id }).then((data) => {
    for (const key in company_info) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        company_info[key] = data[key];
      }
    }
  });
};

// 查看凭证
const openTransfer = (uri: any) => {
  window.open(uri);
};

// 创建合同与发送短信结束
// 查询条件
const queryParams = reactive({
  company_id: "",
});

if (route.query.company_id) {
  queryParams["company_id"] = route.query.company_id;
  getCompanyInfo();
}

const datas = reactive({
  dictTypeLists: [],
});
const getdictTypeLists = async () => {
  const data = await dictDataLists({ type_id: 6 });
  datas["dictTypeLists"] = data["lists"];
};
getdictTypeLists();
// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: getDepositRechargeTransferVoucherList,
  params: queryParams,
});

getLists();
</script>
  <style lang="scss">
.btn {
  position: absolute;
}
h1 {
  text-align: center;
  font-weight: bold;
  font-size: 30px;
  color: red;
  margin-bottom: 10px;
}

.content {
  font-size: 20px;
}

.info {
  color: red;
  font-weight: bold;
  font-size: 18px;
  display: inline-block;
  margin: 0 5px;
}

.btn_menu {
  margin-top: 10vh;
  display: flex;
  justify-content: space-around;
}
.formdata {
  .el-form-item {
    width: 20%;
  }
}
</style>
  