<template>
  <div>
    <el-form
      class="page-form"
      :model="form2"
      ref="form"
      size="small"
      :inline="true"
      label-width="8em"
    >
      <el-row>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item label="基本数量" prop="quantity">
            <el-input-number
              :min="1"
              v-model="form2.quantity"
              placeholder="请输入基本数量"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item label="基本计量单位" prop="unitOfMeasure">
            <el-input
              v-model="form2.unitOfMeasure"
              placeholder="请输入物料清单的基本计量单位"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item
            label="物料清单授权组"
            prop="billOfMaterialsAuthorizationGroup"
          >
            <el-input
              v-model="form2.billOfMaterialsAuthorizationGroup"
              placeholder="请输入物料清单授权组"
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item label="抬头文本描述" prop="remark">
            <el-input v-model="form2.remark" placeholder="抬头文本描述" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider></el-divider>

    <el-table :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="idx" />
      <el-table-column label="组件名称" align="center" prop="itemName" />
      <el-table-column label="项目类别" align="center" prop="itemClass">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sap_item_type"
            :value="scope.row.itemClass"
          />
        </template>
      </el-table-column>
      <el-table-column label="组件数量" align="center" prop="quantity" />
      <el-table-column label="计量单位" align="center" prop="unitOfMeasure" />
      <el-table-column
        label="成本核算"
        align="center"
        prop="costAccountingMarking"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bom_cost_accounting_marking"
            :value="scope.row.costAccountingMarking"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="报废百分比"
        align="center"
        prop="scrapPercentage"
      />
      <el-table-column label="特殊获取" align="center" prop="specialAccess">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bom_special_access"
            :value="scope.row.specialAccess"
          />
        </template>
      </el-table-column>
      <el-table-column label="物料供应" align="center" prop="itemSupplyMarking">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.item_supply_marking"
            :value="scope.row.itemSupplyMarking"
          />
        </template>
      </el-table-column>
      <el-table-column label="替代组" align="center" prop="alternativeGroup" />
      <el-table-column
        label="替代优先级"
        align="center"
        prop="alternativePriorities"
      />
      <el-table-column label="策略" align="center" prop="alternativeStrategy">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.bom_alternative_strategy"
            :value="scope.row.alternativeStrategy"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="使用概率"
        align="center"
        prop="alternativeProbability"
      />
      <el-table-column label="BOM行备注" align="center" prop="bomItemRemark" />
      <el-table-column label="物料备注" align="center" prop="itemRemark" />
      <el-table-column label="固定数量" align="center" prop="isFixed">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.isFixed"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="mode == 'edit'"
        label="操作"
        align="center"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="updateRow(row, $index)"
            >修改</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="mode == 'info'"
        label="操作"
        align="center"
        width="120"
      >
        <template slot-scope="{ row, $index }">
          <el-button
            type="text"
            icon="el-icon-search"
            size="small"
            @click="searchRow(row, $index)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="showFlag" width="700px" title="添加BOM">
      <el-form
        ref="page-form"
        :model="form"
        :rules="rules"
        inline
        label-width="100px"
        size="small"
      >
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="物料" prop="itemId">
              <el-input
                v-model="form.itemName"
                readonly
                placeholder="请选择物料"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="$refs['ItemSelectRef'].showFlag = true"
                ></el-button>
              </el-input>
              <ItemSelect ref="ItemSelectRef" @onSelected="onItemSelect" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="项目类别" prop="itemClass">
              <el-select
                v-model="form.itemClass"
                clearable
                placeholder="请选择项目类别"
              >
                <el-option
                  v-for="dict in dict.type.sap_item_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="组件数量" prop="quantity">
              <el-input
                type="number"
                v-model="form.quantity"
                placeholder="请输入组件数量"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="计量单位" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
                placeholder="请输入计量单位"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="报废百分比" prop="scrapPercentage">
              <el-input
                v-model="form.scrapPercentage"
                placeholder="请输入报废百分比"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="特殊获取" prop="specialAccess">
              <el-select
                v-model="form.specialAccess"
                placeholder="请选择特殊获取"
              >
                <el-option
                  v-for="dict in dict.type.bom_special_access"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="物料供应" prop="itemSupplyMarking">
              <el-select
                v-model="form.itemSupplyMarking"
                placeholder="请选择物料供应标识"
              >
                <el-option
                  v-for="dict in dict.type.item_supply_marking"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="成本核算" prop="costAccountingMarking">
              <el-select
                v-model="form.costAccountingMarking"
                placeholder="请选择是否与成本核算相关"
              >
                <el-option
                  v-for="dict in dict.type.bom_cost_accounting_marking"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="替代组" prop="alternativeGroup">
              <el-input
                v-model="form.alternativeGroup"
                placeholder="请输入替代组"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="替代优先级" prop="alternativePriorities">
              <el-input
                v-model="form.alternativePriorities"
                placeholder="请输入替代优先级"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="策略" prop="alternativeStrategy">
              <el-select
                v-model="form.alternativeStrategy"
                placeholder="请选择策略"
              >
                <el-option
                  v-for="dict in dict.type.bom_alternative_strategy"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="使用概率" prop="alternativeProbability">
              <el-input
                v-model="form.alternativeProbability"
                placeholder="请输入使用概率"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="是否固定数量" prop="isFixed">
              <el-select
                v-model="form.isFixed"
                placeholder="请选择是否固定数量"
              >
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="BOM行备注" prop="bomItemRemark">
              <el-input
                v-model="form.bomItemRemark"
                placeholder="请输入BOM行备注"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item label="物料备注" prop="itemRemark">
              <el-input
                v-model="form.itemRemark"
                placeholder="请输入物料备注"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="!(optType === 'detail')"
          >确定</el-button
        >
        <el-button type="default" @click="showFlag = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ItemBomSelect from "@/components/itemBomSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
