<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.focal"
            placeholder="焦距"
            clearable
          ></el-input>
          <el-input
            v-model="searchModel.status"
            placeholder="状态"
            clearable
          ></el-input>
          <el-button
            @click="getEquipList"
            type="primary"
            round
            icon="el-icon-search"
            >查询</el-button
          >
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            @click="openEditUI(null)"
            type="primary"
            icon="el-icon-plus"
            circle
          >
          </el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 结果列表 -->
    <el-card>
      <el-table :data="equipList" stripe style="width: 100%">
        <el-table-column prop="date" label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo - 1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="x2" label="设备编号" width="180">
        </el-table-column>
        <el-table-column prop="focal" label="焦距" width="180">
        </el-table-column>
        <el-table-column prop="status" label="状态" width="180">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1">可用</el-tag>
            <el-tag v-else type="danger">不可用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="x1" label="备注"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" size="mini" circle></el-button>
            <el-button @click="deleteEquip(scope.row)" type="danger" icon="el-icon-delete" size="mini" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 用户信息编辑对话框 -->
    <el-dialog
      @close="clearForm"
      :title="title"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="equipForm" ref="equipFormRef" :rules="rules">
        <el-form-item label="设备编号" prop="x2" :label-width="formLabelWidth">
          <el-input v-model="equipForm.x2" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="焦距" prop="focal" :label-width="formLabelWidth">
          <el-input v-model="equipForm.focal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="形状" prop="form" :label-width="formLabelWidth">
          <el-input v-model="equipForm.form" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="x1" :label-width="formLabelWidth">
          <el-input v-model="equipForm.x1" autocomplete="off"></el-input>
          <el-form-item label="设备状态" :label-width="formLabelWidth">
            <el-switch
              v-model="equipForm.status"
              :active-value="1"
              :inactive-value="0"
            >
            </el-switch>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEquip">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import equipApi from "@/api/equipManage";
export default {
  data() {
    return {
      formLabelWidth: "130px",
      equipForm: {},
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      equipList: [],
      rules: {
        x2: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "长度在 1 到 50 个字符",
            trigger: "blur",
          },
        ],

      },
    };
  },
  methods: {
    deleteEquip(equip){
      this.$confirm(`您确认删除用户 ${equip.x2} ?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          equipApi.deleteEquipById(equip.id).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            });
            this.getEquipList();
          });
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    saveEquip(){
      // 触发表单验证
      this.$refs.equipFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          equipApi.saveEquip(this.equipForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            });
            // 关闭对话框
            this.dialogFormVisible = false;
            // 刷新表格
            this.getEquipList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    clearForm(){
      this.equipForm = {};
      this.$refs.equipFormRef.clearValidate();
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getEquipList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getEquipList();
    },
    getEquipList() {
      equipApi.getEquipList(this.searchModel).then((response) => {
        this.equipList = response.data.rows;
        this.total = response.data.total;
      });
    },
    openEditUI(id) {
      if( id == null){
        this.title = "新增设备";
      }else{
        this.title = "修改设备";
        // 根据id查询设备数据
        equipApi.getEquipById(id).then(response => {
          this.equipForm = response.data;
        });
      }
      this.dialogFormVisible = true;
    },
  },
  created() {
    this.getEquipList();
  },
};
</script>

<style>
/* #search .el-input {
  width: 200px; 
  margin: 10px; 
} */
.el-dialog .el-input {
  width: 85%;
}
</style>