<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="联系电话" prop="linkMobile">
        <el-input
          v-model="queryParams.linkMobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装时间" prop="installTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.installTimeStart"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择起始时间">
        </el-date-picker><span style="color: #bebfc3"> - </span>
        <el-date-picker clearable size="small"
                        v-model="queryParams.installTimeEnd"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择截止时间">
        </el-date-picker>
      </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="['standingBook:equipment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['standingBook:equipment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="equipmentList" >
      <el-table-column label="用户名称" align="center" prop="userName" width="200px"/>
      <el-table-column label="身份证号" align="center" prop="idCard" width="180px"/>
      <el-table-column label="联系电话" align="center" prop="linkMobile"/>
      <el-table-column label="详细地址" align="center" prop="userAddress" width="380px"/>
      <el-table-column label="安装时间" align="center" prop="installTime"/>
      <el-table-column label="品牌名称" align="center" prop="brandName"/>
      <el-table-column label="操作" align="center" width="180px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['standingBook:equipment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDetail(scope.row)"
            v-hasPermi="['standingBook:equipment:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['standingBook:equipment: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="title1" :visible.sync="open1" width="800px" append-to-body @cancel="cancel1">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号" prop="userNo">
              <el-input v-model="form.userNo" placeholder="请输入用户编号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkMobile">
              <el-input v-model="form.linkMobile" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="详细地址" prop="userAddress">
              <el-input type="textarea" v-model="form.userAddress" placeholder="请输入详细地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="品牌名称" prop="brandName">
              <el-input v-model="form.brandName" placeholder="请输入品牌名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安装时间" prop="installTime">
              <el-date-picker clearable size="small"
                              v-model="form.installTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择安装时间"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注信息" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注信息" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="open2" width="800px" append-to-body @cancel="cancel2">
      <el-form ref="detailForm" :model="detailForm" label-width="120px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户名称:">
              <font>{{detailForm.userName}}</font>
            </el-form-item>
            <el-form-item label="身份证号:">
              <font>{{detailForm.idCard}}</font>
            </el-form-item>
            <el-form-item label="品牌名称:">
              <font v-if="detailForm.brandName != '' && detailForm.brandName != null">{{detailForm.brandName}}</font>
              <font v-else> - </font>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户编号:">
              <font v-if="detailForm.userNo != '' && detailForm.userNo != null">{{detailForm.userNo}}</font>
              <font v-else> - </font>
            </el-form-item>
            <el-form-item label="联系电话:">
              <font>{{detailForm.linkMobile}}</font>
            </el-form-item>
            <el-form-item label="安装时间:">
              <font>{{detailForm.installTime}}</font>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="详细地址:">
            <font>{{detailForm.userAddress}}</font>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="备注信息:">
            <font v-if="detailForm.remarks != '' && detailForm.remarks != null">{{detailForm.remarks}}</font>
            <font v-else> - </font>
          </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listEquipment, getEquipment, delEquipment, addEquipment, updateEquipment, exportEquipment } from "@/api/standingBook/equipment";

export default {
  name: "Equipment",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户加装安全装置台账表格数据
      equipmentList: [],
      // 弹出层标题
      title1: "",
      title2: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        linkMobile: null,
        installTimeStart: null,
        installTimeEnd: null
      },
      // 表单参数
      form: {},
      detailForm: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        userAddress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
        linkMobile: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度11位", trigger: "blur" },
        ],
        idCard: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
          { min: 18, max: 18, message: "长度18位", trigger: "blur" },
        ],
        installTime: [
          { required: true, message: "选择安装时间", trigger: "change" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户加装安全装置台账列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then(response => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    cancel2() {
      this.open2 = false;
      this.reset2();
    },
    // 表单重置
    reset1() {
      this.form = {
        safeEquipmentId: null,
        userName: null,
        userNo: null,
        userAddress: null,
        idCard: null,
        linkMobile: null,
        installTime: null,
        brandName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
    },
    reset2() {
      this.detailForm = {
        safeEquipmentId: null,
        userName: null,
        userNo: null,
        userAddress: null,
        idCard: null,
        linkMobile: null,
        installTime: null,
        brandName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("detailForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        linkMobile: null,
        installTimeStart: null,
        installTimeEnd: null
      },
      this.resetForm("queryParams");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset1();
      this.open1 = true;
      this.title1 = "添加用户加装安全装置台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset1();
      getEquipment(row.safeEquipmentId).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title1 = "修改用户加装安全装置台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.safeEquipmentId != null) {
            updateEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const safeEquipmentIds = row.safeEquipmentId || this.ids;
      row.isDel = "1";
      this.$confirm('是否确认删除"' + row.userName + '"的台账?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateEquipment(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户加装安全装置台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportEquipment(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细信息跳转 */
    showDetail(row) {
      this.reset2();
      getEquipment(row.safeEquipmentId).then(response => {
        this.detailForm = response.data;
        this.open2 = true;
        this.title2 = "用户加装安全装置台账详情";
      });
    },
  }
};
</script>
