<!--工厂信息-->
<template>
  <div class="app-container">
    <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="['md:factory:add']"
          >新增</el-button
        >
        <FactorySelect ref="FactorySelectRef" @onSelected="onFactorySelect" />
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['md:factory: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="['md:factory:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="factoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="产品工厂id" align="center" prop="productFactoryId" />
      <el-table-column label="产品id" align="center" prop="itemId" />-->
<!--      <el-table-column
        label="工厂id"
        align="center"
        prop="factoryId"
        width="100"
      />-->
<!--      <el-table-column
        label="工厂编码"
        align="center"
        prop="factoryCode"
        width="100"
      />-->
      <el-table-column
        label="工厂名称"
        align="center"
        prop="factoryName"
        width="120"
      />
<!--      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="修改人"
        align="center"
        prop="updateBy"
        width="120"
      />
      <el-table-column
        label="修改时间"
        align="center"
        prop="updateTime"
        width="180"
      />-->
      <el-table-column label="操作" align="center" width="90">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['md:factory:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

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

<script>
import {
  listFactory,
  getFactory,
  delFactory,
  addFactory,
  updateFactory,
} from "@/api/mes/md/productFactory";
import FactorySelect from "@/components/FactorySelect/index.vue";

export default {
  name: "Factory",
  components: { FactorySelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品工厂配置表格数据
      factoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemId: this.itemId,
        factoryId: null,
        factoryCode: null,
        factoryName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      },
      // 表单参数
      form: {
        factoryName: "",
        factoryId: "",
        factoryCode: "",
      },
      // 表单校验
      rules: {},
    };
  },
  props: {
    optType: undefined,
    itemId: {
      type: Number | undefined,
      default: undefined,
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品工厂配置列表 */
    getList() {
      this.loading = true;
      listFactory(this.queryParams).then((response) => {
        this.factoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productFactoryId: null,
        itemId: null,
        factoryId: null,
        factoryCode: null,
        factoryName: 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.productFactoryId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs["FactorySelectRef"].showFlag = true;
    },

    onFactorySelect(row) {
      if (row != null && row != undefined) {
        this.form.factoryId = row.factoryId;
        this.form.factoryName = row.factoryName;
        this.form.itemId = this.itemId
        const idx = this.factoryList.findIndex(itm => itm.factoryId == row.factoryId)
        if (idx > -1) {
          this.$modal.msgError("新增工厂失败，工厂已存在");
          return
        }
        addFactory(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productFactoryIds = row.productFactoryId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除产品工厂配置编号为"' + productFactoryIds + '"的数据项？'
        )
        .then(function () {
          return delFactory(productFactoryIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/factory/export",
        {
          ...this.queryParams,
        },
        `factory_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
