<template>
  <section class="app-container">
    <el-row>
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable size="small" style="width: 240px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" 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 size="mini" type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:project:add']">增加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['monitor:project:remove']">批量删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center">
          </el-table-column>
          <el-table-column prop="projectName" align="center" label="项目名称">
          </el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间">
          </el-table-column>
          <!-- <el-table-column prop="status" align="center" label="状态">
          </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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:project:edit']">
                修改
              </el-button>
              <el-button class="text-danger" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:project: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-col>
      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" placeholder="请输入项目名称" />
          </el-form-item>
          <el-form-item label="项目SN" prop="projectId">
            <el-input :disabled="!!form.id" v-model="form.projectId" placeholder="请输入项目SN" />
          </el-form-item>
          <el-form-item label="客户" prop="userId">
            <el-select v-model="form.userId" clearable placeholder="请选择">
              <el-option v-for="item in userSelectList" :key="item.id" :label="item.nickName" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="状态">
            <el-input v-model="form.status" />
          </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-row>
  </section>
</template>

<script>
import { list, Info, add, update, del, select } from "@/api/hardware/project";
import { userSelect } from "@/api/system/user";
export default {
  name: "ProjectList",
  data () {
    return {
      userSelectList: [],
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表格数据
      tableData: null,
      // 表单参数
      form: {},
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        projectId: [
          { required: true, message: "项目SN不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "客户不能为空", trigger: "blur" },
        ],
      },
    }
  },
  created () {
    userSelect().then(({ code, msg, select }) => {
      if (code == 200) {
        this.userSelectList = select;
      } else {
        this.$message.error(msg);
      }
    })
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },

    // 表单重置
    reset () {
      this.form = {
        projectName: "",
        createTime: "",
        status: "",
      };
      this.resetForm("form");
    },

    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },

    // 获取项目列表
    getList () {
      list(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
      });
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },

    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = Object.assign({}, row);
      this.open = true;
      this.title = "修改项目";
    },

    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return del(ids);
        })
        .catch(() => { })
        .then((res) => {
          if (200 == res.code) {
            this.getList();
            this.msgSuccess("删除成功");
          } else {
            this.msgSuccess(res.msg);
          }
        })
        .catch((res) => { });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // debugger
          if (this.form.id != undefined) {
            update(this.form).then((res) => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }
            });
          } else {
            add(this.form).then((res) => {
              if (200 == res.code) {
                this.msgSuccess(this.title + "成功");
                this.open = false;
                this.getList();
              } else {
                this.msgSuccess(res.msg);
              }
            });
          }
        }
      });
    },
  }
}
</script>

<style>
</style>