SpareAdd.jsx 11.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
import React, { useState, useEffect } from "react";

import {
  Modal,
  Button,
  Input,
  Divider,
  Table,
  Typography,
  Row,
  Col,
  Space,
  Select,
  // Popconfirm
} from "antd";

import {
  PlusCircleFilled,
  DeleteOutlined,
  ExclamationCircleOutlined,
  OrderedListOutlined,
  SaveOutlined,
} from "@ant-design/icons";

// import Service
import {
  FetchSpareByDetailID,
  InsertSpare,
  DeleteSpare,
  UpdateDetail,
} from "../../../services";

const { Column, ColumnGroup } = Table;
const { Text, Title } = Typography;
const { confirm } = Modal;
const { Option } = Select;

export default function SpareAdd(props) {
  const {detailID, memberTel} = props;
  // console.log(memberTel, detailID)

  // Set State
  const [visible, setVisible] = useState(false);
  const [confirmLoading, setConfirmLoading] = useState(false);
  const [datas, setDatas] = useState([]);
  const [status, setStatus] = useState('อยู่ระหว่างการซ่อม');
  const [payment, setPayment] = useState('ยังไม่ได้ชำระ');

  // state spare
  const [spareName, setSpareName] = useState();
  const [qty, setQTY] = useState();
  const [qtyPrice, setQTYPrice] = useState();
  const [sumPrice, setSumPrice] = useState(0);

  useEffect(() => {
    const getSpare = async () => {
      let data = {
        detailID: detailID
      }
      await FetchSpareByDetailID(data).then((response) => {
        if (response.data) {
          setDatas(response.data);
        }
      });
      setSumPrice(qty * qtyPrice);
    };
    getSpare();
  }, [detailID, qty, qtyPrice]);

  const showModal = () => {
    setVisible(true);
  };

  let listData = [];
  let sum = 0;
  if (datas.length !== 0) {
    for (let i = 0; i < datas.length; i++) {
      let toarr = JSON.parse(datas[i].spare);
      sum = sum + toarr.sumPrice;
      let newData = {
        spareID: datas[i].spareID,
        spareName: toarr.spareName,
        qty: toarr.qty,
        qtyPrice: toarr.qtyPrice,
        sumPrice: toarr.sumPrice,
      };

      listData.push(newData);
    }
  }

  // console.log(listData);

  const handleOk = () => {
    let listSpare = {
      spareName: spareName,
      qty: qty,
      qtyPrice: qtyPrice,
      sumPrice: sumPrice,
    };

    // let sum = 0;
    // sum = sum +

    const data = {
      detailsID: detailID,
      memberTel: memberTel,
      spare: JSON.stringify(listSpare),
    };

    // console.log('data', data)

    if (spareName === "" || spareName === null || spareName === undefined) {
      Modal.info({
        title: "ข้อมูลไม่ครบ",
        content: `กรุณากรอกรายการการซ่อม`,
      });
    } else {
      InsertSpare(data);
    }

    // setModalText("The modal will be closed after two seconds");
    setConfirmLoading(true);
    setTimeout(() => {
      setVisible(false);
      setConfirmLoading(false);

      setSpareName(null);
      setQTY();
      setQTYPrice();
      setSumPrice(0);
    }, 2000);
  };

  const handleCancel = () => {
    // console.log("Clicked cancel button");
    setVisible(false);
  };

  function destroyAll() {
    Modal.destroyAll();
  }

  const handleDelete = (id) => {
    confirm({
      icon: <ExclamationCircleOutlined />,
      content: <Button onClick={destroyAll}>คลิ๊ก ok เพื่อลบรายการนี้</Button>,
      onOk() {
        // console.log("detele", id);
        DeleteSpare(id);
      },
      onCancel() {
        console.log("Cancel");
      },
    });
  };

  const handleSave = () => {
   
    let data = {
      status: status,
      status_payment: payment,
      sumPrice: sum,
      detailsID: detailID
    }
    // console.log('data', data)
    UpdateDetail(data)
    setTimeout(() => {
      window.location.reload(false)
    }, 1);
  }

  return (
    <>
      <Divider />

      <div className="div-p-5">
        <Button className="bt-them" onClick={showModal}>
          <Text className="color-fff-hover">
            <PlusCircleFilled /> เพิ่มรายการซ่อม
          </Text>
        </Button>

        <Table
          dataSource={listData}
          scroll={{ x: 400 }}
          bordered
          className="border"
        >
          <ColumnGroup title="รายละเอียดการซ่อม">
            <Column
              title="ลำดับ"
              width={"10%"}
              render={(value, item, index) => index + 1}
            />
            <Column
              title="รายการซ่อม / อะไหล่"
              dataIndex="spareName"
              key="spareName"
              width={"48%"}
            />

            <Column title="จำนวน" dataIndex="qty" key="qty" width={"10%"} />

            <Column
              title="ราคาต่อหน่วย"
              dataIndex="qtyPrice"
              key="qtyPrice"
              width={"12%"}
            />
            <Column
              title="ราคารวม"
              dataIndex="sumPrice"
              key="sumPrice"
              width={"10%"}
            />

            <Column
              title="ลบข้อมูล"
              width={"10%"}
              key="spairID"
              render={(text, record, key) => (
                <Space size="middle">
                  <Button
                    className="bt-delete"
                    onClick={() => {
                      handleDelete(record.spareID);
                    }}
                  >
                    <DeleteOutlined />
                  </Button>
                </Space>
              )}
            />
          </ColumnGroup>
        </Table>
      </div>

      {/* สรุปรายการซ่อม */}
      {listData.length !== 0 ? (
        <div>
          <Row gutter={[0, 16]}>
            <Col lg={{ span: 11, offset: 1 }}>
              <Title level={5}>
                <Text>
                  <OrderedListOutlined /> สรุปรายการซ่อม
                </Text>
              </Title>
              <div className="border">
                <Row style={{ marginTop: "10px" }}>
                  <Col span={11} offset={1}>
                    <Title level={5}>
                      <Text>รายการ</Text>
                    </Title>
                  </Col>
                  <Col span={5} offset={1}>
                    <Title level={5}>
                      <Text>จำนวน</Text>
                    </Title>
                  </Col>
                  <Col span={5} offset={1}>
                    <Title level={5}>
                      <Text>ราคา</Text>
                    </Title>
                  </Col>
                  {/* <Divider /> */}

                  {listData.map((val) => {
                    return (
                      <>
                        <Col span={11} offset={1}>
                          {val.spareName}
                        </Col>
                        <Col span={5} offset={1}>
                          {val.qty}
                        </Col>
                        <Col span={5} offset={1}>
                          {val.sumPrice}
                        </Col>
                      </>
                    );
                  })}
                  <Divider />
                  <Col span={11} offset={1}>
                    <Title level={5}>
                      <Text>ยอดสุทธิ</Text>
                    </Title>
                  </Col>
                  <Col span={11} offset={1}>
                    <Title level={5}>
                      <Text style={{ color: "red" }}>{sum} บาท</Text>
                    </Title>
                  </Col>
                </Row>
              </div>
            </Col>

            {/* อัพเดตสถานะ Update Status */}
            <Col xs={24} sm={24} md={24} lg={{ span: 11, offset: 1 }}>
              <Title level={5}>
                <Text>
                <SaveOutlined /> อัพเดตสถานะ
                </Text>
              </Title>
              <div className="border">
                <Row style={{ marginTop: "5%" }}>
                  <Col span={10} offset={1}>
                    <Title level={5}>
                      <Text>สถานะการซ่อม</Text>
                    </Title>
                    <div className="div-select">
                      <Select
                        allowClear
                        style={{ width: "100%" }}
                        defaultValue="อยู่ระหว่างการซ่อม"
                        onChange={(e) => {
                          setStatus(e);
                          // console.log(e)
                        }}
                      >
                        <Option value="อยู่ระหว่างการซ่อม">
                          อยู่ระหว่างการซ่อม
                        </Option>
                        <Option value="สำเร็จ">สำเร็จ</Option>
                      </Select>
                    </div>
                  </Col>

                  <Col span={11} offset={1}>
                    <Title level={5}>
                      <Text>การชำระเงิน</Text>
                    </Title>
                    <div className="div-select">
                      <Select
                        allowClear
                        style={{ width: "100%" }}
                        defaultValue={{ value: 'ยังไม่ได้ชำระ' }}
                        
                        onChange={(e) => {
                          setPayment(e);
                        }}
                      >
                        <Option value="ยังไม่ได้ชำระ">ยังไม่ได้ชำระ</Option>
                        <Option value="ชำระแล้ว">ชำระแล้ว</Option>
                      </Select>
                    </div>
                  </Col>

                  <Divider />

                  <Col span={24}>
                    <Button className="bt-them" onClick={handleSave}>
                      <Text className="color-fff-hover">
                        <SaveOutlined /> บันทึก
                      </Text>
                    </Button>
                  </Col>
                </Row>
              </div>
            </Col>
          </Row>
        </div>
      ) : null}

      {/* Model Pop Up */}
      <Modal
        title="เพิ่มรายการซ่อม / อะไหล่"
        visible={visible}
        onOk={handleOk}
        confirmLoading={confirmLoading}
        onCancel={handleCancel}
        width={1000}
      >
        <Row>
          <Col lg={{ span: 11, offset: 1 }}>
            <Text>รายการ</Text>
            <Input
              required={true}
              value={spareName}
              onChange={(e) => {
                setSpareName(e.target.value);
              }}
            />
          </Col>
          <Col lg={{ span: 3, offset: 1 }}>
            <Text>จำนวน</Text>
            <Input
              required={true}
              type="number"
              value={qty}
              onChange={(e) => {
                setQTY(e.target.value);
              }}
            />
          </Col>
          <Col lg={{ span: 3, offset: 1 }}>
            <Text>ราคาต่อหน่วย</Text>
            <Input
              required={true}
              type="number"
              value={qtyPrice}
              onChange={(e) => {
                setQTYPrice(e.target.value);
              }}
            />
          </Col>
          <Col lg={{ span: 3, offset: 1 }}>
            <Text>ยอดรวม</Text>
            <Input type="number" value={sumPrice} />
          </Col>
        </Row>
      </Modal>
    </>
  );
}