<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="[
          selectStatus
            ? {}
            : {
                text: '导出Excel',
                action: () => deriveExcel(),
              },
        ]"
        @get-list="getList"
      >
        <template #btn="{ item }">
          <div style="width: 100%; text-align: center">
            <el-button
              type="primary"
              @click="checkTheFlow(item)"
              v-if="!selectStatus"
              text
              >查看流水</el-button
            >
            <!-- <el-button type="primary" @click="clickSelect(item)" v-else text
              >选择</el-button
            > -->
            <el-button type="primary" @click="clickSelect(item)" v-else text
              >选择</el-button
            >
          </div>
        </template>
      </byTable>
      <!-- <div>
        <div>已选择货品</div>
        <div style="margin: 10px 0px">
          <el-tag
            style="margin-right: 10px"
            type="info"
            closable
            v-for="(good, index) in goodList"
            :key="good.id"
            @close="handleRemove(index)"
            >{{ good.productName }}</el-tag
          >
        </div>
        <div style="text-align: center">
          <el-button @click="handleCancel" size="large">取消</el-button>
          <el-button type="primary" @click="handleSubmit" size="large">
            确 定
          </el-button>
        </div>
      </div> -->
    </div>
  </div>
</template>

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

const { proxy } = getCurrentInstance();
const props = defineProps({
  selectStatus: Boolean,
  warehouseId: String,
});
const warehouseList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    id: "",
  },
});
const productUnit = ref([]);
const definition = ref([
  {
    label: "产品",
    value: "1",
  },
  {
    label: "物料",
    value: "2",
  },
]);
const loading = ref(false);
const selectConfig = computed(() => {
  if (props.warehouseId) {
    return [];
  }
  return [
    {
      label: "仓库名称",
      prop: "id",
      data: warehouseList.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseId",
        width: 220,
      },
      render(type) {
        return proxy.dictValueLabel(type, warehouseList.value);
      },
    },
    {
      attrs: {
        label: "物品类型",
        prop: "productDefinition",
        width: 140,
      },
      render(type) {
        return proxy.dictValueLabel(type, definition.value);
      },
    },
    {
      attrs: {
        label: "所属分类",
        prop: "productClassifyName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "物品编码",
        prop: "productCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
        "min-width": 180,
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "productSpec",
        width: 160,
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
        width: 120,
      },
      render(unit) {
        return proxy.dictDataEcho(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "库存数量",
        prop: "quantity",
        width: 140,
      },
    },
    {
      attrs: {
        label: "结存单价",
        prop: "unitPrice",
        width: 120,
      },
    },

    {
      attrs: {
        label: "操作",
        slot: "btn",
        width: 120,
        fixed: "right",
        align: "center",
      },
    },
  ];
});
const getDict = () => {
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          label: item.name,
          value: item.id,
        };
      });
    }
  });
  proxy.getDict(["unit"]).then((res) => {
    productUnit.value = res["unit"];
  });
  // proxy
  //   .post("/dictTenantData/page", {
  //     pageNum: 1,
  //     pageSize: 999,
  //     dictCode: "product_type",
  //     tenantId: useUserStore().user.tenantId,
  //   })
  //   .then((res) => {
  //     if (res.rows && res.rows.length > 0) {
  //       productType.value = res.rows.map((item) => {
  //         return {
  //           label: item.dictValue,
  //           value: item.dictKey,
  //         };
  //       });
  //     }
  //   });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  if (props.warehouseId) {
    sourceList.value.pagination.id = props.warehouseId;
  }
  proxy.post("/stock/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 checkTheFlow = (item) => {
  proxy.$router.replace({
    path: "/purchaseSales/outAndInWarehouse/record",
    query: {
      productId: item.productId,
    },
  });
};
const deriveExcel = () => {
  console.log("deriveExcel");
};
// const clickSelect = (item) => {
//   proxy.$emit("select", item);
// };
const goodList = ref([]);
const clickSelect = (row) => {
  const flag = goodList.value.some((x) => x.id === row.id);
  if (flag)
    return ElMessage({
      message: "该物品已选择",
      type: "info",
    });
  goodList.value.push({
    ...row,
    goodType: sourceList.value.pagination.definition,
  });
  return ElMessage({
    message: "选择成功",
    type: "success",
  });
};
const handleRemove = (index) => {
  goodList.value.splice(index, 1);
  return ElMessage({
    message: "删除成功",
    type: "success",
  });
};

const handleCancel = () => {
  goodList.value = [];
  proxy.$emit("cancel");
};

const handleSubmit = () => {
  if (!goodList.value.length > 0)
    return ElMessage({
      message: "请添加货品",
      type: "info",
    });
  proxy.$emit("select", goodList.value);
  goodList.value = [];
};
</script>

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