export default {
  components: { ItemBomSelect, ItemSelect },
  inject: ["mode"],
  dicts: [
    "bom_cost_accounting_marking",
    "item_supply_marking",
    "sys_yes_no",
    "sap_item_type",
    "bom_alternative_strategy",
    "bom_special_access",
  ],
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**多选 */
      selection: [],
      optType: "add",
      /**表单初始化数据 */
      form2: {
        quantity: "",
        unitOfMeasure: "",
        remark: "",
        billOfMaterialsAuthorizationGroup: "",
      },
      form: {
        bomItemId: "",
        bomHeadId: "",
        idx: "",
        sapItemCode: "",
        itemName: "",
        itemClass: "",
        itemType: "",
        unitOfMeasure: "",
        quantity: "",
        costAccountingMarking: "",
        scrapPercentage: "",
        specialAccess: "",
        itemSupplyMarking: "",
        alternativeGroup: "",
        alternativePriorities: "",
        alternativeStrategy: "",
        alternativeProbability: "",
        bomItemRemark: "",
        itemRemark: "",
        isFixed: "",
      },
      currentRowIdx: undefined,
    };
  },
  created() {},
  methods: {
    /**多选 */
    handleSelectionChange(val) {
      this.selection = val;
    },
    /**删除操作 */
    handleDelete() {},
    /**添加操作 */
    handleAdd() {
      this.resetForm();
      this.showFlag = true;
      this.optType = "add";
    },
    /**更新行 */
    updateRow(row, idx) {
      Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "edit";
      this.showFlag = true;
    },
    /**删除行 */
    deleteRow(index) {
      this.tableData.splice(index, 1);
    },
    onItemBomSelect(row) {
      // if (row != undefined && row != null) {
      //   this.tableData.unshift({...row})
      // }
    },
    onItemSelect(row) {
      if (row != undefined && row != null) {
        this.form.itemId = row.itemId;
        this.form.itemName = row.itemName;
        this.form.unitOfMeasure = row.unitOfMeasure;
        this.form.itemRemark = row.remark;
      }
    },
    /**表单提交 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.optType == "add") {
            this.tableData.unshift({ ...this.form });
          } else {
            this.tableData.splice(this.currentRowIdx, 1, { ...this.form });
          }
          this.showFlag = false;
        }
      });
    },
    /**重置表单 */
    resetForm() {
      this.form = {
        quantity: "",
        unitOfMeasure: "",
        remark: "",
        billOfMaterialsAuthorizationGroup: "",
      };
    },
    /**重置组件状态 */
    resetState() {
      this.resetForm();
      this.tableData = [];
    },
    /**获取组件数据 */
    getComData() {
      return {
        ...this.form2,
        bomItemList: this.tableData,
      };
    },
    /**查看行 */
    searchRow(row, idx) {
      this.form = Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      this.showFlag = true;
    },
  },
};
</script>

<style></style>
