<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入类型名称"
          clearable
          size="small"
          style="width: 140px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生效状态" prop="status">
          <el-select v-model="queryParams.status"  placeholder="请选择生效状态" size="small"  style="width: 160px">
            <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in statusList" :key="item" />
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adminList" >
      <el-table-column label="序号" align="left" prop="" show-overflow-tooltip >
        <template slot-scope="scope">
         {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="商品类型" align="left" prop="a" show-overflow-tooltip/>
      <el-table-column label="生效状态" align="left" prop="b" show-overflow-tooltip />
      <el-table-column label="备注" align="left" prop="c" show-overflow-tooltip />
      <el-table-column label="创建时间" align="left" prop="d"  show-overflow-tooltip />
      <el-table-column label="操作" align="left" 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:admin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog   :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false"  >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" >
        <el-row :gutter="0">
          <el-col :span="16">
            <el-form-item label="类型名称"  >
              <el-input v-model="form.name" placeholder="请输入类型名称" maxlength="15" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预约人"  >
              <el-select v-model="form.type"  placeholder="请选择生效状态" size="small"  style="width: 180px">
                <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in statusList" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="门禁控制器编码" >
              <el-select v-model="form.type"  placeholder="请选择门禁控制器编码" size="small"  style="width: 180px">
                <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in typeList" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
           <el-col :span="24">
            <el-form-item label="门禁通道编码"  >
              <el-select v-model="form.type"  placeholder="请选择门禁通道编码" size="small"  style="width: 180px">
                <el-option :label="index.label"  :value="index.value" v-for = "(index, item) in typeList" :key="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="introduce">
              <el-input type="textarea" :rows="4" placeholder="请输入备注" v-model="form.introduce" maxlength="50" show-word-limit>
              </el-input>
            </el-form-item>
          </el-col>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!loading">确 定</el-button>
          <el-button :loading="loading" type="primary" @click="submitForm" v-else>提交中...</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>

    </el-dialog>
  </div>
</template>

<script>
import { addType, updataType, delType, getType, exportType } from "@/api/recharge/consumePlaces";
export default {
  name: "admin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      dialogFormVisible:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图片显示
      fileList:[{"url":''}],
      // 参数表格数据
      adminList: [
        {a:1,b:2,c:3,d:4,f:5}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        aliasName: undefined,
      },
      statusList:[
        {value:"",label:"全部"},
        {value:"1",label:"有效"},
        {value:"2",label:"无效"},
      ],
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ],
        logo: [
          { required: true, message: "生效状态不能为空", trigger: "change" }
        ],
        logo: [
          { required: true, message: "门禁控制编码不能为空", trigger: "change" }
        ],
        logo: [
          { required: true, message: "门禁通道编码不能为空", trigger: "change" }
        ],
      },


    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getType(this.queryParams).then(response => {
        this.adminList = response.data.records
        this.loading = false;
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form.id =undefined
      this.form.name =undefined
      this.form.introduce =undefined
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.stringify(row)
      this.open = true;
      this.title = "修改参数";
    },

    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            updataType(this.form).then(response => {
              this.loading = false
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          } else {
            addType(this.form).then(response => {
              this.loading = false
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open = false
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delType(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.msgSuccess("刷新成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportType(queryParams).then(response =>{
        this.exportLoading = false;
        localStorage.setItem('fileExportId', response.data)
        this.$router.push({path:'/recharge/fileExport'})
      })
    },
  }
};
</script>
<style lang="scss" scoped>
</style>

