<template>
  <div class="tenant">
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :selectConfig="selectConfig"
        highlight-current-row
        :action-list="[
          {
            text: '退款申请',
            action: () => applyForRefund(),
          },
        ]"
        @get-list="getList"
      >
        <template #amount="{ item }">
          <div style="color: #409eff">{{ moneyFormat(item.amount, 2) }}</div>
        </template>
        <template #sumRefundMoney="{ item }">
          <div>{{ moneyFormat(item.sumRefundMoney, 2) }}</div>
        </template>
      </byTable>
    </div>

    <el-dialog
      title="到款登记"
      v-if="dialogVisible"
      v-model="dialogVisible"
      width="600"
      v-loading="loadingDialog"
    >
      <byForm
        :formConfig="formConfig"
        :formOption="formOption"
        v-model="formData.data"
        :rules="rules"
        ref="submit"
      >
        <template #receiptAmount>
          <div style="width: 100%">
            <el-input-number
              v-model="formData.data.receiptAmount"
              placeholder="请输入到款金额"
              :min="0"
              :precision="2"
              :controls="false"
              style="width: 100%"
            />
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import useUserStore from "@/store/modules/user";
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const refundStatus = ref([]);
const accountList = ref([]);
const status = ref([
  {
    label: "草稿",
    value: 0,
  },
  {
    label: "审批中",
    value: 10,
  },
  {
    label: "驳回",
    value: 20,
  },
  {
    label: "审批通过",
    value: 30,
  },
  {
    label: "终止",
    value: 99,
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    refundStatus: "",
    status: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "退款状态",
      prop: "refundStatus",
      data: refundStatus.value,
    },
    {
      label: "审批状态",
      prop: "status",
      data: status.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "供应商",
        prop: "supplyName",
        width: 300,
      },
    },
    {
      attrs: {
        label: "应退款金额",
        slot: "amount",
      },
    },
    {
      attrs: {
        label: "已退款金额",
        slot: "sumRefundMoney",
      },
    },
    {
      attrs: {
        label: "申请人",
        prop: "refundName",
      },
    },
    {
      attrs: {
        label: "申请时间",
        prop: "refundTime",
        width: 180,
      },
    },
    {
      attrs: {
        label: "审批状态",
        prop: "status",
      },
      render(type) {
        let text = "";
        if (status.value && status.value.length > 0) {
          let data = status.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "退款状态",
        prop: "refundStatus",
      },
      render(type) {
        let text = "";
        if (refundStatus.value && refundStatus.value.length > 0) {
          let data = refundStatus.value.filter((item) => item.value == type);
          if (data && data.length > 0) {
            text = data[0].label;
          }
        }
        return text;
      },
    },
    {
      attrs: {
        label: "操作",
        width: "100",
        align: "center",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "到款登记",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              receiptRegistration(row);
            },
          },
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      dictCode: "refund_status",
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        refundStatus.value = res.rows.map((item) => {
          return {
            label: item.dictValue,
            value: item.dictKey,
          };
        });
      }
    });
  proxy
    .post("/accountManagement/page", { pageNum: 1, pageSize: 999 })
    .then((res) => {
      accountList.value = res.rows.map((item) => {
        return {
          bankName: item.name,
          accountOpening: item.accountOpening,
          openingBank: item.openingBank,
          interbankNumber: item.interbankNumber,
          label: item.alias,
          value: item.id,
        };
      });
    });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/refund/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const applyForRefund = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "refund_flow",
      flowName: "退款申请",
    },
  });
};
const dialogVisible = ref(false);
const loadingDialog = ref(false);
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const formConfig = computed(() => {
  return [
    {
      label: "应退款信息",
    },
    {
      type: "input",
      prop: "supplyName",
      label: "供应商",
      itemType: "text",
      disabled: true,
    },
    {
      type: "input",
      prop: "amount",
      label: "应退金额",
      itemType: "text",
      disabled: true,
    },
    {
      label: "退款信息",
    },
    {
      type: "select",
      label: "到款账户",
      prop: "receiptAccountManagementId",
      data: accountList.value,
      itemWidth: 50,
    },

    {
      type: "slot",
      prop: "receiptAmount",
      slotName: "receiptAmount",
      label: "到款金额",
    },
    {
      type: "input",
      prop: "receiptName",
      label: "对方户名",
      itemType: "text",
    },
    {
      type: "input",
      prop: "receiptOpeningBank",
      label: "对方开户银行",
      itemType: "text",
    },
    {
      type: "input",
      prop: "receiptAccountOpening",
      label: "对方银行账号",
      itemType: "text",
    },
  ];
});
const rules = ref({
  receiptAccountManagementId: [
    { required: true, message: "请选择到款账户", trigger: "change" },
  ],
  receiptAmount: [
    { required: true, message: "请输入到款金额", trigger: "blur" },
  ],
});
const formData = reactive({
  data: {},
});
const receiptRegistration = (row) => {
  formData.data = {
    supplyName: row.supplyName,
    amount: row.amount,
    id: row.id,
  };
  loadingDialog.value = false;
  dialogVisible.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    loadingDialog.value = true;
    proxy.post("/refund/receipt/registration", formData.data).then(
      () => {
        ElMessage({
          message: "提交成功",
          type: "success",
        });
        dialogVisible.value = false;
        getList();
      },
      (err) => {
        console.log(err);
        loadingDialog.value = false;
      }
    );
  });
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
