<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-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({
  qualified: [{ required: true, message: "请输入数量", trigger: "blur" }],
});
const { proxy } = getCurrentInstance();
const selectConfig = reactive([
  // {
  //   label: "spu类型",
  //   prop: "type",
  //   data: [],
  // },
]);

const config = reactive([
  {
    attrs: {
      label: "仓库名称",
      prop: "warehouseName",
    },
  },
  {
    attrs: {
      label: "产品编码",
      prop: "code",
    },
  },
  {
    attrs: {
      label: "产品名称",
      prop: "productName",
    },
  },
  {
    attrs: {
      label: "规格",
      prop: "spec",
    },
  },
  {
    attrs: {
      label: "单位",
      prop: "unit",
    },
  },
  {
    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");
          },
        },
      ];
    },
  },
]);

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: "input",
    prop: "qualified",
    label: "数量",
  },
]);
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),
    }));
    sourceList.value.pagination.total = message.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};

const submitForm = () => {
  let submitAddress =
    modalType.value === "add"
      ? "/stock/defectiveToQualified"
      : "/stock/qualifiedToDefective";
  byform.value.handleSubmit((valid) => {
    if (Number(formData.data.qualified) > 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,
    qualified: "",
  };
  dialogVisible.value = true;
};

getList();

const handleChange = () => {
  sourceList.value.pagination.pageNum = 1;
  if (activeName.value === "first") {
    requestUrl = "/stock/pageByWarehouse";
    config.unshift({
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
      },
    });
  } else {
    requestUrl = "/stock/pageByProduct";
    config.shift();
  }
  getList();
};
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>