<template>
  <div class="tenant">
    <!-- <Banner /> -->
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        highlight-current-row
        :selectConfig="selectConfig"
        :table-events="{
          //element talbe事件都能传
          select: select,
        }"
        :action-list="[
          {
            text: '发起请款',
            action: () => openModal(),
          },
        ]"
        @get-list="getList"
      >
        <template #fileSlot="{ item }">
          <div
            style="cursor: pointer; color: #409eff"
            @click="handleClickFile(item)"
          >
            {{ item.fileName }}
          </div>
        </template>
      </byTable>
    </div>
    <el-dialog
      :title="modalType == 'add' ? '添加供应商' : '编辑供应商'"
      v-model="dialogVisible"
      width="800"
      v-loading="loading"
    >
      <byForm
        :formConfig="formConfig"
        :formOption="formOption"
        v-model="formData.data"
        :rules="rules"
        ref="byform"
      >
        <template #address>
          <el-row :gutter="10" style="width: 100%">
            <el-col :span="8">
              <el-select v-model="formData.data.aa" placeholder="国家">
                <el-option v-for="item in 3" label="ceshi" value="1">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-select v-model="formData.data.aa" placeholder="省/洲">
                <el-option v-for="item in 3" label="ceshi" value="1">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8">
              <el-select v-model="formData.data.aa" placeholder="城市">
                <el-option v-for="item in 3" label="ceshi" value="1">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
          <el-row style="margin-top: 20px; width: 100%">
            <el-col :span="24">
              <el-input v-model="formData.data.bb" type="textarea"> </el-input>
            </el-col>
          </el-row>
        </template>

        <template #contact>
          <el-row :gutter="10" style="width: 100%">
            <el-col :span="8">
              <el-input v-model="formData.data.aa" placeholder="联系人">
              </el-input>
            </el-col>
            <el-col :span="16">
              <el-input v-model="formData.data.aa" placeholder="联系电话">
              </el-input>
            </el-col>
          </el-row>
        </template>

        <template #fileSlot>
          <div>
            <el-upload
              v-model:fileList="fileList"
              class="upload-demo"
              action="https://winfaster.obs.cn-south-1.myhuaweicloud.com"
              :limit="3"
              :data="uploadData"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :before-upload="handleBeforeUpload"
              accept=".pdf"
            >
              <el-button type="primary">选择</el-button>
              <template #file>
                <div>
                  <div style="margin-top: 15px">
                    <el-tag
                      class="ml-2"
                      type="info"
                      v-for="(item, index) in fileList"
                      :key="index"
                      closable
                      @close="handleClose(index)"
                      >{{ item.fileName }}</el-tag
                    >
                  </div>
                </div>
              </template>
            </el-upload>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="large">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('byform')"
          size="large"
          :loading="submitLoading"
        >
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
  
<script setup>
/* eslint-disable vue/no-unused-components */
import { ElMessage, ElMessageBox } from "element-plus";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import useUserStore from "@/store/modules/user";

const loading = ref(false);
const submitLoading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
    status: "",
  },
});
let modalType = ref("add");

