<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="BOM编码" prop="bomCode">
        <el-input
          v-model="queryParams.bomCode"
          placeholder="请输入BOM编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用途名称" prop="usageName">
        <el-input
          v-model="queryParams.usageName"
          placeholder="请输入用途名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="enableFlag">
        <el-input
          v-model="queryParams.enableFlag"
          placeholder="请输入是否启用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</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']"
          >新增</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']"
          >修改</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']"
          >删除</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']"
          >导出</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="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="产品名称" fixed="left" width="120" align="center" prop="itemName" />
      <el-table-column label="生产用途" align="center" prop="usageName" />
      <el-table-column label="基本数量" align="center" prop="quantity" />
      <el-table-column
        label="基本计量单位"
        width="125"
        align="center"
        prop="unitOfMeasure"
      />
      <el-table-column label="是否启用" 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="有效期开始"
        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="有效期结束"
        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="ECN编码"  width="120" align="center" prop="ecnCode" />
      <el-table-column
        label="单层厚"
        width="120"
        align="center"
        prop="singleLayerThickness"
      />
      <el-table-column label="单重"  width="120" align="center" prop="singleWeight" />
      <el-table-column label="排版行数"  width="120" align="center" prop="rowNum" />
      <el-table-column
        label="派工倍数"
        width="120"
        align="center"
        prop="dispatchMultiple"
      />
      <el-table-column label="是否启用"  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="备注"
        width="120"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        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']"
            >修改</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']"
            >删除</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="BOM编码" prop="bomCode">
              <el-input v-model="form.bomCode" placeholder="请输入BOM编码" />
            </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="自动生成"
                @change="handleAutoGenChange(autoGenFlag)"
                v-if="optType != 'view'"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="物料名称" prop="itemName">
              <el-input
                v-model="form.itemName"
                disabled
                placeholder="请选择物料信息"
              >
                <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="是否启用" 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="用途名称" prop="usageId">
              <el-input
                v-model="form.usageName"
                disabled
                placeholder="请选择用途"
              >
                <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="基本数量" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入基本数量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="基本计量单位" 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="有效期开始" prop="expirationDateStart">
              <el-date-picker
                clearable
                v-model="form.expirationDateStart"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择有效期开始"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="有效期结束" prop="expirationDateEnd">
              <el-date-picker
                clearable
                v-model="form.expirationDateEnd"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择有效期结束"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="ECN编码" prop="ecnCode">
              <el-input v-model="form.ecnCode" placeholder="请输入ECN编码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="单层厚" prop="singleLayerThickness">
              <el-input
                v-model="form.singleLayerThickness"
                placeholder="请输入单层厚"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单重" prop="singleWeight">
              <el-input v-model="form.singleWeight" placeholder="请输入单重" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排版行数" prop="rowNum">
              <el-input v-model="form.rowNum" placeholder="请输入排版行数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="派工倍数" prop="dispatchMultiple">
              <el-input
                v-model="form.dispatchMultiple"
                placeholder="请输入派工倍数"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="center" v-if="form.bomId != null"
        >组件</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'"
          >返回</el-button
        >
        <el-button type="primary" @click="submitForm" v-else>确 定</el-button>
        <el-button @click="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: "BOM编码不能为空", trigger: "blur" },
        ],
        usageName: [
          { required: true, message: "用途不能为空", trigger: "blur" },
        ],
        itemName: [
          { required: true, message: "产品id不能为空", trigger: "blur" },
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "blur" },
        ],
        usageId: [{ required: true, message: "用途不能为空", 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 = "添加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 = "查看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 = "修改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.open = false;
              this.getList();
            });
          } else {
            addBom(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bomIds = row.bomId || this.ids;
      this.$modal
        .confirm("是否确认删除BOM？")
        .then(function () {
          return delBom(bomIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .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>
