<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构性质" prop="storeNature">
        <el-input
          v-model="queryParams.storeNature"
          placeholder="请输入机构性质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录账号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入登录账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="leaderTel">
        <el-input
          v-model="queryParams.leaderTel"
          placeholder="请输入负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="begintime">
        <el-date-picker clearable
          v-model="queryParams.begintime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endtime">
        <el-date-picker clearable
          v-model="queryParams.endtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="固定电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入固定电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属区域" prop="isRegion">
        <el-input
          v-model="queryParams.isRegion"
          placeholder="请输入所属区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务区域" prop="serviceRegion">
        <el-input
          v-model="queryParams.serviceRegion"
          placeholder="请输入服务区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算周期" prop="settleTime">
        <el-input
          v-model="queryParams.settleTime"
          placeholder="请输入结算周期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家在线" prop="storeOnline">
        <el-input
          v-model="queryParams.storeOnline"
          placeholder="请输入商家在线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否自营" prop="isOne">
        <el-input
          v-model="queryParams.isOne"
          placeholder="请输入是否自营"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照" prop="businessurl">
        <el-input
          v-model="queryParams.businessurl"
          placeholder="请输入营业执照"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家图片" prop="storeurl">
        <el-input
          v-model="queryParams.storeurl"
          placeholder="请输入商家图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同图片" prop="contracturl">
        <el-input
          v-model="queryParams.contracturl"
          placeholder="请输入合同图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社会统一信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入社会统一信用代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布范围" prop="releaseScope">
        <el-input
          v-model="queryParams.releaseScope"
          placeholder="请输入发布范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建部门" prop="createDepartment">
        <el-input
          v-model="queryParams.createDepartment"
          placeholder="请输入创建部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          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="['system:manage:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:manage:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:manage:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:user:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:manage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="机构名称" align="center" prop="storeName" />
      <el-table-column label="机构性质" align="center" prop="storeNature" />
      <el-table-column label="登录账号" align="center" prop="username" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="负责人电话" align="center" prop="leaderTel" />
      <el-table-column label="服务状态" align="center" prop="serviceStatus" />
      <el-table-column label="开始时间" align="center" prop="begintime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.begintime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="周末服务" align="center" prop="weekendStatus" />
      <el-table-column label="假日服务" align="center" prop="holidayStatus" />
      <el-table-column label="固定电话" align="center" prop="phone" />
      <el-table-column label="所属区域" align="center" prop="isRegion" />
      <el-table-column label="服务区域" align="center" prop="serviceRegion" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="结算周期" align="center" prop="settleTime" />
      <el-table-column label="商家在线" align="center" prop="storeOnline" />
      <el-table-column label="是否自营" align="center" prop="isOne" />
      <el-table-column label="营业执照" align="center" prop="businessurl" />
      <el-table-column label="商家图片" align="center" prop="storeurl" />
      <el-table-column label="合同图片" align="center" prop="contracturl" />
      <el-table-column label="社会统一信用代码" align="center" prop="creditCode" />
      <el-table-column label="发布范围" align="center" prop="releaseScope" />
      <el-table-column label="创建部门" align="center" prop="createDepartment" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="机构介绍" align="center" prop="introduction" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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="['system:manage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:manage: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="机构性质" prop="storeNature">
          <el-input v-model="form.storeNature" placeholder="请输入机构性质" />
        </el-form-item>
        <el-form-item label="登录账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入登录账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="负责人" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="leaderTel">
          <el-input v-model="form.leaderTel" placeholder="请输入负责人电话" />
        </el-form-item>
        <el-form-item label="开始时间" prop="begintime">
          <el-date-picker clearable
            v-model="form.begintime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endtime">
          <el-date-picker clearable
            v-model="form.endtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="固定电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入固定电话" />
        </el-form-item>
        <el-form-item label="所属区域" prop="isRegion">
          <el-input v-model="form.isRegion" placeholder="请输入所属区域" />
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceRegion">
          <el-input v-model="form.serviceRegion" placeholder="请输入服务区域" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="结算周期" prop="settleTime">
          <el-input v-model="form.settleTime" placeholder="请输入结算周期" />
        </el-form-item>
        <el-form-item label="商家在线" prop="storeOnline">
          <el-input v-model="form.storeOnline" placeholder="请输入商家在线" />
        </el-form-item>
        <el-form-item label="是否自营" prop="isOne">
          <el-input v-model="form.isOne" placeholder="请输入是否自营" />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessurl">
          <el-input v-model="form.businessurl" placeholder="请输入营业执照" />
        </el-form-item>
        <el-form-item label="商家图片" prop="storeurl">
          <el-input v-model="form.storeurl" placeholder="请输入商家图片" />
        </el-form-item>
        <el-form-item label="合同图片" prop="contracturl">
          <el-input v-model="form.contracturl" placeholder="请输入合同图片" />
        </el-form-item>
        <el-form-item label="社会统一信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入社会统一信用代码" />
        </el-form-item>
        <el-form-item label="发布范围" prop="releaseScope">
          <el-input v-model="form.releaseScope" placeholder="请输入发布范围" />
        </el-form-item>
        <el-form-item label="创建部门" prop="createDepartment">
          <el-input v-model="form.createDepartment" 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>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManage, getManage, delManage, addManage, updateManage } from "@/api/system/manage";
import {getToken} from "@/utils/auth";

export default {
  name: "Manage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务商管理表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/manage/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        storeNature: null,
        username: null,
        password: null,
        leader: null,
        leaderTel: null,
        serviceStatus: null,
        begintime: null,
        endtime: null,
        weekendStatus: null,
        holidayStatus: null,
        phone: null,
        isRegion: null,
        serviceRegion: null,
        address: null,
        settleTime: null,
        storeOnline: null,
        isOne: null,
        businessurl: null,
        storeurl: null,
        contracturl: null,
        creditCode: null,
        releaseScope: null,
        createDepartment: null,
        status: null,
        introduction: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务商管理列表 */
    getList() {
      this.loading = true;
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        storeNature: null,
        username: null,
        password: null,
        leader: null,
        leaderTel: null,
        serviceStatus: null,
        begintime: null,
        endtime: null,
        weekendStatus: null,
        holidayStatus: null,
        phone: null,
        isRegion: null,
        serviceRegion: null,
        address: null,
        settleTime: null,
        storeOnline: null,
        isOne: null,
        businessurl: null,
        storeurl: null,
        contracturl: null,
        creditCode: null,
        releaseScope: null,
        createDepartment: null,
        status: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务商管理";
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/manage/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务商管理编号为"' + ids + '"的数据项？').then(function() {
        return delManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/manage/export', {
        ...this.queryParams
      }, `manage_${new Date().getTime()}.xlsx`)
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    }
  }
};
</script>
