<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item :label="$t('BOM编码')" prop="bomCode">
        <el-input
          v-model="queryParams.bomCode"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('产品名称')" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('用途名称')" prop="usageName">
        <el-input
          v-model="queryParams.usageName"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('是否启用')" prop="enableFlag">
        <el-input
          v-model="queryParams.enableFlag"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t('common.search') }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{ $t('common.reset') }}</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :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="bomList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('BOM编码')" width="130" align="center" prop="bomCode">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:bom:query']"
            >{{ scope.row.bomCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column :label="$t('产品名称')" fixed="left" width="120" align="center" prop="itemName" />
      <el-table-column :label="$t('生产用途')" align="center" prop="usageName" />
      <el-table-column :label="$t('基本数量')" align="center" prop="quantity" />
      <el-table-column
        :label="$t('基本计量单位')"
        width="125"
        align="center"
        prop="unitOfMeasure"
      />
      <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="expirationDateStart"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.expirationDateStart, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('有效期结束')"
        align="center"
        prop="expirationDateEnd"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.expirationDateEnd, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('ECN编码')"  width="120" align="center" prop="ecnCode" />
      <el-table-column
        :label="$t('单层厚')"
        width="120"
        align="center"
        prop="singleLayerThickness"
      />
      <el-table-column :label="$t('单重')"  width="120" align="center" prop="singleWeight" />
      <el-table-column :label="$t('排版行数')"  width="120" align="center" prop="rowNum" />
      <el-table-column
        :label="$t('派工倍数')"
        width="120"
        align="center"
        prop="dispatchMultiple"
      />
      <el-table-column :label="$t('是否启用')"  width="120" 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('common.remark')"
        width="120"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('common.option')"
        align="center"
        width="130"
        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
          style="margin-left: 10px"
            size="mini"
            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="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('BOM编码')" prop="bomCode">
              <el-input v-model="form.bomCode" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label-width="80">
              <el-switch
                v-model="autoGenFlag"
                active-color="#13ce66"
                :active-text="$t('自动生成')"
                @change="handleAutoGenChange(autoGenFlag)"
                v-if="optType != 'view'"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item :label="$t('物料名称')" prop="itemName">
              <el-input
                v-model="form.itemName"
                disabled
               
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleItemSelect"
                ></el-button>
              </el-input>
              <MdItem ref="MdItem" @onSelected="onItemSelected"></MdItem>
            </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-row>

        <el-row>
          <el-col :span="7">
            <el-form-item :label="$t('用途名称')" prop="usageId">
              <el-input
                v-model="form.usageName"
                disabled
               
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleUsageSelect()"
                ></el-button>
              </el-input>
              <UsageItem
                ref="woSelect"
                @onSelected="onUsageSelected"
              ></UsageItem>
            </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-col :span="8">
            <el-form-item :label="$t('基本计量单位')" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('有效期开始')" prop="expirationDateStart">
              <el-date-picker
                clearable
                v-model="form.expirationDateStart"
                type="date"
                value-format="yyyy-MM-dd"
               
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('有效期结束')" prop="expirationDateEnd">
              <el-date-picker
                clearable
                v-model="form.expirationDateEnd"
                type="date"
                value-format="yyyy-MM-dd"
               
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('ECN编码')" prop="ecnCode">
              <el-input v-model="form.ecnCode" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('单层厚')" prop="singleLayerThickness">
              <el-input
                v-model="form.singleLayerThickness"
               
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('单重')" prop="singleWeight">
              <el-input v-model="form.singleWeight" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('排版行数')" prop="rowNum">
              <el-input v-model="form.rowNum" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item :label="$t('派工倍数')" prop="dispatchMultiple">
              <el-input
                v-model="form.dispatchMultiple"
               
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('common.remark')" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
               
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="center" v-if="form.bomId != null"
        >{{$t('组件')}}</el-divider
      >
      <BomItem
        v-if="form.bomId != null"
        :bomId="form.bomId"
        :optType="optType"
      ></BomItem>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType == 'view'"
          >{{ $t('common.back') }}</el-button
        >
        <el-button type="primary" @click="submitForm" v-else>{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { listBom, getBom, delBom, addBom, updateBom } from "@/api/mes/pro/bom";
// import { listAllUsage } from "@/api/mes/pro/usage";
import BomItem from "./item.vue";
import MdItem from "@/components/itemSelect/single.vue";
import UsageItem from "@/components/usageSelect/single.vue";
import { genCode } from "@/api/system/autocode/rule";
export default {
  name: "Bom",
  dicts: ["sys_yes_no"],
  components: { BomItem, MdItem, UsageItem },
  data() {
    return {
      //自动生成编码
      autoGenFlag: false,
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产BOM表格数据
      bomList: [],
      // 用途列表
      UsageOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomCode: null,
        usageId: null,
        itemId: null,
        enableFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bomCode: [
          { required: true, message: this.$t('BOM编码不能为空'), trigger: "blur" },
        ],
        usageName: [
          { required: true, message: this.$t('用途不能为空'), trigger: "blur" },
        ],
        itemName: [
          { required: true, message: this.$t('产品id不能为空'), trigger: "blur" },
        ],
        enableFlag: [
          { required: true, message: this.$t('是否启用不能为空'), trigger: "blur" },
        ],
        usageId: [{ required: true, message: this.$t('用途不能为空'), trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    // this.getUsage();
  },
  methods: {
    /** 查询生产BOM列表 */
    getList() {
      this.loading = true;
      listBom(this.queryParams).then((response) => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询物料信息
    handleItemSelect() {
      this.$refs.MdItem.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;
      }
    },
    // 查询用途信息
    handleUsageSelect() {
      this.$refs.woSelect.showFlag = true;
    },
    onUsageSelected(row) {
      if (row != undefined && row != null) {
        this.form.usageId = row.usageId;
        this.form.usageCode = row.usageCode;
        this.form.usageName = row.usageName;
      }
    },
    // getUsage() {
    //   listAllUsage().then((response) => {
    //     this.UsageOptions = response.data;
    //   });
    // },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bomId: null,
        bomCode: null,
        itemId: null,
        itemName: null,
        usageName: null,
        usageId: null,
        quantity: null,
        unitOfMeasure: null,
        expirationDateStart: null,
        expirationDateEnd: null,
        ecnCode: null,
        singleLayerThickness: null,
        singleWeight: null,
        rowNum: null,
        dispatchMultiple: null,
        enableFlag: "Y",
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.autoGenFlag = false;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bomId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加BOM');
      this.optType = "add";
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const bomId = row.bomId || this.ids;
      getBom(bomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('查看BOM信息');
        this.optType = "view";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bomId = row.bomId || this.ids;
      getBom(bomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改BOM');
        this.optType = "edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bomId != null) {
            updateBom(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addBom(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bomIds = row.bomId || this.ids;
      this.$modal
        .confirm(this.$t('是否确认删除BOM？'))
        .then(function () {
          return delBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t('common.del_success'));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/bom/export",
        {
          ...this.queryParams,
        },
        `bom_${new Date().getTime()}.xlsx`
      );
    },
    //自动生成编码
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode("BOM_CODE").then((response) => {
          this.form.bomCode = response;
        });
      } else {
        this.form.bomCode = null;
      }
    },
  },
};
</script>
