<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名" prop="companyId">
        <el-select filterable  v-model="queryParams.companyId" placeholder="请选择公司名"  @change="companyChange" clearable size="small">
          <el-option
            v-for="item in companys"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyId"
          />
        </el-select>
      </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>
        <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-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name="00"></el-tab-pane>
      <el-tab-pane label="待销售审批" name="0"></el-tab-pane>
      <el-tab-pane label="销售审批拒绝" name="1"></el-tab-pane>
      <el-tab-pane label="待总后台审核" name="2"></el-tab-pane>
      <el-tab-pane label="总后台审核拒绝" name="3"></el-tab-pane>
      <el-tab-pane label="审核通过" name="4"></el-tab-pane>
    </el-tabs>
    <el-table height="500" border v-loading="loading" :data="storeOrderAuditList" :key="tableKey">
      <el-table-column label="订单号" align="center" prop="orderCode" width="200" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="所属员工" align="center" prop="companyUserName" />
      <el-table-column label="审核状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag prop="status" v-for="item in statusOptions"
                  :type="item.type"
                  v-if="scope.row.auditStatus === item.dictValue">
            {{item.dictLabel}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="销售审核时间" align="center" prop="companyAuditTime" />
      <el-table-column label="销售审核人" align="center" prop="companyAuditUserName" />
      <el-table-column label="总后台审核时间" align="center" prop="adminAuditTime" />
      <el-table-column label="总后台审核人" align="center" prop="adminAuditUserName" />
      <el-table-column label="被拒原因" align="center" prop="reason" show-overflow-tooltip />
      <el-table-column label="提交时间" align="center" prop="createTime" />
      <el-table-column label="操作" fixed="right" align="center" width="80" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popover
            v-if="scope.row.auditStatus === 2"
            placement="right"
            trigger="click"
            :ref="'popover_' + scope.row.id">
            <el-button size="mini" type="success" @click="handlePass(scope.row)">通过</el-button>
            <el-button size="mini" type="danger" @click="handleAudit(scope.row)">拒绝</el-button>
            <el-button
              slot="reference"
              size="mini"
              type="text"
              v-hasPermi="['store:storeOrderAudit:audit']"
            >审核订单</el-button>
          </el-popover>
        </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="show.title" :visible.sync="show.open" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="拒绝原因" prop="reviewContent">
          <el-input
            type="textarea"
            v-model="auditForm.reviewContent"
            placeholder="请输入拒绝原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleReject">确定</el-button>
        <el-button @click="cancelAudit">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCompanyList } from "@/api/company/company";
import {list, audit} from "@/api/store/storeOrderAudit";

export default {
  name: "StoreOrderAudit",
  data() {
    const statusOptions = [
      {
        dictLabel: "待销售审批",
        dictValue: 0,
        type: ''
      },
      {
        dictLabel: "销售审批拒绝",
        dictValue: 1,
        type: 'danger'
      },
      {
        dictLabel: "待总后台审核",
        dictValue: 2,
        type: ''
      },
      {
        dictLabel: "总后台审核拒绝",
        dictValue: 3,
        type: 'danger'
      },
      {
        dictLabel: "审核通过",
        dictValue: 4,
        type: 'success'
      }
    ]

    return {
      tableKey: 0,
      showSearch: true,
      companys: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyId: null,
        companyUserName: null,
        auditStatus: null
      },
      activeName: '00',
      loading: false,
      storeOrderAuditList: [],
      statusOptions: statusOptions,
      show: {
        open: false,
        title: "审核订单"
      },
      auditForm: {
        auditId: null,
        reviewType: null,
        reviewContent: ''
      },
      auditRules: {
        reviewContent: [
          { required: true, message: "请输入拒绝原因", trigger: "blur" }
        ]
      },
      currentRowId: null,
    }
  },
  created() {
    this.getCompanyOptions()
    this.handleQuery()
  },
  activated() {
    this.tableKey = Date.now()
  },
  methods: {
    getCompanyOptions() {
      getCompanyList().then(response => {
        this.companys = response.data
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true
      list(this.queryParams).then(response => {
        const {rows, total} = response
        this.storeOrderAuditList = rows
        this.total = total
        this.loading = false
      })
    },
    handleClick(tab) {
      this.queryParams.auditStatus = tab.name === '00' ? null : tab.name
      this.handleQuery()
    },
    companyChange(val) {
      this.queryParams.companyId = val
    },
    handleAudit(row) {
      this.currentRowId = row.id
      this.auditForm.auditId = row.id;
      this.auditForm.reviewType = 0;
      this.show.open = true;
    },
    handlePass(row) {
      this.currentRowId = row.id
      this.auditForm.auditId = row.id;
      this.auditForm.reviewType = 1;
      this.submitAudit();
    },
    handleReject() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          this.submitAudit();
        }
      });
    },
    submitAudit() {
      audit(this.auditForm).then(response => {
        const {msg} = response
        this.msgSuccess(msg);
        this.show.open = false;
        this.$refs['popover_' + this.currentRowId].doClose();
        this.getList();
        this.resetAuditForm();
      });
    },
    cancelAudit() {
      this.show.open = false;
      this.resetAuditForm();
    },
    resetAuditForm() {
      this.auditForm = {
        auditId: null,
        reviewType: null,
        reviewContent: ''
      };
      this.$refs["auditForm"]?.resetFields();
    }
  }
}
</script>

<style scoped>

</style>
