<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="$t('基本数量')" prop="quantity">
            <el-input-number
              :min="1"
              v-model="form2.quantity"
             
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item :label="$t('基本计量单位')" prop="unitOfMeasure">
            <el-input
              v-model="form2.unitOfMeasure"
             
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item
            :label="$t('物料清单授权组')"
            prop="billOfMaterialsAuthorizationGroup"
          >
            <el-input
              v-model="form2.billOfMaterialsAuthorizationGroup"
             
            />
          </el-form-item>
        </el-col>
        <el-col :lg="6" :md="8" :sm="12" :xs="12">
          <el-form-item :label="$t('抬头文本描述')" prop="remark">
            <el-input v-model="form2.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-divider></el-divider>

    <el-table :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column :label="$t('role.idx')" align="center" prop="idx" />
      <el-table-column :label="$t('组件名称')" align="center" prop="itemName" />
      <el-table-column :label="$t('项目类别')" 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="$t('组件数量')" align="center" prop="quantity" />
      <el-table-column :label="$t('计量单位')" align="center" prop="unitOfMeasure" />
      <el-table-column
        :label="$t('成本核算')"
        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="$t('报废百分比')"
        align="center"
        prop="scrapPercentage"
      />
      <el-table-column :label="$t('特殊获取')" 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="$t('物料供应')" 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="$t('替代组')" align="center" prop="alternativeGroup" />
      <el-table-column
        :label="$t('替代优先级')"
        align="center"
        prop="alternativePriorities"
      />
      <el-table-column :label="$t('策略')" 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="$t('使用概率')"
        align="center"
        prop="alternativeProbability"
      />
      <el-table-column :label="$t('BOM行备注')" align="center" prop="bomItemRemark" />
      <el-table-column :label="$t('物料备注')" align="center" prop="itemRemark" />
      <el-table-column :label="$t('固定数量')" 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="$t('common.option')"
        align="center"
        fixed="right"
      >
        <template slot-scope="{ row, $index }">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="updateRow(row, $index)"
            >{{ $t('common.edit') }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="mode == 'info'"
        :label="$t('common.option')"
        align="center"
        width="120"
      >
        <template slot-scope="{ row, $index }">
          <el-button
            type="text"
            icon="el-icon-search"
            size="small"
            @click="searchRow(row, $index)"
            >{{$t('查看详情')}}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="showFlag" width="700px" :title="$t('添加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="$t('物料')" prop="itemId">
              <el-input
                v-model="form.itemName"
                readonly
               
              >
                <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="$t('项目类别')" prop="itemClass">
              <el-select
                v-model="form.itemClass"
                clearable
               
              >
                <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="$t('组件数量')" prop="quantity">
              <el-input
                type="number"
                v-model="form.quantity"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('计量单位')" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

       
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('报废百分比')" prop="scrapPercentage">
              <el-input
                v-model="form.scrapPercentage"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('特殊获取')" prop="specialAccess">
              <el-select
                v-model="form.specialAccess"
               
              >
                <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="$t('物料供应')" prop="itemSupplyMarking">
              <el-select
                v-model="form.itemSupplyMarking"
               
              >
                <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="$t('成本核算')" prop="costAccountingMarking">
              <el-select
                v-model="form.costAccountingMarking"
               
              >
                <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-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('替代组')" prop="alternativeGroup">
              <el-input
                v-model="form.alternativeGroup"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('替代优先级')" prop="alternativePriorities">
              <el-input
                v-model="form.alternativePriorities"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('策略')" prop="alternativeStrategy">
              <el-select
                v-model="form.alternativeStrategy"
               
              >
                <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="$t('使用概率')" prop="alternativeProbability">
              <el-input
                v-model="form.alternativeProbability"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('是否固定数量')" prop="isFixed">
              <el-select
                v-model="form.isFixed"
               
              >
                <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="$t('BOM行备注')" prop="bomItemRemark">
              <el-input
                v-model="form.bomItemRemark"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="8" :sm="12" :xs="12">
            <el-form-item :label="$t('物料备注')" prop="itemRemark">
              <el-input
                v-model="form.itemRemark"
               
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="!(optType === 'detail')"
          >{{ $t('common.confirm') }}</el-button
        >
        <el-button type="default" @click="showFlag = false">{{ $t('common.cancel') }}</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>
