<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企微公司" prop="corpId">
        <el-select clearable v-model="queryParams.corpId" placeholder="请选择企微公司"  size="small" @change="updateCorpId()">
          <el-option
            v-for="dict in myQwCompanyList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入接替销售公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售名称" prop="companyUserName">
        <el-input
          v-model="queryParams.companyUserName"
          placeholder="请输入接替销售名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable >
          <el-option :value="0" label="全部"/>
          <el-option
            v-for="item in auditStatusOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </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-table v-loading="loading" :data="list" border>
      <el-table-column label="ID" align="center" prop="id" width="55"/>
      <el-table-column label="主体名称" align="center" prop="corpName" />
      <el-table-column label="接替公司名称" align="center" prop="companyName" />
      <el-table-column label="接替销售名称" align="center" prop="companyUserName" />
      <el-table-column label="接替企微用户名称" align="center" prop="qwUserName" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="auditStatusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="审核时间" align="center" prop="auditTime" />
      <el-table-column label="被拒原因" align="center" prop="reason" />
      <el-table-column label="提交人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['qw:externalContactTransferCompanyAudit:detail']"
          >详情</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-drawer title="详情" size="75%" :visible.sync="dialogViewVisible" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            style="margin-left: 10px"
            type="primary"
            plain
            size="mini"
            v-if="currentAudit.status === 2"
            :loading="syncLoading"
            @click="sync"
            v-hasPermi="['qw:externalContactTransferCompanyAudit:sync']"
          >同步</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="detailLoading" :data="userList" border>
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="客户名称" align="center" prop="externalUserName" />
        <el-table-column label="原公司名称" align="center" prop="companyName" />
        <el-table-column label="原销售名称" align="center" prop="companyUserName" />
        <el-table-column label="原企微用户名称" align="center" prop="qwUserName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="接替状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="replaceStatusOptions" :value="scope.row.status"/>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="userTotal>0"
        :total="userTotal"
        :page.sync="userQueryParams.pageNum"
        :limit.sync="userQueryParams.pageSize"
        @pagination="getUserList"
      />
    </el-drawer>
  </div>
</template>

<script>
import { detail, listExternalContactTransferAudit, sync } from '@/api/qw/externalContactTransferAudit'
import { getMyQwCompanyList } from '@/api/qw/user'

export default {
  name: "companyTransferDetail",
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        corpName: null,
        companyUserName: null,
        status: 0,
        corpId: null,
      },
      total: 0,
      list: [],
      auditStatusOptions: [],
      replaceStatusOptions: [],
      dialogAuditVisible: false,
      dialogViewVisible: false,
      detailLoading: false,
      userQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      userList: [],
      userTotal: 0,
      currentAudit: {},
      syncLoading: false,
      myQwCompanyList:[],
    }
  },
  created() {
    getMyQwCompanyList().then(response => {
      this.myQwCompanyList = response.data;
    });
    this.getDicts("sys_qw_transfer_audit_status").then((response) => {
      this.auditStatusOptions = response.data;
    });
    this.getDicts("sys_qw_transfer_status").then((response) => {
      this.replaceStatusOptions = response.data;
    });
    this.handleQuery()
  },
  methods: {
    updateCorpId() {
      this.handleQuery()
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        corpName: null,
        companyUserName: null,
        status: 0,
        corpId: null,
      };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      const params = {
        ...this.queryParams,
        status: this.queryParams.status === 0 ? null : this.queryParams.status
      }
      listExternalContactTransferAudit(params).then(response => {
        this.list = response.rows.map(item => {
          return {
            ...item,
            createTime: item.createTime ? item.createTime.replace("T", " ") : null,
            auditTime: item.auditTime ? item.auditTime.replace("T", " ") : null,
          }
        })
        this.total = response.total
        this.loading = false
      })
    },
    handleView(row) {
      this.currentAudit = row
      this.userQueryParams.pageNum = 1
      this.getUserList()
      this.dialogViewVisible = true
    },
    getUserList() {
      this.detailLoading = true
      detail(this.currentAudit.id, this.userQueryParams).then(response => {
        this.userList = response.rows
        this.userTotal = response.total
        this.detailLoading = false
      })
    },
    sync() {
      const id = this.currentAudit.id
      this.syncLoading = true
      sync(id).then(() => {
        this.getUserList()
        this.syncLoading = false
      })
    },
  }
};
</script>

<style scoped>

</style>
