<template>
  <a-modal
    :open="visible"
    title="修改物料信息"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      layout="vertical"
    >
      <a-row :gutter="[16, 0]">
        <a-col :span="12">
          <a-form-item ref="skuCode" label="物料编号" name="skuCode">
            <a-input
              v-model:value="formState.skuCode"
              placeholder="请输入物料编号"
              :disabled="true"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="skuName" label="物料名称" name="skuName">
            <a-input
              v-model:value="formState.skuName"
              placeholder="请输入物料名称"
            />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="ABC分类" name="abc">
            <a-select
              v-model:value="formState.abc"
              :options="skuAbc"
              placeholder="请选择ABC分类"
            >
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item ref="baseUnit" label="库存单位" name="baseUnit">
            <a-input
              v-model:value="formState.baseUnit"
              placeholder="请选择库存单位"
              :disabled="true"
            />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item ref="remark" label="备注" name="remark">
            <a-textarea
              v-model:value="formState.remark"
              laceholder="请输入备注"
            />
          </a-form-item>
        </a-col>
      </a-row>

      <!-- <a-form-item ref="barCode" label="条码" name="barCode">
        <a-input v-model:value="formState.barCode" placeholder="请输入条码" />
      </a-form-item> -->

      <!-- <a-form-item ref="categoryCode" label="种类编号" name="categoryCode">
        <a-input v-model:value="formState.categoryCode" placeholder="请选择种类编号" />
      </a-form-item> -->
      <!-- <a-form-item label="种类名称" name="categoryName">
        <a-select v-model:value="formState.categoryName" :options="skuCategoryName" placeholder="请选择种类名称">
          <a-select-option value="成品">成品</a-select-option>
          <a-select-option value="原材料">原材料</a-select-option>
        </a-select>
      </a-form-item> -->

      <!-- <a-form-item ref="primaryUnit" label="换算单位" name="primaryUnit">
        <a-input v-model:value="formState.primaryUnit" placeholder="请选择换算单位" />
      </a-form-item> -->
      <!-- <a-form-item label="换算单位" name="primaryUnit">
        <a-select v-model:value="formState.primaryUnit" :options="skuPrimaryUnit" placeholder="请选择换算单位">
        </a-select>
      </a-form-item> -->

      <!-- <a-form-item ref="stackType" label="垛形" name="stackType">
        <a-input v-model:value="formState.stackType" placeholder="请选择垛形" style="width: 100%"/>
      </a-form-item>
      <a-form-item ref="ext1" label="颜色" name="ext1">
        <a-input v-model:value="formState.ext1" placeholder="请输入颜色" />
      </a-form-item>
      <a-form-item ref="ext2" label="转速" name="ext2">
        <a-input v-model:value="formState.ext2" placeholder="请输入转速" />
      </a-form-item>
      <a-form-item ref="ext3" label="配置" name="ext3">
        <a-input v-model:value="formState.ext3" @change="valueChanged" placeholder="请输入配置" style="width: 100%"/>
      </a-form-item> -->
      <!-- <a-form-item ref="fullQty" label="满盘数量" name="fullQty">
        <a-input v-model:value="formState.fullQty" placeholder="请输入满盘数量" />
      </a-form-item> -->
      <!-- <a-form-item ref="expire" label="有效期" name="expire">
        <a-input-number v-model:value="formState.expire" placeholder="请选择有效期" style="width: 100%" />
      </a-form-item> -->
      <!--
      <a-form-item label="使用标志" name="useFlag">
        <a-switch v-model:checked="formState.useFlag" checked-children="启用" un-checked-children="禁用"/>
      </a-form-item> -->
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "@hwork/ant-design-vue";
import { edit } from "@/api/wms/sku";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    },
  },
  emit: ["close"],
  setup(props, context) {
    const formRef = ref();

    const formState = reactive({});

    watch(props, (props) => {
      formState.id = props.record.id;
      formState.skuCode = props.record.skuCode;
      formState.skuName = props.record.skuName;
      formState.barCode = props.record.barCode;
      formState.abc = props.record.abc;
      formState.categoryCode = props.record.categoryCode;
      formState.categoryName = props.record.categoryName;
      formState.baseUnit = props.record.baseUnit;
      formState.primaryUnit = props.record.primaryUnit;
      formState.ext3 = props.record.ext3;
      formState.stackType = props.record.stackType;
      formState.ext1 = props.record.ext1;
      formState.ext2 = props.record.ext2;
      formState.fullQty = props.record.fullQty;
      formState.expire = props.record.expire;
      formState.useFlag = props.record.useFlag;
    });

    const formRules = {};

    const editKey = "add";

    const skuAbc = ref([
      {
        value: "A类",
        label: "A类",
      },
      {
        value: "B类",
        label: "B类",
      },
      {
        value: "C类",
        label: "C类",
      },
    ]);

    const skuCategoryName = ref([
      {
        value: "成品",
        label: "成品",
      },
      {
        value: "原材料",
        label: "原材料",
      },
    ]);

    const skuPrimaryUnit = ref([
      {
        value: "台",
        label: "台",
      },
      {
        value: "箱",
        label: "箱",
      },
      {
        value: "盒",
        label: "盒",
      },
    ]);

    const valueChanged = (e) => {
      formState.ext3 = formState.ext3.toFixed(2);
    };

    const submit = (e) => {
      formRef.value
      .validate()
      .then(() => {
          message.loading({
            content: "提交中...",
            key: editKey,
          });
          edit(toRaw(formState)).then((response) => {
            if (response.success) {
              message
                .success({
                  content: "保存成功",
                  key: editKey,
                  duration: 1,
                })
                .then(() => {
                  cancel();
                });
            } else {
              message
                .success({
                  content: "保存失败",
                  key: editKey,
                  duration: 1,
                })
                .then(() => {
                  cancel();
                });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      valueChanged,
      skuAbc,
      skuCategoryName,
      skuPrimaryUnit,

      labelCol: {
        span: 8,
      },
      wrapperCol: {
        span: 12,
      },
    };
  },
});
</script>
