<template>
  <div>
    <el-row :gutter="10" class="mb8" v-if="mode != 'info'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"  align="center" />
      <el-table-column
        label="物料名称"
        align="center"
        prop="itemName"
      />
      <el-table-column
        label="组件数量"
        align="center"
        prop="quantity"
        width="120"
      />
      <el-table-column
        label="计量单位"
        align="center"
        prop="unitOfMeasure"
        width="120"
      />
      <el-table-column
        label="替代组"
        align="center"
        prop="alternativeGroup"
        width="120"
      />
      <el-table-column
        v-if="mode != 'info'"
        label="操作"
        align="center"
        width="120"
      >
        <template slot-scope="{ row, $index }">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="updateRow(row, $index)"
            >修改</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            size="small"
            @click="deleteRow($index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="showFlag" width="700px" title="添加BOM">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        inline
        label-width="100px"
        size="small"
      >
        <el-row>
          <el-col :span="24">
            <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-row>
        <el-row>
          <el-col :span="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 :span="12">
            <el-form-item label="计量单位" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
                placeholder="请输入计量单位"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报废百分比" prop="scrapPercentage">
              <el-input
                v-model="form.scrapPercentage"
                placeholder="请输入报废百分比"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="替代组" prop="alternativeGroup">
              <el-input
                v-model="form.alternativeGroup"
                placeholder="请输入替代组"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="替代优先级" prop="alternativePriorities">
              <el-input
                v-model="form.alternativePriorities"
                placeholder="请输入替代优先级"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="策略" prop="alternativeStrategy">
              <el-input
                v-model="form.alternativeStrategy"
                placeholder="请输入策略"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用概率" prop="alternativeProbability">
              <el-input
                v-model="form.alternativeProbability"
                placeholder="请输入使用概率"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="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 :span="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">确定</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"],
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**多选 */
      selection: [],
      optType: "add",
      /**表单初始化数据 */
      form: {
        quantity: "",
        itemName: "",
        itemId: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        alternativeGroup: "",
        alternativePriorities: "",
        alternativeStrategy: "",
        alternativeProbability: "",
        bomItemRemark: "",
        itemRemark: "",
      },
      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 = {
        itemName: "",
        itemId: "",
        quantity: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        alternativeGroup: "",
        alternativePriorities: "",
        alternativeStrategy: "",
        alternativeProbability: "",
        bomItemRemark: "",
        itemRemark: "",
      };
    },
    /**重置组件状态 */
    resetState() {
      this.resetForm();
      this.tableData = [];
    },
    /**获取组件数据 */
    getComData() {
      return this.tableData;
    },
  },
};
</script>

<style></style>
