<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: selectRow,
        }"
        :action-list="[
          {
            text: '采购',
            disabled: selectData.length === 0,
            action: () => start(20),
          },
        ]"
        @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>
import { ElMessage, ElMessageBox } from "element-plus";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
const loading = ref(false);
const submitLoading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
    status: "15,30",
  },
});
const dialogVisible = ref(false);
const modalType = ref("add");
let rules = ref({
  name: [{ required: true, message: "请输入供应商名称", trigger: "blur" }],
});
const { proxy } = getCurrentInstance();
const selectConfig = reactive([
  {
    label: "状态",
    prop: "status",
    data: [
      {
        label: "待采购",
        value: "15",
      },
      {
        label: "部分采购",
        value: "30",
      },
    ],
  },
  {
    label: "收货仓库",
    prop: "receiptWarehouseId",
    data: [],
  },
  {
    label: "产品归属部门",
    prop: "deptId",
    data: [],
  },
]);
const config = computed(() => {
  return [
    {
      type: "selection",
      attrs: {
        checkAtt: "isCheck",
      },
    },
    {
      attrs: {
        label: "申购单号",
        prop: "subscribeCode",
      },
    },
    {
      attrs: {
        label: "货品类型",
        prop: "productDefinition",
      },
      render(definition) {
        return definition == 1 ? "产品" : definition == 2 ? "物料" : "";
      },
    },

    {
      attrs: {
        label: "物品编码",
        prop: "productCustomCode",
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
      },
      render(unit) {
        return proxy.dictDataEcho(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "申购数量",
        prop: "count",
      },
    },
    {
      attrs: {
        label: "已采购数量",
        prop: "purchaseCount",
      },
    },
    {
      attrs: {
        label: "收货仓库",
        prop: "receiptWarehouseName",
        width: 130,
      },
    },
    {
      attrs: {
        label: "要求到货时间",
        prop: "planArrivalTime",
        width: 130,
      },
    },
    {
      attrs: {
        label: "采购状态",
        prop: "status",
      },
      render(status) {
        return status == 15 ? "待采购" : status == 20 ? "已采购" : "部分采购";
      },
    },
    {
      attrs: {
        label: "申购人",
        prop: "subcribeName",
      },
    },
    {
      attrs: {
        label: "申购时间",
        prop: "subcribeTime",
        width: 155,
      },
    },
    {
      attrs: {
        label: "操作",
        width: "80",
        align: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "采购",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              selectDataOne.value = [row];
              start(10);
            },
          },
        ];
      },
    },
  ];
});

const formData = reactive({
  data: {
    type: "1",
  },
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const byform = ref(null);
const formConfig = computed(() => {
  return [];
});

const getList = async (req) => {
  if (req === undefined || req.status === "") {
    req = {
      ...req,
      neStatus: "99",
      status: "15,30",
    };
  }
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  getDeptList();
  loading.value = true;
  proxy
    .post("/subscribeDetail/pageByWdly", sourceList.value.pagination)
    .then((message) => {
      message.rows.forEach((x) => {
        if (x.status <= 30) {
          x.isCheck = true;
        } else {
          x.isCheck = false;
        }
      });
      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 = () => {
  byform.value.handleSubmit((valid) => {
    formData.data.fileList = fileList.value;
    submitLoading.value = true;
    proxy.post("/productionProcesses/" + modalType.value, formData.data).then(
      (res) => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        fileList.value = [];
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => {
        console.log(err, "aswwwww");
        submitLoading.value = false;
      }
    );
  });
};

const selectData = ref([]);
const selectDataOne = ref([]);
const selectRow = (data) => {
  selectData.value = data;
};
watch(selectData, (newVal, oldVal) => {
  if (newVal.length == 0) {
    sourceList.value.data.forEach((x) => {
      if (x.status <= 30) {
        x.isCheck = true;
      } else {
        x.isCheck = false;
      }
    });
  } else if (newVal.length == 1) {
    const current = newVal[0];
    sourceList.value.data.forEach((x) => {
      if (x.receiptWarehouseId !== current.receiptWarehouseId) {
        x.isCheck = false;
      }
    });
  }
});

const start = (type) => {
  modalType.value = "add";
  let ids = [];
  let row = {};
  if (type === 10) {
    row = selectDataOne.value[0];
    ids = selectDataOne.value.map((x) => x.id).join();
  } else if (type === 20) {
    ids = selectData.value.map((x) => x.id).join();
    row = selectData.value[0];
  }
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "wdly_purchase",
      ids,
      flowName: "采购申请",
    },
  });
};

const warehouseList = ref([]);
const productUnit = ref([]);
const deptData = ref([]);
const getDeptList = () => {
  proxy
    .post("/subscribe/getDepts", { status: sourceList.value.pagination.status })
    .then((res) => {
      deptData.value = res;
      selectConfig[2].data = res.map((x) => ({
        label: x.deptName,
        value: x.deptId,
      }));
    });
};
const getDict = () => {
  proxy.getDict(["unit"]).then((res) => {
    productUnit.value = res["unit"];
  });
  proxy
    .post("/warehouse/page", {
      pageNum: 1,
      pageSize: 999,
    })
    .then((message) => {
      warehouseList.value = message.rows;
      selectConfig[1].data = message.rows.map((x) => ({
        label: x.name,
        value: x.id,
      }));
    });
  // getDeptList();
};
getDict();
getList();
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
:deep(.el-table__header-wrapper .el-checkbox) {
  display: none;
}
</style>