<template>
  <div class="app-container">
    <el-row v-if="optType != 'view'" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:pro:bom:add']"
          >{{ $t('common.add') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:pro:bom:edit']"
          >{{ $t('common.edit') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:pro:bom:remove']"
          >{{ $t('common.del') }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:pro:bom:export']"
          >{{ $t('common.export') }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="bomItemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :label="$t('物料名称')"
        fixed="left"
        width="120"
        align="center"
        prop="itemName"
      />
      <el-table-column
        :label="$t('组件数量')"
        align="center"
        prop="quantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('计量单位')"
        align="center"
        prop="unitOfMeasure"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('报废百分比')"
        width="120"
        align="center"
        prop="scrapPercentage"
        :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('是否启用')" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.enableFlag"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('替代组')"
        align="center"
        prop="alternativeGroup"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('替代优先级')"
        width="120"
        align="center"
        prop="alternativePriorities"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('替代策略')"
        width="120"
        align="center"
        prop="alternativeStrategy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('替代使用概率')"
        width="120"
        align="center"
        prop="alternativeProbability"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('BOM行备注')"
        align="center"
        width="120"
        prop="bomItemRemark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('物料备注')"
        width="150"
        align="center"
        prop="itemRemark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('common.option')"
        width="150px"
        v-if="optType != 'view'"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-link
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:bom:edit']"
            >{{ $t('common.edit') }}</el-link
          >
          <el-link
            size="mini"
            style="margin-left: 10px"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:bom:remove']"
            >{{ $t('common.del') }}</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产BOM内容对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('物料编码')" prop="itemCode">
              <el-input v-model="form.itemCode">
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleItemSelect"
                ></el-button>
              </el-input>
              <MdItem ref="woSelect" @onSelected="onItemSelected"></MdItem>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('物料名称')" prop="itemName">
              <el-input v-model="form.itemName" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('组件数量')" prop="quantity">
              <el-input v-model="form.quantity" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('计量单位')" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" disabled />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item :label="$t('报废百分比')" prop="scrapPercentage">
              <el-input
                v-model="form.scrapPercentage"
               
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('替代组')" prop="alternativeGroup">
              <el-input
                v-model="form.alternativeGroup"
               
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('替代优先级')" prop="alternativePriorities">
              <el-input
                v-model="form.alternativePriorities"
               
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item :label="$t('替代策略')" prop="alternativeStrategy">
              <el-input
                v-model="form.alternativeStrategy"
               
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="$t('替代使用概率')" prop="alternativeProbability">
              <el-input
                v-model="form.alternativeProbability"
               
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="$t('是否启用')" prop="enableFlag">
              <el-radio-group
                v-model="form.enableFlag"
                disabled
                v-if="optType == 'view'"
              >
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
              <el-radio-group v-model="form.enableFlag" v-else>
                <el-radio
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item :label="$t('BOM行备注')" prop="bomItemRemark">
              <el-input
                v-model="form.bomItemRemark"
                type="textarea"
               
              />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item :label="$t('物料备注')" prop="itemRemark">
              <el-input
                v-model="form.itemRemark"
                type="textarea"
               
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import {
  listBomItem,
  getBomItem,
  delBomItem,
  addBomItem,
  updateBomItem,
} from "@/api/mes/pro/bomItem";
import MdItem from "@/components/itemSelect/single.vue";
import { listMdItem } from "@/api/mes/md/mdItem";
export default {
  name: "BomItem",
  dicts: ["sys_yes_no"],
  components: { MdItem },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产BOM内容表格数据
      bomItemList: [],
      // 物料选项
      itemOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomId: this.bomId,
        itemId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // bomId: [{ required: true, message: "BOM不能为空", trigger: "blur" }],
      },
    };
  },
  props: {
    bomId: undefined,
    optType: undefined,
  },
  created() {
    this.getList();
    // this.getMdItem();
  },
  methods: {
    /** 查询生产BOM内容列表 */
    getList() {
      this.loading = true;
      listBomItem(this.queryParams).then((response) => {
        this.bomItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询物料信息
    getMdItem() {
      listMdItem().then((response) => {
        this.itemOptions = response.data;
      });
    },
    handleItemSelect() {
      this.$refs.woSelect.showFlag = true;
    },
    onItemSelected(row) {
      if (row != undefined && row != null) {
        this.form.itemId = row.itemId;
        this.form.itemCode = row.itemCode;
        this.form.itemName = row.itemName;
        this.form.itemRemark = row.itemRemark;
        this.form.unitOfMeasure = row.unitOfMeasure;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bomItemId: null,
        bomId: null,
        itemId: null,
        itemName: null,
        quantity: null,
        unitOfMeasure: null,
        scrapPercentage: null,
        alternativeGroup: null,
        alternativePriorities: null,
        alternativeStrategy: null,
        alternativeProbability: null,
        bomItemRemark: null,
        itemRemark: null,
        enableFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bomItemId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加操作步骤');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bomItemId = row.bomItemId || this.ids;
      getBomItem(bomItemId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改操作步骤');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bomItemId != null) {
            updateBomItem(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            const params = {
              ...this.form,
              bomId: this.bomId,
            };
            addBomItem(params).then((response) => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bomItemIds = row.bomItemId || this.ids;
      this.$modal
        .confirm(this.$t('是否确认删除操作步骤？'))
        .then(function () {
          return delBomItem(bomItemIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('common.del_success'));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/bomItem/export",
        {
          ...this.queryParams,
        },
        `bomItem_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
