<template>
  <div class="app-container iot-vendor">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="厂商名称" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="厂商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商地址" prop="vendorAddress">
        <el-input
          v-model="queryParams.vendorAddress"
          placeholder="厂商地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="vendorContact">
        <el-input
          v-model="queryParams.vendorContact"
          placeholder="联系方式"
          clearable
          size="small"
          @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="['iot:vendor:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="vendorList"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'createTime', order: 'descending' }"
      @sort-change="sortChange"
    >
      <el-table-column
        type="index"
        label="序号"
        align="center"
        :index="indexFormatter"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="厂商名称"
        align="left"
        width="250"
        prop="vendorName"
      />
      <el-table-column
        label="联系方式"
        align="left"
        width="250"
        prop="vendorContact"
      />
      <el-table-column label="厂商地址" align="left" prop="vendorAddress">
        <template slot-scope="scope">
          <span class="lay-table-textarea" :title="scope.row.vendorAddress">
            {{ scope.row.vendorAddress }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="left" prop="remark">
        <template slot-scope="scope">
          <span class="lay-table-textarea" :title="scope.row.remark">
            {{ scope.row.remark }}
          </span>
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        sortable="custom"
        width="160"
        prop="createTime"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['iot:vendor:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iot:vendor: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>
      <dialog-template :title="title" :visible="open" @close="open = false">
        <el-form
          ref="form"
          slot="dialog-center"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="厂商名称：" prop="vendorName">
            <el-input v-model="form.vendorName" placeholder="请输入厂商名称" />
          </el-form-item>
          <el-form-item label="联系方式：" prop="vendorContact">
            <el-input
              v-model="form.vendorContact"
              placeholder="请输入厂商联系方式"
            />
          </el-form-item>
          <el-form-item label="厂商编码：" prop="vendorCode">
            <el-input
              v-model="form.vendorCode"
              :disabled="form.vendorId !== null && form.vendorId !== undefault"
              placeholder="请输入厂商编码"
            />
          </el-form-item>
          <el-form-item label="厂商地址：" prop="vendorAddress">
            <el-input
              maxlength="120"
              v-model="form.vendorAddress"
              placeholder="请输入厂商地址"
            />
          </el-form-item>

          <el-form-item label="备注：" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              :rows="4"
              maxlength="200"
              placeholder="请输入备注"
            />
          </el-form-item>
        </el-form>
        <div slot="dialog-footer">
          <el-button type="primary" size="mini" @click="submitForm"
            >确 定</el-button
          >
          <el-button size="mini" @click="cancel">取 消</el-button>
        </div>
      </dialog-template>
    </div>
  </div>
</template>

<script>
import {
  listVendor,
  getVendor,
  delVendor,
  addVendor,
  updateVendor,
  exportVendor,
} from "@/api/iot/vendor";
import DialogTemplate from "@/components/DialogTemplate";

export default {
  name: "Vendor",
  components: {
    DialogTemplate,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 厂商表格数据
      vendorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vendorName: null,
        vendorAddress: null,
        vendorContact: null,
        orderByColumn: "createTime",
        isAsc: "desc",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vendorName: [
          { required: true, message: "厂商名称不能为空", trigger: "blur" },
        ],
        vendorContact: [
          { required: true, message: "联系方式不能为空", trigger: "blur" },
        ],
        vendorCode: [
          { required: true, message: "厂商编码不能为空", trigger: "blur" },
          {
            pattern: /^[0-9]{3}$/,
            message: "格式不正确：[0-9] 三个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop,
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    /** 查询厂商列表 */
    getList() {
      this.loading = true;
      listVendor(this.queryParams).then((response) => {
        this.vendorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vendorId: null,
        vendorName: null,
        vendorAddress: null,
        vendorContact: null,
        vendorCode: "",
        delFlag: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        remark: 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.vendorId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加厂商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vendorId = row.vendorId || this.ids;
      getVendor(vendorId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改厂商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.vendorId != null) {
            updateVendor(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVendor(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vendorIds = row.vendorId || this.ids;
      this.$confirm("是否删除该选项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delVendor(vendorIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有厂商数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportVendor(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss">
.iot-vendor {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
  }
}
</style>
