<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="88px">
      <el-row>
        <el-col :span="8">
          <el-form-item class="treeselectForm treeselectForm2" label="组织架构" prop="deptIds">
            <tree
              @treeselectFn="treeselectFn"
              :treeval="queryParams.deptIds"
              style="width: 193px"></tree>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提交人" prop="userId">
            <user
              :userId="queryParams.userId"
              :orgIds="queryParams.deptIds"
              @userIdFn="userIdFn"></user>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="汇报类型" prop="reportTypeList">
            <el-select
              v-model="queryParams.reportTypeList"
              clearable
              multiple
              collapse-tags
              placeholder="请选择汇报类型"
              @change="onInput">
              <el-option
                v-for="item in reportTypeOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="汇报状态" prop="statusList">
            <el-select
              v-model="queryParams.statusList"
              clearable
              placeholder="请选择汇报状态"
              multiple
              collapse-tags
              @change="onInput">
              <el-option
                v-for="item in statusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="岗位类型" prop="reportPostType">
            <el-select
              v-model="queryParams.reportPostType"
              clearable
              collapse-tags
              placeholder="请选择岗位类型"
              @change="onInput">
              <el-option
                v-for="dict in dict.type.report_post_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="报告日期" prop="datetimerange">
            <el-date-picker
              style="width: 193px"
              v-model="datetimerange"
              type="daterange"
              align="right"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="datetimeRangeChange">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="点评人" prop="remarkUserId">
            <user :userId="queryParams.remarkUserId" @userIdFn="remarkUserIdFn"></user>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="点评日期" prop="datetimerange">
            <el-date-picker
              style="width: 193px"
              v-model="remarkTime"
              type="daterange"
              align="right"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="remarkTimeChange">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <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-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:report:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList">
      <el-table-column
        label="公司"
        align="center"
        prop="gsName"
        width="200"
        :show-overflow-tooltip="true" />
      <el-table-column label="大区" prop="dqName" width="140" />
      <el-table-column label="销售部" prop="deptName" width="140" />
      <el-table-column label="提交人工号" prop="workCode" width="140" />
      <el-table-column
        label="提交人姓名"
        align="center"
        prop="nickName"
        width="200"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="提交人职位"
        align="center"
        prop="postName"
        width="140"
        :show-overflow-tooltip="true" />
      <el-table-column label="是否离职" align="center" prop="delFlag" width="84" />
      <!-- 0=未请假,1=全天请假,2=短时请假 -->
      <el-table-column label="是否休假" align="center" prop="isLeave" width="84">
        <template slot-scope="scope">
          <span v-if="scope.row.isLeave == 0">未请假</span>
          <span v-if="scope.row.isLeave == 1">全天请假</span>
          <span v-if="scope.row.isLeave == 2">短时请假</span>
        </template>
      </el-table-column>
      <el-table-column label="入职时间" align="center" prop="contractBegin" width="150" />
      <el-table-column label="提交时间" align="center" prop="commitTime" width="150" />
      <el-table-column label="报告类型" align="center" prop="reportTypeStr" width="84" />
      <el-table-column label="报告日期" align="center" prop="showDate" width="150" />
      <el-table-column label="报告状态" align="center" prop="statusStr" width="100" />
      <el-table-column
        label="审核人"
        align="center"
        prop="pnickName"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="审核人职位"
        align="center"
        prop="ppostName"
        width="140"
        :show-overflow-tooltip="true" />
      <el-table-column label="审核时间" align="center" prop="approvalTime" width="150" />
      <el-table-column label="打分" align="center" prop="number" width="70" />
      <el-table-column label="提交人公司" align="center" prop="ugsName" width="140" />
      <el-table-column label="提交人大区" align="center" prop="udqName" width="140" />
      <el-table-column label="提交人销售部" align="center" prop="udeptName" width="140" />
      <el-table-column label="点评人" align="center" prop="remarkName" width="140" />
      <el-table-column label="点评人岗位" align="center" prop="remarkPost" width="140" />
      <el-table-column label="点评时间" align="center" prop="remarkTime" width="150" />
      <el-table-column
        label="点评内容"
        align="center"
        prop="remark"
        width="150"
        :show-overflow-tooltip="true" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="60px"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            :disabled="scope.row.status == 0"
            @click="handleDetail(scope.row)"
            >详情</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" />
  </div>
</template>

<script>
import { listReport } from '@/api/sfa/report';
import tree from '@/views/components/sfa/treeselectParent';
import user from '@/views/components/sfa/user';
export default {
  name: 'Report',
  dicts: ['report_post_type'],
  components: { tree, user },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店覆盖和线路规则明细表格数据
      reportList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptIds: null,
        reportPostType: null,
        statusList: ['3'],
        reportTypeList: ['1', '2', '3'],
      },
      reportTypeOptions: [
        {
          dictValue: '1',
          dictLabel: '日报',
        },
        {
          dictValue: '2',
          dictLabel: '周报',
        },
        {
          dictValue: '3',
          dictLabel: '半月报',
        },
      ],
      statusOptions: [
        {
          dictValue: '0',
          dictLabel: '待提交',
        },
        {
          dictValue: '1',
          dictLabel: '提交待审批',
        },
        {
          dictValue: '2',
          dictLabel: '驳回待提交',
        },
        {
          dictValue: '3',
          dictLabel: '审批完成',
        },
        {
          dictValue: '-1',
          dictLabel: '过期未提交',
        },
      ],
      chainsList: [],
      rangeNumber: 10,
      resOptions: [],
      datetimerange: [],
      remarkTime: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    onInput() {
      this.$forceUpdate();
    },
    treeselectFn(val) {
      this.queryParams.deptIds = val;
    },
    userIdFn(val) {
      this.queryParams.userId = val;
    },
    remarkUserIdFn(val) {
      this.queryParams.remarkUserId = val;
    },
    /** 查询门店覆盖和线路规则明细列表 */
    getList() {
      this.loading = true;
      this.queryParams.reportPostType =
        this.queryParams.reportPostType == '' ? null : this.queryParams.reportPostType;
      listReport(this.queryParams).then((response) => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.queryParams.deptIds = [];
      this.datetimerange = [];
      this.queryParams.startTime = '';
      this.queryParams.endTime = '';
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'sfa/report/export',
        {
          ...this.queryParams,
        },
        `汇报查询_${new Date().getTime()}.xlsx`
      );
    },
    datetimeRangeChange(val) {
      this.$forceUpdate();
      if (val == undefined || val == null) {
        this.datetimerange = [];
        this.queryParams.startTime = '';
        this.queryParams.endTime = '';
      } else {
        this.queryParams.startTime = val[0];
        this.queryParams.endTime = val[1];
      }
    },
    remarkTimeChange(val) {
      this.$forceUpdate();
      if (val == undefined || val == null) {
        this.remarkTime = [];
        this.queryParams.remarkStartTime = '';
        this.queryParams.remarkEndTime = '';
      } else {
        this.queryParams.remarkStartTime = val[0];
        this.queryParams.remarkEndTime = val[1];
      }
    },
    handleDetail(row) {
      var dayTime = row.date;
      var times = new Date(
        dayTime.slice(0, 4) + '-' + dayTime.slice(4, 6) + '-' + dayTime.slice(6, 8)
      ).getTime();
      var time = new Date('2022-08-25').getTime();
      if (times < time && row.status == -1) {
        this.$alert('非常抱歉，8月25日前历史报告数据不存在', '系统提示', {
          confirmButtonText: '确定',
          callback: (action) => {},
        });
      } else {
        this.$router.push({ path: '/report/reportDetail', query: { id: row.id } });
      }
    },
  },
};
</script>
