<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="transferTime">
        <el-date-picker clearable v-model="queryParams.transferTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择时间">
        </el-date-picker>
      </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="AC账号" prop="userAccount">
        <el-input v-model="queryParams.userAccount" placeholder="请输入AC账号" clearable />
      </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:csTransferRecord: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:csTransferRecord: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:csTransferRecord: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:csTransferRecord: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:csTransferRecord:export']">导出模板</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="csTransferRecordList" @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="userName" />
      <el-table-column label="工单账号" align="center" prop="orderAccount" />
      <el-table-column label="用户单位" align="center" prop="userCompany" />
       <el-table-column label="AC账号" align="center" prop="userAccount" />
      <el-table-column label="时间" align="center" prop="transferTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transferTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户来源" align="center" prop="memberSource" />
      <el-table-column label="服务组对接人" align="center" prop="contactPerson" />
      <el-table-column label="用户具体情况或需求" align="center" prop="userDemand" />
      <el-table-column label="市场对接人" align="center" prop="marketLiaison" />
      <el-table-column label="市场跟踪结果" align="center" prop="followingResults" />
      <el-table-column label="后续跟进" align="center" prop="followUp" />
      <el-table-column label="年度内结果" align="center" prop="annualResults" />
      <el-table-column label="缴费金额" align="center" prop="payPrice" />
      <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:csTransferRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csTransferRecord: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="150px">
        <el-row>
          <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="orderAccount">
              <el-input v-model="form.orderAccount" 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="transferTime">
              <el-date-picker clearable v-model="form.transferTime" 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="memberSource">
              <el-input v-model="form.memberSource" placeholder="请输入用户来源" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务组对接人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入服务组对接人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户具体情况或需求" prop="userDemand">
              <el-input v-model="form.userDemand" placeholder="请输入用户具体情况或需求" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市场对接人" prop="marketLiaison">
              <el-input v-model="form.marketLiaison" placeholder="请输入市场对接人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="市场跟踪结果" prop="followingResults">
              <el-input v-model="form.followingResults" placeholder="请输入市场跟踪结果" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后续跟进" prop="followUp">
              <el-input v-model="form.followUp" placeholder="请输入后续跟进" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年度内结果" prop="annualResults">
              <el-input v-model="form.annualResults" placeholder="请输入年度内结果" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="缴费金额" prop="payPrice">
              <el-input v-model="form.payPrice" placeholder="请输入缴费金额" />
            </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 { listCsTransferRecord, getCsTransferRecord, delCsTransferRecord, addCsTransferRecord, updateCsTransferRecord } from "@/api/cs/csTransferRecord";
import {
    searchMemberList,
    searchCompanyList,
  } from "@/api/cs/csExpansionRecord";
  export default {
    name: "CsTransferRecord",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 转单记录表格数据
        csTransferRecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          keyword: '',
          dateRange: [],
          userName: null,
          userCompanyId: null,
          userCompany: null,
          transferTime: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        memberUserIdList: [],
        userCompanyIdList: []
      };
    },
    created() {
      this.getList();
      this.searchMemberList()
      this.searchCompanyList()
    },
    methods: {
      searchMemberList() {
        searchMemberList().then(res => {
          if (res.code == 200) {
            this.memberUserIdList = res.data
          }
        })
      },
      searchCompanyList() {
        searchCompanyList().then(res => {
          if (res.code == 200) {
            this.userCompanyIdList= res.data
          }
        })
      },
      /** 查询转单记录列表 */
      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];
        }
        listCsTransferRecord(this.queryParams).then(response => {
          this.csTransferRecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      changeUserCompanyId(val){
        if(val){
          this.form.userCompany=val.split('-')[1]
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          transferRecordId: null,
          resId: null,
          resName: null,
          resType: null,
          resourceClassId: null,
          resourceClass: null,
          memberUserId: null,
          userName: null,
          orderAccount: null,
          concatPhone: null,
          userCompanyId: null,
          userCompany: null,
          transferTime: null,
          memberSource: null,
          contactPerson: null,
          userDemand: null,
          marketLiaison: null,
          followingResults: null,
          followUp: null,
          annualResults: null,
          payPrice: 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: [],
          userName: null,
          userCompanyId: null,
          userCompany: null,
          transferTime: null,
        },
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.transferRecordId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加转单记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const transferRecordId = row.transferRecordId || this.ids
        getCsTransferRecord(transferRecordId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改转单记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.transferRecordId != null) {
              updateCsTransferRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCsTransferRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const transferRecordIds = row.transferRecordId || this.ids;
        this.$modal.confirm('是否确认删除转单记录编号为"' + transferRecordIds + '"的数据项？').then(function () {
          return delCsTransferRecord(transferRecordIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => { });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('cs/csTransferRecord/export', {
          ...this.queryParams
        }, `转单记录_${new Date().getTime()}.xlsx`)
      },
      handleExportTemplate() {
        this.download('cs/csTransferRecord/exportTemplate', {
          ...this.queryParams
        }, `转单记录模板_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
