<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="关键词">
        <el-input placeholder="请输入关键词" v-model="queryParams.keyword" />
      </el-form-item>
      <el-form-item label="账户分类" prop="accountClassId">
        <el-input v-model="queryParams.accountClassId" 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="userCompanyId">
        <el-input v-model="queryParams.userCompanyId" placeholder="请输入用户单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="提交时间" prop="dateRange">
        <el-date-picker clearable v-model="queryParams.dateRange" type="daterange" range-separator="至"
          start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
        </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="['cs:csOpenRecord: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="['cs:csOpenRecord: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="['cs:csOpenRecord:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cs:csOpenRecord:export']">导出</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-download" size="mini" @click="handleExportTemplate"
          v-hasPermi="['cs:csOpenRecord:export']">导出模板</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="csOpenRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort">
        <template slot-scope="scope">
          {{ ((queryParams.pageNum - 1) * queryParams.pageSize) + (scope.$index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="账户分类" align="center" prop="accountClass" />
      <el-table-column label="工单账号" align="center" prop="orderAccount" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="用户单位" align="center" prop="userCompany" />
      <el-table-column label="单位类型" align="center" prop="companyClass" />
      <el-table-column label="手机号" align="center" prop="concatPhone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="开通账号日期" align="center" prop="openTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号到期日期" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请资源" align="center" prop="applyResource" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <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="['cs:csOpenRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csOpenRecord: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="账户分类" prop="accountClassId">
              <el-input v-model="form.accountClassId" placeholder="请输入账户分类" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单账号" prop="orderAccount">
              <el-input v-model="form.orderAccount" placeholder="请输入工单账号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="用户姓名" prop="memberUserId">
             <el-select v-model="form.memberUserId" clearable placeholder="请选择用户姓名" class="member-select">
               <el-option v-for="item in memberUserIdList" :key="item.memberUserId" :label="item.realName"
                 :value="item.memberUserId">
               </el-option>
             </el-select>
           </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户单位" prop="userCompanyId">
              <el-select v-model="form.userCompanyId" clearable placeholder="请选择用户单位"  @change="changeUserCompanyId">
                <el-option v-for="item in userCompanyIdList" :key="item.companyId" :label="item.companyName"
                  :value="`${item.companyId}-${item.companyName}`">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位类型" prop="companyClassId">
              <el-input v-model="form.companyClassId" placeholder="请输入单位类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="concatPhone">
              <el-input v-model="form.concatPhone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开通账号日期" prop="openTime">
              <el-date-picker clearable v-model="form.openTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择开通账号日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账号到期日期" prop="expireTime">
              <el-date-picker clearable v-model="form.expireTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择账号到期日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="申请资源" prop="applyResource">
              <el-input v-model="form.applyResource" placeholder="请输入申请资源" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input type="textarea" :rows="2" v-model="form.remarks" placeholder="请输入备注" maxlength="100" />
            </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="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCsOpenRecord, getCsOpenRecord, delCsOpenRecord, addCsOpenRecord, updateCsOpenRecord } from "@/api/cs/csOpenRecord";
  import {
    searchMemberList,
    searchCompanyList
  } from "@/api/cs/csExpansionRecord";
export default {
  name: "CsOpenRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 开户记录表格数据
      csOpenRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        dateRange: [],
        accountClassId: null,
        accountClass: null,
        userName: null,
        userCompanyId: null,
        userCompany: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      memberUserIdList: [],
      userCompanyIdList: []
    };
  },
  created() {
    this.getList();
    this.searchMemberList()
    this.searchCompanyList()
  },
  methods: {
    /** 查询开户记录列表 */
    getList() {
      this.loading = true;
      if (null != this.queryParams.dateRange && '' != this.queryParams.dateRange) {
        this.queryParams["beginCreateAt"] = this.queryParams.dateRange[0];
        this.queryParams["endCreateAt"] = this.queryParams.dateRange[1];
      }
      listCsOpenRecord(this.queryParams).then(response => {
        this.csOpenRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    searchMemberList() {
      searchMemberList().then(res => {
        if (res.code == 200) {
          this.memberUserIdList = res.data
        }
      })
    },
    searchCompanyList() {
      searchCompanyList().then(res => {
        if (res.code == 200) {
          this.userCompanyIdList= res.data
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        openRecordId: null,
        resId: null,
        resName: null,
        resType: null,
        resourceClassId: null,
        resourceClass: null,
        memberUserId: null,
        accountClassId: null,
        accountClass: null,
        orderAccount: null,
        userName: null,
        userCompanyId: null,
        userCompany: null,
        companyClassId: null,
        companyClass: null,
        concatPhone: null,
        email: null,
        openTime: null,
        expireTime: null,
        applyResource: null,
        remarks: null,
        content: null,
        status: "0",
        sort: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        dateRange: [],
        accountClassId: null,
        accountClass: null,
        userName: null,
        userCompanyId: null,
        userCompany: null,
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.openRecordId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    changeUserCompanyId(val){
      if(val){
        this.form.userCompany=val.split('-')[1]
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加开户记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const openRecordId = row.openRecordId || this.ids
      getCsOpenRecord(openRecordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改开户记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.openRecordId != null) {
            updateCsOpenRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCsOpenRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const openRecordIds = row.openRecordId || this.ids;
      this.$modal.confirm('是否确认删除开户记录编号为"' + openRecordIds + '"的数据项？').then(function () {
        return delCsOpenRecord(openRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cs/csOpenRecord/export', {
        ...this.queryParams
      }, `开户记录模板_${new Date().getTime()}.xlsx`)
    },
    handleExportTemplate() {
      this.download('cs/csOpenRecord/exportTemplate', {
        ...this.queryParams
      }, `开户记录模板_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
