<template>
  <div class="tenant">
    <!-- <Banner /> -->

    <div style="background: #fff">
      <el-tabs
        v-model="activeName"
        type="card"
        class="demo-tabs"
        @tab-change="handleChange"
      >
        <el-tab-pane label="按仓库" name="first"></el-tab-pane>
        <el-tab-pane label="按产品" name="second"></el-tab-pane>
        <el-tab-pane label="按SPU" name="three"></el-tab-pane>
      </el-tabs>
      <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"
      >
      </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"
      >
      </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";
import SelectProduct from "@/components/product/SelectProduct";

const loading = ref(false);
let activeName = ref("first");
const submitLoading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
  },
});
let dialogVisible = ref(false);
let openProduct = ref(false);

let roomDialogVisible = ref(false);
let modalType = ref("add");
let rules = ref({
  quantity: [{ required: true, message: "请输入数量", trigger: "blur" }],
});
const { proxy } = getCurrentInstance();
const selectConfig = ref([
  {
    label: "仓库名称",
    prop: "id",
    data: [],
  },
  {
    label: "物品类型",
    prop: "definition",
    data: [
      {
        label: "产品",
        value: "1",
      },
      {
        label: "物料",
        value: "2",
      },
    ],
  },
]);

const selectConfigData = [
  [
    {
      label: "仓库名称",
      prop: "id",
      data: [],
    },
    {
      label: "物品类型",
      prop: "definition",
      data: [
        {
          label: "产品",
          value: "1",
        },
        {
          label: "物料",
          value: "2",
        },
      ],
    },
  ],
  [
    {
      label: "物品类型",
      prop: "definition",
      data: [
        {
          label: "产品",
          value: "1",
        },
        {
          label: "物料",
          value: "2",
        },
      ],
    },
  ],
];

const config = ref([]);

const configData = [
  [
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
      },
    },
    {
      attrs: {
        label: "所属分类",
        prop: "productClassifyName",
      },
    },
    {
      attrs: {
        label: "物品编码",
        prop: "productCustomCode",
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "productSpec",
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
      },
      render(unit) {
        return proxy.dictValueLabel(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "可用库存",
        prop: "quantity",
      },
    },
    {
      attrs: {
        label: "冻结库存",
        prop: "frozenQuantity",
      },
    },
    {
      attrs: {
        label: "次品库存",
        prop: "defectiveQuantity",
      },
    },
    {
      attrs: {
        label: "操作",
        width: "200",
        align: "right",
      },
      // 渲染 el-button，一般用在最后一列。
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "良转次",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              getDtl(row, "add");
            },
          },
          {
            attrs: {
              label: "次转良",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              getDtl(row, "edit");
            },
          },
        ];
      },
    },
  ],
  [
    {
      attrs: {
        label: "所属分类",
        prop: "productClassifyName",
      },
    },
    {
      attrs: {
        label: "物品编码",
        prop: "productCustomCode",
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "productSpec",
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
      },
      render(unit) {
        return proxy.dictValueLabel(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "可用库存",
        prop: "quantity",
      },
    },
    {
      attrs: {
        label: "冻结库存",
        prop: "frozenQuantity",
      },
    },
    {
      attrs: {
        label: "次品库存",
        prop: "defectiveQuantity",
      },
    },
    {
      attrs: {
        label: "操作",
        width: "200",
        align: "right",
      },
      // 渲染 el-button，一般用在最后一列。
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "良转次",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              getDtl(row, "add");
            },
          },
          {
            attrs: {
              label: "次转良",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              getDtl(row, "edit");
            },
          },
        ];
      },
    },
  ],
  [
    {
      attrs: {
        label: "spu编码",
        prop: "productSpuCode",
      },
    },
    {
      attrs: {
        label: "spu名称",
        prop: "productSpuName",
      },
    },
    {
      attrs: {
        label: "关联产品数",
        prop: "linkProductQuantity",
      },
    },
    {
      attrs: {
        label: "库存数量",
        prop: "quantity",
      },
    },
  ],
];
let formData = reactive({
  data: {},
  treeData: [],
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});

const formConfig = reactive([
  {
    type: "input",
    prop: "canSum",
    label: "可转数量",
    disabled: true,
  },
  {
    type: "number",
    prop: "quantity",
    label: "数量",
    itemWidth: 33,
    precision: 2,
    min: 0,
    controls: false,
  },
]);
const byform = ref(null);

let requestUrl = "/stock/pageByWarehouse";
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post(requestUrl, sourceList.value.pagination).then((message) => {
    console.log(message);
    sourceList.value.data = message.rows.map((x) => ({
      ...x,
      ...JSON.parse(x.victoriatouristJson),
      quantity: x.quantity,
    }));
    sourceList.value.pagination.total = message.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};

const submitForm = () => {
  let submitAddress =
    modalType.value === "add"
      ? "/stock/qualifiedToDefective"
      : "/stock/defectiveToQualified";
  byform.value.handleSubmit((valid) => {
    if (Number(formData.data.quantity) > Number(formData.data.canSum)) {
      return ElMessage({
        message: "不可大于可转数量！",
        type: "info",
      });
    }
    submitLoading.value = true;
    proxy.post(submitAddress, formData.data).then(
      (res) => {
        ElMessage({
          message: "操作成功",
          type: "success",
        });
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => (submitLoading.value = false)
    );
  });
};

const getDtl = (row, type) => {
  modalType.value = type;
  let canSum = type === "add" ? row.quantity : row.defectiveQuantity;
  formData.data = {
    id: row.id,
    canSum,
    quantity: "",
  };
  dialogVisible.value = true;
};
config.value = configData[0];
getList();

const handleChange = () => {
  sourceList.value.pagination.pageNum = 1;
  if (activeName.value === "first") {
    requestUrl = "/stock/pageByWarehouse";
    selectConfig.value = selectConfigData[0];
    selectConfig.value[0].data = warehouseList.value.map((x) => ({
      label: x.name,
      value: x.id,
    }));
    config.value = configData[0];
  } else if (activeName.value === "second") {
    requestUrl = "/stock/pageByProduct";
    selectConfig.value = selectConfigData[1];
    config.value = configData[1];
  } else {
    requestUrl = "/stock/pageByProductSpu";
    selectConfig.value = selectConfigData[1];
    config.value = configData[2];
  }
  getList();
};

const warehouseList = ref([]);
const productUnit = ref([]);
const getDict = () => {
  proxy.getDictOne(["unit"]).then((res) => {
    productUnit.value = res["unit"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
  });
  proxy
    .post("/warehouse/page", {
      pageNum: 1,
      pageSize: 999,
    })
    .then((message) => {
      warehouseList.value = message.rows;
      selectConfig.value = selectConfigData[0];
      selectConfig.value[0].data = warehouseList.value.map((x) => ({
        label: x.name,
        value: x.id,
      }));
    });
};
getDict();
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>