<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="[]"
        @get-list="getList"
      >
        <template #money="{ item }">
          {{ item.currency }} {{ moneyFormat(item.amount) }}
        </template>
      </byTable>
    </div>
    <el-dialog
      title="入库"
      v-model="dialogVisible"
      width="800"
      v-loading="loading"
    >
      <byForm
        :formConfig="formConfig"
        :formOption="formOption"
        v-model="formData.data"
        :rules="rules"
        ref="byform"
      >
        <template #detail>
          <div style="width: 100%">
            <el-button type="primary" style="margin-bottom: 10px"
              >选择合同</el-button
            >
            <el-table :data="formData.data.stockWaitDetailsList">
              <el-table-column prop="productCode" label="合同编码" />
              <el-table-column prop="quantity" label="关联金额" min-width="150">
                <template #default="{ row, $index }">
                  <el-form-item
                    :prop="'stockWaitDetailsList.' + $index + '.quantity'"
                    :rules="rules.quantity"
                    :inline-message="true"
                  >
                    <el-input-number
                      v-model="row.quantity"
                      :precision="4"
                      :controls="false"
                      :min="0"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column prop="zip" label="操作" width="100">
                <template #default="{ $index }">
                  <el-button type="primary" link @click="handleRemove($index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </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 { computed, defineComponent, ref } from "vue";
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,
    type: 1,
    dataType: "1",
  },
});
let dialogVisible = ref(false);
let roomDialogVisible = ref(false);
let modalType = ref("add");
let rules = ref({
  type: [
    { required: true, message: "请选择仓库类型", trigger: ["blur", "change"] },
  ],
  warehouseId: [
    { required: true, message: "请选择仓库名称", trigger: "change" },
  ],
  quantity: [{ required: true, message: "请输入入库数量", trigger: "blur" }],
});
const { proxy } = getCurrentInstance();
const selectConfig = reactive([
  {
    label: "认领状态",
    prop: "type",
    data: [
      {
        label: "未认领",
        value: "1",
      },
      {
        label: "已认领",
        value: "2",
      },
    ],
  },
]);
const inboundType = ref([
  {
    label: "未入库",
    value: "0",
  },
  {
    label: "部分入库",
    value: "1",
  },
  {
    label: "入库完成",
    value: "2",
  },
]);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "账户名称",
        prop: "accountManagementName",
      },
      // render(type) {
      //   return proxy.dictDataEcho(type, warehouseType.value);
      // },
    },
    {
      attrs: {
        label: "到账金额",
        prop: "amount",
        slot: "money",
      },
    },

    {
      attrs: {
        label: "到账时间",
        prop: "transactionTime",
      },
    },
    {
      attrs: {
        label: "对方账户名称",
        prop: "name",
      },
    },
    {
      attrs: {
        label: "对方开户银行",
        prop: "openingBank",
      },
    },
    {
      attrs: {
        label: "对方账号",
        prop: "accountOpening",
      },
    },
    {
      attrs: {
        label: "摘要",
        prop: "remarks",
      },
    },
    {
      attrs: {
        label: "认领状态",
        prop: "isClaim",
      },
      render(isClaim) {
        return isClaim == 1 ? "已认领" : isClaim == 0 ? "未认领" : "";
      },
    },
    {
      attrs: {
        label: "操作",
        width: "100",
        align: "right",
      },
      // 渲染 el-button，一般用在最后一列。
      renderHTML(row) {
        return [
          row.isClaim == 0
            ? {
                attrs: {
                  label: "认领",
                  type: "primary",
                  text: true,
                  bg: true,
                  disabled: false,
                },
                el: "button",
                click() {
                  getDtl(row);
                },
              }
            : {},
          row.isClaim == 1
            ? {
                attrs: {
                  label: "取消认领",
                  text: true,
                  bg: true,
                  type: "primary",
                  disabled: false,
                  style: {
                    color: "#e6a23c",
                  },
                },
                el: "button",
                click() {
                  ElMessageBox.confirm("是否确定取消认领?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                  }).then(() => {
                    // 删除
                    proxy
                      .post("/productInfo/delete", {
                        id: row.id,
                      })
                      .then((res) => {
                        ElMessage({
                          message: "操作成功",
                          type: "success",
                        });
                        getList();
                      });
                  });
                },
              }
            : {},
        ];
      },
    },
  ];
});

