<template>
  <div>
    <el-row :gutter="10" class="mb8" v-if="!(mode === 'info'|| mode === 'make' || mode === 'makeInfo')">
      <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="sapItemCode" />
      <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="isBackflush"
        width="120"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.isBackflush"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="!(mode === 'info'|| mode === 'make' || mode === 'makeInfo')"
        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-column
        v-if="mode == 'info'|| mode == 'make'"
        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="900px" title="添加BOM">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        size="small"
      >
        <el-row>
          <el-col :span="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"
                  v-if="mode != 'info'"
                ></el-button>
              </el-input>
              <ItemSelect ref="ItemSelectRef" @onSelected="onItemSelect" />
            </el-form-item>
          </el-col>
          <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="storeAddr">
              <el-input
                v-model="form.storeAddr"
                placeholder="请输入存储地点"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位用量" prop="unitUsage">
              <el-input
                type="number"
                min="1"
                v-model="form.unitUsage"
                placeholder="请输入单位用量"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="虚拟项目标识" prop="virtualProjectLog">
              <el-select
                v-model="form.virtualProjectLog"
                style="width:100%;"
                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 :span="12">
            <el-form-item label="存储地点2" prop="storeAddr2">
              <el-input
                v-model="form.storeAddr2"
                placeholder="请输入存储地点2"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否倒冲" prop="isBackflush">
              <el-select
                v-model="form.isBackflush"
                style="width:100%;"
                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-row>
      </el-form>
      <div slot="footer">
        <el-button
          v-if="!(optType === 'detail')"
          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"],
  dicts: ["sys_yes_no"],
  data() {
    return {
      /**弹窗状态 */
      showFlag: false,
      /**表格数据 */
      tableData: [],
      /**表单规则 */
      rules: {},
      /**多选 */
      selection: [],
      optType: "add",
      /**表单初始化数据 */
      form: {
        quantity: "",
        itemName: "",
        sapItemCode: "",
        itemId: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        isBackflush: "",
        virtualProjectLog: "",
        storeAddr: "",
        unitUsage: "",
        storeAddr2: "",
        processRef: "",
        requirementNumber: "",
        requirementProjectNumber: "",
      },
      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;
    },
    /**查看行 */
    searchRow(row, idx) {
      this.form = Object.assign(this.form, row);
      this.currentRowIdx = idx;
      this.optType = "detail";
      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: "",
        itemName: "",
        itemId: "",
        unitOfMeasure: "",
        scrapPercentage: "",
        isBackflush: "",
        virtualProjectLog: "",
        sapItemCode: "",
        storeAddr: "",
        unitUsage: "",
        storeAddr2: "",
        processRef: "",
        requirementNumber: "",
        requirementProjectNumber: "",
      };
    },
    /**重置组件状态 */
    resetState() {
      this.resetForm();
      this.tableData = [];
    },
    /**获取组件数据 */
    getComData() {
      return this.tableData;
    },
  },
};
</script>

<style></style>
