<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"
          >{{ $t('common.add') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >{{ $t('common.del') }}</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="$t('物料名称')"
        align="center"
        prop="itemName"
        width="120"
      />
      <el-table-column
        :label="$t('组件数量')"
        align="center"
        prop="quantity"
        width="120"
      />
      <el-table-column
        :label="$t('计量单位')"
        align="center"
        prop="unitOfMeasure"
        width="120"
      />
      <el-table-column
        :label="$t('报废百分比')"
        align="center"
        prop="scrapPercentage"
        width="120"
      />
      <el-table-column
        :label="$t('替代组')"
        align="center"
        prop="alternativeGroup"
        width="120"
      />
      <el-table-column
        :label="$t('替代优先级')"
        align="center"
        prop="alternativePriorities"
        width="120"
      />
      <el-table-column
        :label="$t('策略')"
        align="center"
        prop="alternativeStrategy"
        width="120"
      />
      <el-table-column
        :label="$t('使用概率')"
        align="center"
        prop="alternativeProbability"
        width="120"
      />
      <el-table-column
        :label="$t('BOM行备注')"
        align="center"
        prop="bomItemRemark"
        min-width="120"
      />
      <el-table-column
        :label="$t('物料备注')"
        align="center"
        prop="itemRemark"
        min-width="120"
      />
      <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-edit"
            size="small"
            @click="updateRow(row, $index)"
            >{{ $t('common.edit') }}</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            size="small"
            @click="deleteRow($index)"
            >{{ $t('common.del') }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="showFlag" width="700px" :title="$t('添加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="$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-row>
        <el-row>
          <el-col :span="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 :span="12">
            <el-form-item :label="$t('计量单位')" prop="unitOfMeasure">
              <el-input
                v-model="form.unitOfMeasure"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('报废百分比')" prop="scrapPercentage">
              <el-input
                v-model="form.scrapPercentage"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('替代组')" prop="alternativeGroup">
              <el-input
                v-model="form.alternativeGroup"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('替代优先级')" prop="alternativePriorities">
              <el-input
                v-model="form.alternativePriorities"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('策略')" prop="alternativeStrategy">
              <el-input
                v-model="form.alternativeStrategy"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('使用概率')" prop="alternativeProbability">
              <el-input
                v-model="form.alternativeProbability"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('BOM行备注')" prop="bomItemRemark">
              <el-input
                v-model="form.bomItemRemark"
               
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="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">{{ $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"],
  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>
