<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :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="220px"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>
      <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" width="130px"/>
      <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="['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="title" :visible.sync="open" width="800px" append-to-body @cancel="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <div  v-if="title != '用户加装安全装置台账详情'">
          <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>
        </div>

        <div v-else>
          <el-row>
            <el-col :span="11">
              <el-form-item label="用户名称:">
                <font>{{form.userName}}</font>
              </el-form-item>
              <el-form-item label="身份证号:">
                <font>{{form.idCard}}</font>
              </el-form-item>
              <el-form-item label="品牌名称:">
                <font v-if="form.brandName != '' && form.brandName != null">{{form.brandName}}</font>
                <font v-else> - </font>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="用户编号:">
                <font v-if="form.userNo != '' && form.userNo != null">{{form.userNo}}</font>
                <font v-else> - </font>
              </el-form-item>
              <el-form-item label="联系电话:">
                <font>{{form.linkMobile}}</font>
              </el-form-item>
              <el-form-item label="安装时间:">
                <font>{{form.installTime}}</font>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="详细地址:">
              <font>{{form.userAddress}}</font>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="备注信息:">
              <font v-if="form.remarks != '' && form.remarks != null">{{form.remarks}}</font>
              <font v-else> - </font>
            </el-form-item>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="title != '用户加装安全装置台账详情'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </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: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        idCard: null,
        linkMobile: null,
        installTimeStart: null,
        installTimeEnd: null
      },
      // 表单参数
      form: {},
      // 表单校验
      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;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      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");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户加装安全装置台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getEquipment(row.safeEquipmentId).then(response => {
        console.log("data",response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改用户加装安全装置台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.safeEquipmentId != null) {
            updateEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = 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.reset();
      getEquipment(row.safeEquipmentId).then(response => {
        console.log("data",response.data);
        this.form = response.data;
        this.open = true;
        this.title = "用户加装安全装置台账详情";
      });
    },
  }
};
</script>