let formData = reactive({
  data: {},
  treeData: [],
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const byform = ref(null);
const treeData = ref([]);
const formConfig = reactive([
  {
    type: "title",
    title: "账户信息",
  },
  {
    type: "select",
    prop: "businessType",
    label: "选择账户",
    required: true,
    disabled: true,
  },
  {
    type: "title",
    title: "交易信息",
  },
  {
    type: "date",
    itemType: "datetime",
    prop: "businessType",
    label: "交易时间",
    disabled: true,
  },
  {
    type: "select",
    // itemType: "datetime",
    prop: "businessType",
    label: "交易金额",
    itemWidth: 20,
    disabled: true,
  },
  {
    type: "selectInput",
    // itemType: "datetime",
    prop: "businessType",
    label: " ",
    itemWidth: 80,
    disabled: true,
  },

  // {
  //   type: "select",
  //   prop: "warehouseId",
  //   label: "仓库名称",
  //   itemWidth: 33,
  //   isLoad: {
  //     url: "/warehouse/page",
  //     req: {
  //       pageNum: 1,
  //       pageSize: 9999,
  //     },
  //     labelKey: "name",
  //     labelVal: "id",
  //     method: "post",
  //     resUrl: "rows",
  //   },
  // },
  {
    type: "title",
    title: "认领信息",
  },
  {
    type: "slot",
    slotName: "detail",
    label: "",
  },
]);
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("/accountRunningWater/page", sourceList.value.pagination)
    .then((message) => {
      console.log(message);
      sourceList.value.data = message.rows;
      // .map((x) => ({
      //   ...x,
      //   ...JSON.parse(x.victoriatouristJson),
      // }));
      sourceList.value.pagination.total = message.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};
const openModal = () => {
  dialogVisible.value = true;
  modalType.value = "add";
  formData.data = {};
};

const submitForm = () => {
  byform.value.handleSubmit((valid) => {
    const list = formData.data.stockWaitDetailsList;
    const total = list.reduce((total, x) => (total += Number(x.quantity)), 0);
    if (!(total > 0)) {
      return ElMessage({
        message: `本次入库不能为0！`,
        type: "info",
      });
    }
    for (let i = 0; i < list.length; i++) {
      const e = list[i];
      if (Number(e.receiptQuantity) + e.quantity > Number(e.purchaseQuantity)) {
        return ElMessage({
          message: "本次入库加已入库数量不可大于到货数量！",
          type: "info",
        });
      }
    }
    submitLoading.value = true;
    proxy.post("/stockWait/addByWdly", formData.data).then(
      (res) => {
        ElMessage({
          message: "操作成功",
          type: "success",
        });
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => (submitLoading.value = false)
    );
  });
};

const getDtl = (row) => {
  modalType.value = "edit";
  dialogVisible.value = true;

  // proxy.post("/stockWait/detailByWdly", { id: row.id }).then((res) => {
  //   const json = JSON.parse(res.victoriatouristJson);
  //   formData.data = {
  //     type: "1",
  //     businessType: res.businessType,
  //     warehouseId: "",
  //     code: json.code,
  //     stockWaitDetailsList: res.stockWaitDetailsList.map((x) => ({
  //       ...x,
  //       arrivalQuantity: x.quantity,
  //       quantity: 0,
  //     })),
  //   };
  //   dialogVisible.value = true;
  // });
};

const warehouseType = ref([]);
const getDict = () => {
  // // 币种数据
  proxy
    .post("/dictTenantData/page", {
      pageNum: 1,
      pageSize: 999,
      tenantId: useUserStore().user.tenantId,
      dictCode: "warehouse_type",
    })
    .then((res) => {
      warehouseType.value = res.rows;
      formConfig[0].data = res.rows.map((x) => ({
        label: x.dictValue,
        value: x.dictKey,
      }));
    });
};
getList();
getDict();
onMounted(() => {});
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>