<script>
import {bignumber, chain, multiply} from "mathjs";
import {erpDetail, erpUpdate} from "@/api/inventory/erp";

export default {
  props: {
    id: {type: [String, Number], required: true},
    editable: {type: Boolean, default: false},
    selectable: {type: Boolean, default: false},
  },
  data() {
    return {
      loading: false,
      model: {},
      calibreOptions: [
        {dictLabel: '合格', dictValue: '0'},
        {dictLabel: '不合格', dictValue: '1'},
      ],

      selected: [],
    }
  },
  watch: {
    id(value, old) { if (value && value !== old) this.getData() },
  },
  created() {
    if (this.id) this.getData();
  },
  methods: {
    async getData() {
      this.loading = true;
      try {
        const model = await erpDetail(this.id).then(res => res.data || {});
        model.details = Array.isArray(model.details) ? model.details.map(item => {
          if (!item.actualWarehousingAmount) item.actualWarehousingAmount = item.planWarehousingAmount;
          return {
            ...item, calibreStatus: item.calibreStatus || '0',
            convertFactorValue: multiply(bignumber(item.convertFactor), bignumber(item.actualWarehousingAmount)).valueOf(),
          }
        }) : []
        this.model = model;
        this.selected = model.details;
        this.$refs.table.toggleAllSelection();
      } catch (error) {}
      this.loading = false;
    },
    updateRow(row, value) {
      value = bignumber(value || 0);
      row.actuaPrice = multiply(value, bignumber(row.singlePrice)).valueOf();
      row.convertFactorValue = multiply(value, bignumber(row.convertFactor)).valueOf();
    },
    selectRow(rows) {
      this.selected = rows;
      let totalActualWarehousingAmount = chain(bignumber(0));
      let totalActuaPrice = chain(bignumber(0));
      for (const item of this.selected) {
        totalActualWarehousingAmount = totalActualWarehousingAmount.add(bignumber(item.actualWarehousingAmount));
        totalActuaPrice = totalActuaPrice.add(bignumber(item.actuaPrice));
      }
      this.model.actualWarehousingAmount = totalActualWarehousingAmount.value.valueOf();
      this.model.actuaPrice = totalActuaPrice.value.valueOf();
    },
    async handle() {
      if (!this.selected.length) return this.msgInfo('请至少选择一条数据');
      let totalActualWarehousingAmount = chain(bignumber(0));
      let totalActuaPrice = chain(bignumber(0));
      for (const item of this.selected) {
        if (!item.actualWarehousingAmount) return this.msgError('请完善实际入库数据');
        totalActualWarehousingAmount = totalActualWarehousingAmount.add(bignumber(item.actualWarehousingAmount));
        totalActuaPrice = totalActuaPrice.add(bignumber(item.actuaPrice));
      }
      try {
        await erpUpdate({
          // ...this.model,
          id: this.id,
          actualWarehousingAmount: totalActualWarehousingAmount.value.valueOf(),
          actuaPrice: totalActuaPrice.value.valueOf(),
          details: this.selected,
        })
        this.$emit('close', true);
      } catch (error) {}
    },
    cancel() {
      this.$emit('close', false);
    },
    handleDelete() {
      this.$emit('delete', {id: this.id});
    },
  }
}
</script>

<template>
  <div>
    <el-form ref="form" :model="model"
             label-width="80px" label-position="top"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="ERP订单号" prop="erpNo">
            <el-input style="width: 100%" v-model="model.erpNo" placeholder="请输入ERP订单号"
                      disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="操作时间" prop="updateTime">
            <el-input style="width: 100%" v-model="model.updateTime" placeholder="请输入操作时间"
                      disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="供货单位" prop="supplierName">
            <el-input style="width: 100%" v-model="model.supplierName" placeholder="请输入供货单位"
                      disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="计划入库" prop="planWarehousingAmount">
            <el-input style="width: 100%" v-model="model.planWarehousingAmount" placeholder="请输入计划入库"
                      disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实际入库" prop="updateTime">
            <el-input style="width: 100%" v-model="model.actualWarehousingAmount" placeholder="请输入实际入库"
                      disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="计划金额" prop="planPrice">
            <el-input style="width: 100%" v-model="model.planPrice" placeholder="请输入计划金额"
                      disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="实际金额" prop="actuaPrice">
            <el-input style="width: 100%" v-model="model.actuaPrice" placeholder="请输入实际金额"
                      disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-table ref="table" :data="model.details"
                v-loading="loading"
                @selection-change="selectRow">
        <el-table-column v-if="selectable && editable" type="selection" width="55" align="center"/>
        <el-table-column label="序号" type="index" width="65" align="center"></el-table-column>
        <el-table-column label="药品名称" prop="drugsName" width="110" align="center"></el-table-column>
        <el-table-column label="药品规格" prop="specsName" width="110" align="center"></el-table-column>
        <el-table-column label="供货单位" prop="supplierName" width="110" align="center"></el-table-column>
        <el-table-column label="药品产地" prop="placeName" width="110" align="center"></el-table-column>
        <el-table-column label="药品单价" prop="singlePrice" width="110" align="center"></el-table-column>
        <el-table-column label="生产批号" prop="productionCode" width="110" align="center"></el-table-column>
        <el-table-column label="计划入库" prop="planWarehousingAmount" width="110" align="center"></el-table-column>
        <el-table-column label="实际入库" prop="actualWarehousingAmount" width="110" align="center">
          <template slot-scope="scope">
            <el-input-number v-if="editable" style="width: 100%;" v-model="scope.row.actualWarehousingAmount"
                             :min="0"
                             controls-position="right"
                             placeholder="实际入库" :disabled="!editable"
                             @change="updateRow(scope.row, $event)"
            />
            <span v-else>{{ scope.row.actualWarehousingAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="转化系数" prop="convertFactor" width="110" align="center"></el-table-column>
        <el-table-column label="转化后数量" prop="convertFactorValue" width="110" align="center"></el-table-column>
        <el-table-column label="成本价格" prop="costPrice" width="110" align="center"></el-table-column>
        <el-table-column label="实际金额" prop="actuaPrice" width="110" align="center"></el-table-column>
        <el-table-column label="生产日期" prop="produceTime" width="110" align="center"></el-table-column>
        <el-table-column label="有效日期" prop="validTime" width="110" align="center"></el-table-column>
        <el-table-column label="质量情况" prop="calibreStatus" width="110" align="center">
          <template slot-scope="scope">
            <el-select v-if="editable" v-model="scope.row.calibreStatus" placeholder="质量情况">
              <el-option v-for="item in calibreOptions" :key="item.dictValue" :value="item.dictValue"
                         :label="item.dictLabel"></el-option>
            </el-select>
            <dict-tag v-else :options="calibreOptions" :value="scope.row.calibreStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" width="110" align="center">
          <template slot-scope="scope">
            <el-input v-if="editable" v-model="scope.row.remark" placeholder="备注"/>
            <span v-else>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div v-if="editable" class="el-dialog__footer">
      <el-button type="primary" @click="handle" :disabled="selected.length === 0">接 收</el-button>
      <el-button v-hasPermi="['inventory:erp:remove']" type="danger" @click="handleDelete">作 废</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>

