<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库房名称" prop="kfName">
        <el-input
          v-model="queryParams.kfName"
          placeholder="请输入库房名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库房位置" prop="kfAddress">
        <el-input
          v-model="queryParams.kfAddress"
          placeholder="请输入库房位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库房管理员" prop="kfAdmin">
        <el-select v-model="queryParams.kfAdmin" placeholder="请选择库房管理员">
          <el-option
            v-for="item in userSelect"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </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="['system:kufang:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="kufangList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="库房名称" align="center" prop="kfName" />
      <el-table-column label="库房位置" align="center" prop="kfAddress" />
      <el-table-column label="库房管理员" align="center" prop="kfAdmin" >
        <template slot-scope="scope">
            <p>{{ userTableName[scope.row.kfAdmin] }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleQrCode(scope.row)"
          >二维码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:kufang:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:kufang: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"
    />

    <!-- 添加或修改库房对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库房名称" prop="kfName">
          <el-input v-model="form.kfName" placeholder="请输入库房名称" />
        </el-form-item>
        <el-form-item label="库房位置" prop="kfAddress">
          <el-input v-model="form.kfAddress" placeholder="请输入库房位置" />
        </el-form-item>
        <el-form-item label="库房管理员" prop="kfAdmin">
          <el-select v-model="form.kfAdmin" placeholder="请选择库房管理员" style="width: 100%">
            <el-option
              v-for="item in userSelect"
              :label="item.userName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="二维码" :visible.sync="codeOpen" width="500px" append-to-body>
      <div id="kf_qrcode" ref="kf_qrcode" style="display: flex;justify-content: center;"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="codeCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listKufang, getKufang, delKufang, addKufang, updateKufang, getUserByRole} from "@/api/system/kufang";
import QRCode from "qrcodejs2";

export default {
  name: "Kufang",
  data() {
    return {
      codeOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库房表格数据
      kufangList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        kfName: null,
        kfAddress: null,
        kfAdmin: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        kfName: [
          { required: true, message: "库房名称不能为空", trigger: "blur" }
        ],
        kfAddress: [
          { required: true, message: "库房位置不能为空", trigger: "blur" }
        ],
        kfAdmin: [
          { required: true, message: "请选择库房管理员", trigger: "blur" }
        ]
      },
      userSelect: [],
      userTableName: {}
    };
  },
  created() {

    getUserByRole().then(json => {
      this.userSelect = json.data || []
      if (json.data && json.data.length) {
        for (let i = 0; i < json.data.length; i++) {
          this.userTableName[json.data[i].userId] = json.data[i].userName
        }
      }
      this.getList();
    })
  },
  methods: {
    /** 查询库房列表 */
    getList() {
      this.loading = true;
      listKufang(this.queryParams).then(response => {
        this.kufangList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        kfName: null,
        kfAddress: null,
        kfAdmin: null,
        createTime: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库房";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKufang(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库房";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKufang(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKufang(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库房编号为"' + ids + '"的数据项？').then(function() {
        return delKufang(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleQrCode(row) {
      this.codeOpen = true
      setTimeout(function () {
        if (document.getElementById('kf_qrcode')) {
          document.getElementById('kf_qrcode').innerHTML = ''; //防止重复生成二维码
          // this.$nextTick(function () {
          new QRCode(document.getElementById('kf_qrcode'), {
            width: 256,
            height: 256,
            text: process.env.VUE_APP_BASE_API + '/page/goods/goods.html?id=' + row.id + '&name=' + row.kfName,
            colorDark: '#333333', // 二维码颜色
            colorLight: '#ffffff', // 二维码背景色
            correctLevel: QRCode.CorrectLevel.L,
          })
          // })
        }
      }, 100)



    },
    codeCancel() {
      this.codeOpen = false;
    },
  }
};
</script>
