<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="[
          {
            text: '发起申购',
            action: () => openModal(),
          },
        ]"
        @get-list="getList"
      >
        <template #code="{ item }">
          <div
            style="cursor: pointer; color: #409eff"
            @click="handleClickCode(item)"
          >
            {{ item.subscribeCode }}
          </div>
        </template>
      </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: "",
  },
});
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: "definition",
    data: [
      {
        label: "产品",
        value: "1",
      },
      {
        label: "物料",
        value: "2",
      },
    ],
  },
  {
    label: "状态",
    prop: "status",
    data: [],
  },
]);
const config = computed(() => {
  return [
    {
      attrs: {
        label: "申购单号",
        prop: "subscribeCode",
        slot: "code",
        width: 110,
      },
    },
    {
      attrs: {
        label: "货品类型",
        prop: "productDefinition",
        width: 80,
      },
      render(definition) {
        return definition == 1 ? "产品" : definition == 2 ? "物料" : "";
      },
    },
    {
      attrs: {
        label: "所属分类",
        prop: "productCategory",
        width: 100,
      },
    },
    {
      attrs: {
        label: "货品编码",
        prop: "productCustomCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "货品名称",
        prop: "productName",
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
        width: 60,
      },
      render(unit) {
        return proxy.dictValueLabel(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "申购数量",
        prop: "count",
        width: 110,
      },
    },
    {
      attrs: {
        label: "收货仓库",
        prop: "receiptWarehouseName",
      },
    },
    {
      attrs: {
        label: "要求到货时间",
        prop: "planArrivalTime",
      },
    },
    {
      attrs: {
        label: "申购时间",
        prop: "createTime",
        width: 155,
      },
    },
    {
      attrs: {
        label: "审批状态",
        prop: "count11",
        width: 100,
      },
    },
    {
      attrs: {
        label: "状态",
        prop: "status",
        width: 80,
      },
      render(status) {
        return statusData.value.find((x) => x.value == status).label;
      },
    },
    {
      attrs: {
        label: "操作",
        width: "80",
        align: "center",
      },
      // 渲染 el-button，一般用在最后一列。
      renderHTML(row) {
        return [
          row.status == 15
            ? {
                attrs: {
                  label: "作废",
                  type: "danger",
                  text: true,
                  // disabled: row.status != 30,
                },
                el: "button",
                click() {
                  // 弹窗提示是否删除
                  ElMessageBox.confirm(
                    "此操作将永久作废该数据, 是否继续?",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  ).then(() => {
                    // 删除
                    proxy
                      .post("/subscribeDetail/edit", {
                        ...row,
                        status: 99,
                      })
                      .then((res) => {
                        ElMessage({
                          message: "作废成功",
                          type: "success",
                        });
                        getList();
                      });
                  });
                },
              }
            : {},
        ];
      },
    },
  ];
});

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) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/subscribeDetail/pageByWdly  ", sourceList.value.pagination).then(
    (message) => {
      sourceList.value.data = message.rows.map((x) => ({
        ...x,
        ...JSON.parse(x.victoriatouristJson),
      }));
      sourceList.value.pagination.total = message.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    },
    (err) => {
      loading.value = false;
    }
  );
};
const openModal = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "wdly_apply_purchase",
      time: Date.now(),
    },
  });
};

const submitForm = () => {
  byform.value.handleSubmit((valid) => {
    submitLoading.value = true;
    proxy.post("/productionProcesses/" + modalType.value, formData.data).then(
      (res) => {
        ElMessage({
          message: modalType.value == "add" ? "添加成功" : "编辑成功",
          type: "success",
        });
        dialogVisible.value = false;
        submitLoading.value = false;
        getList();
      },
      (err) => {
        console.log(err, "aswwwww");
        submitLoading.value = false;
      }
    );
  });
};

const statusData = ref([
  {
    label: "审批中",
    value: "10",
  },
  {
    label: "待采购",
    value: "15",
  },
  {
    label: "部分采购",
    value: "30",
  },
  {
    label: "已采购",
    value: "20",
  },
  {
    label: "已作废",
    value: "99",
  },
]);
selectConfig[1].data = statusData.value;
const productUnit = ref([]);
const getDict = () => {
  proxy.getDictOne(["unit"]).then((res) => {
    productUnit.value = res["unit"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
  });
};
getDict();
getList();

const handleClickCode = (row) => {
  proxy.$router.push({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "wdly_apply_purchase",
      id: row.flowId,
      subscribeId: row.subscribeId,
      processType: 20,
    },
  });
};
</script>
  
<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>