<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <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="corpName">
        <el-input
          v-model="queryParams.corpName"
          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="cyan" 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="主体名称" 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="handleAudit(scope.row)"
            v-if="scope.row.status === 1"
            v-hasPermi="['qw:externalContactTransferCompanyAudit:audit']"
          >审核</el-button>
          <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-dialog title="审核" :visible.sync="dialogAuditVisible" width="800px" append-to-body>
      <el-form :model="from" ref="auditForm" :rules="rules" label-width="100px">
        <el-form-item label="审核结果" prop="type">
          <el-radio-group v-model="from.type">
            <el-radio label="1">通过</el-radio>
            <el-radio label="2">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="from.type === '2'" label="拒绝原因" prop="reason">
          <el-input type="textarea" :rows="2" v-model="from.reason" placeholder="请输入拒绝原因" clearable size="small" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary"
                   @click="submitForm"
                   :disabled="submitLoading"
                   :loading="submitLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer title="详情" size="75%" :visible.sync="dialogViewVisible" append-to-body>
      <el-table v-loading="detailLoading" :data="userList" border>
        <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>
    </el-drawer>
  </div>
</template>

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

export default {
  name: "externalContactTransferAudit",
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        corpName: null,
        companyUserName: null,
        status: 0,
      },
      total: 0,
      list: [],
      auditStatusOptions: [],
      replaceStatusOptions: [],
      dialogAuditVisible: false,
      dialogViewVisible: false,
      detailLoading: false,
      userList: [],
      from: {
        id: null,
        type: null,
        reason: null
      },
      rules: {
        type: [
          { required: true, message: "请选择审核结果", trigger: "blur" }
        ]
      },
      submitLoading: false
    }
  },
  created() {
    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: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        corpName: null,
        companyUserName: null,
        status: 0,
      };
      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
      })
    },
    reset() {
      this.from = {
        id: null,
        type: null,
        reason: null
      }
      this.resetForm("auditForm")
    },
    handleAudit(row) {
      this.reset()
      this.from.id = row.id
      this.submitLoading = false
      this.dialogAuditVisible = true
    },
    handleView(row) {
      this.detailLoading = true
      detail(row.id).then(response => {
        this.userList = response.data
        this.detailLoading = false
      })
      this.dialogViewVisible = true
    },
    submitForm() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          if (this.submitLoading) {
             return
          }
          this.submitLoading = true
          const params = {
            auditId: this.from.id,
            type: this.from.type,
            reason: this.from.reason
          }
          audit(params).then(() => {
            this.$message.success("操作成功, 请在详情查看转接结果");
            this.dialogAuditVisible = false
            this.getList()
          })
        }
      })
    },
    cancel() {
      this.dialogAuditVisible = false
      this.reset()
    },
  },
}
</script>

<style scoped>

</style>
