<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="用户名称" prop="gasUserName">
        <el-input
          v-model="queryParams.gasUserName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="gasUserCode">
        <el-input
          v-model="queryParams.gasUserCode"
          placeholder="请输入用户编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="gasUserType">
        <el-select v-model="queryParams.gasUserType" placeholder="请选择用户类型" clearable size="small">
          <el-option label="居民" value="0" />
          <el-option label="非居民" value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="入户安检状态" prop="gasUserCheckStatus">
        <el-select v-model="queryParams.gasUserCheckStatus" placeholder="请选择入户安检状态" clearable size="small">
          <el-option label="正常入户" value="0" />
          <el-option label="拒绝安检" value="1" />
        </el-select>
      </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"
        >新增燃气用户</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          style="color: #1890ff"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
          style="color: #1890ff"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="用户名称" align="center" prop="gasUserName" />
      <el-table-column label="用户编号" align="center" prop="gasUserCode" />
      <el-table-column label="用户类型" align="center" prop="gasUserType">
        <template slot-scope="scope">
          <span v-if="scope.row.gasUserType == '0'">居民</span>
          <span v-if="scope.row.gasUserType == '1'">非居民</span>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="gasUserStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.gasUserStatus == '0'">正常</span>
          <span v-if="scope.row.gasUserStatus == '1'">停用</span>
        </template>
      </el-table-column>
      <el-table-column label="用户地址" align="center" prop="gasUserAddress" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="入户安检状态" align="center" prop="gasUserCheckStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.gasUserCheckStatus == '0'">正常入户</span>
          <span v-if="scope.row.gasUserCheckStatus == '1'">拒绝安检</span>
        </template>
      </el-table-column>
      <el-table-column label="累计安检次数" align="center" prop="totalCheckNum" />
      <el-table-column label="末次配送时间" align="center" prop="lastDeliveryDate" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.lastDeliveryDate">{{ parseTime(scope.row.lastDeliveryDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="showDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户名称" prop="gasUserName">
              <el-input v-model="form.gasUserName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户编码" prop="gasUserCode">
              <el-input v-model="form.gasUserCode" placeholder="请输入代码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户类型" prop="gasUserType">
              <el-select v-model="form.gasUserType" placeholder="请选择用户类型">
                <el-option label="居民" value="0" />
                <el-option label="非居民" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="telNum">
              <el-input v-model="form.telNum" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户状态">
              <el-select v-model="form.gasUserStatus" placeholder="请选择用户状态">
                <el-option label="正常" value="0" />
                <el-option label="停用" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入户安检状态">
              <el-select v-model="form.gasUserCheckStatus" placeholder="请选择入户安检状态">
                <el-option label="正常入户" value="0" />
                <el-option label="拒绝安检" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="用户地址" prop="gasUserAddress">
          <el-input v-model="form.gasUserAddress" placeholder="请输入用户地址" />
        </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>

    <!-- 导入数据 -->
    <ImportData ref="importData"/>

    <!-- 详情 -->
    <DetailInfo ref="detailInfo"/>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/gasBottleTrack/gasUser.js";
import ImportData from "./ImportData";
import DetailInfo from "./DetailInfo";
export default {
  name: "Info",
  components: {
    ImportData,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 燃气用户表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gasUserName: null,
        gasUserCode: null,
        gasUserType: null,
        telNum: null,
        gasUserStatus: null,
        gasUserAddress: null,
        lastDeliveryDate: null,
        gasUserCheckStatus: null,
        totalCheckNum: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询燃气用户列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gasUserId: null,
        gasUserName: null,
        gasUserCode: null,
        gasUserType: null,
        telNum: null,
        gasUserStatus: "0",
        gasUserAddress: null,
        lastDeliveryDate: null,
        gasUserCheckStatus: "0",
        totalCheckNum: null,
        createTime: null,
        updateTime: null,
        isDel: 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.gasUserId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增燃气用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gasUserId = row.gasUserId || this.ids
      getInfo(gasUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改燃气用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gasUserId != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gasUserIds = row.gasUserId || this.ids;
      this.$confirm('是否确认删除燃气用户名为"' + row.gasUserName + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(gasUserIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有燃气用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    handleImport(){
      this.$refs.importData.upload.title = "燃气用户导入";
      this.$refs.importData.upload.open = true;
    },
    showDetail(row){
      const gasUserId = row.gasUserId || this.ids
      getInfo(gasUserId).then(response => {
        this.$refs.detailInfo.form = response.data;
        this.$refs.detailInfo.detailOpen = true;
      });
    },
  }
};
</script>