const { proxy } = getCurrentInstance();
const selectConfig = reactive([
  {
    label: "类型",
    prop: "type",
    data: [],
  },
  {
    label: "审批状态",
    prop: "status",
    data: [
      {
        label: "审批中",
        value: "10",
      },
      {
        label: "驳回",
        value: "20",
      },
      {
        label: "已通过",
        value: "30",
      },
    ],
  },
  {
    label: "放款状态",
    prop: "paymentStatus",
    data: [],
  },
]);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "归属公司",
        prop: "corporationName",
      },
    },
    {
      attrs: {
        label: "归属部门",
        prop: "deptName",
      },
    },
    {
      attrs: {
        label: "请款类型",
        prop: "type",
      },
      render(type) {
        return proxy.dictDataEcho(type, fundsType.value);
      },
    },
    {
      attrs: {
        label: "请款人",
        prop: "userName",
      },
    },
    {
      attrs: {
        label: "请款时间",
        prop: "paymentTime",
      },
    },
    {
      attrs: {
        label: "请款金额",
        prop: "total",
      },
    },
    {
      attrs: {
        label: "用款说明",
        prop: "paymentRemarks",
      },
    },
    {
      attrs: {
        label: "付款方式",
        prop: "paymentMethod",
      },
      render(paymentMethod) {
        return proxy.dictDataEcho(paymentMethod, fundsPaymentMethod.value);
      },
    },
    {
      attrs: {
        label: "付款账户",
        prop: "accountManagementId",
      },
    },
    {
      attrs: {
        label: "审批状态",
        prop: "status",
      },
      render(status) {
        return status == 10
          ? "审批中"
          : status == 20
          ? "驳回"
          : status == 30
          ? "已通过"
          : "";
      },
    },
    {
      attrs: {
        label: "放款状态",
        prop: "subscribeContent",
      },
      render(subscribeContent) {
        return proxy.dictDataEcho(subscribeContent, fundsPaymentMethod.value);
      },
    },
    {
      attrs: {
        label: "操作",
        width: "100",
        align: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              // 弹窗提示是否删除
              ElMessageBox.confirm(
                "此操作将永久删除该数据, 是否继续?",
                "提示",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                }
              ).then(() => {
                // 删除
                proxy
                  .post("/productionProcesses/delete", {
                    id: row.id,
                  })
                  .then((res) => {
                    ElMessage({
                      message: "删除成功",
                      type: "success",
                    });
                    getList();
                  });
              });
            },
          },
        ];
      },
    },
  ];
});

let formData = reactive({
  data: {},
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const byform = ref(null);
const formConfig = computed(() => {});
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("/accountRequestFunds/page", sourceList.value.pagination)
    .then((message) => {
      console.log(message);
      sourceList.value.data = message.rows;
      sourceList.value.pagination.total = message.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};

const openModal = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "account_request_funds_flow",
    },
  });
};

const submitForm = () => {
  byform.value.handleSubmit((valid) => {
    formData.data.fileList = fileList.value;
    submitLoading.value = true;
    proxy.post("/productionProcesses/" + modalType.value, formData.data).then(
      (res) => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        fileList.value = [];
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => {
        console.log(err, "aswwwww");
        submitLoading.value = false;
      }
    );
  });
};

const getDtl = (row) => {
  modalType.value = "edit";
  proxy.post("/productionProcesses/detail", { id: row.id }).then((res) => {
    formData.data = res;
    dialogVisible.value = true;
  });
};
// 获取用户信息并赋默认值
const userInfo = useUserStore().user;
// const tenantId = "@福建宏星！#￥%……&*（）";
const tenantId = userInfo.tenantId;
const companyData = ref([]);
const accountData = ref([]);
const deptTreeData = ref([]);
const fundsType = ref([]);
const fundsCostType = ref([]);
const fundsPaymentMethod = ref([]);
const currencyType = ref([]);

const getDictData = () => {
  // 获取归属公司数据
  proxy
    .post("/corporation/page", { pageNum: 1, pageSize: 9999 })
    .then((res) => {
      companyData.value = res.rows;
    });
  // 账户数据
  proxy
    .post("/accountManagement/page", { pageNum: 1, pageSize: 9999 })
    .then((res) => {
      accountData.value = res.rows;
    });

  // 部门树
  // proxy
  //   .get("/tenantDept/list", {
  //     pageNum: 1,
  //     pageSize: 9999,
  //     tenantId: userInfo.tenantId,
  //   })
  //   .then((message) => {
  //     recursive(message.data);
  //     deptTreeData.value = proxy.handleTree(message.data, "corporationId");
  //   });
  // 请款类型数据
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      tenantId: tenantId,
      dictCode: "founds_type",
    })
    .then((res) => {
      fundsType.value = res.rows;
      selectConfig[0].data = res.rows.map((x) => ({
        label: x.dictValue,
        value: x.dictKey,
      }));
    });
  // 请款费用类型数据
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      tenantId: tenantId,
      dictCode: "funds_cost_type",
    })
    .then((res) => {
      fundsCostType.value = res.rows;
    });
  // 请款付款方式数据
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      tenantId: tenantId,
      dictCode: "funds_payment_method",
    })
    .then((res) => {
      fundsPaymentMethod.value = res.rows;
    });

  // 币种数据
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      tenantId: tenantId,
      dictCode: "account_currency",
    })
    .then((res) => {
      currencyType.value = res.rows;
    });
};
getList();
getDictData();
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>