<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="申请人" prop="applier">
        <el-select v-model="queryParams.applier" filterable clearable @change="handleQuery">
          <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请部门" prop="useDept">
        <el-select v-model="queryParams.useDept" filterable clearable @change="handleQuery">
          <el-option v-for="item in $store.state.user.deptList" :key="item.deptId" :label="item.deptName"
            :value="item.deptId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目编号" prop="projectId">
        <el-select v-model="queryParams.projectId" clearable filterable remote reserve-keyword placeholder="请输入项目编号"
          :remote-method="remoteMethod" :loading="loading" style="width: 400px;">
          <el-option v-for="project in projectList" :key="project.projectId"
            :label="project.projectNumber + '-' + project.projectName" :value="project.projectId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请类型" prop="carUsage">
        <el-select v-model="queryParams.carUsage" clearable @change="handleQuery">
          <el-option label="项目用车" value="0"></el-option>
          <el-option label="非项目用车" value="1"></el-option>
          <el-option label="工会用车" value="2"></el-option>
          <el-option label="党委用车" value="3"></el-option>
          <el-option label="团委用车" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </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="['car:record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carApprovalList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <!-- <el-table-column label="用车申请id" align="center" prop="carApplyId" /> -->
      <el-table-column label="申请类型" align="center" prop="carUsage">
        <template slot-scope="scope">
          <el-tag :type="setTypeCarUsage(scope.row.carUsage)">
            {{ setCarUsage(scope.row.carUsage) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applier">
        <template slot-scope="scope">
          {{ getUserName(scope.row.applier) }}
        </template>
      </el-table-column>
      <el-table-column label="申请部门" align="center" prop="useDept">
        <template slot-scope="scope">
          {{ getDeptName(scope.row.useDept) }}
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center" prop="project.projectNumber" />
      <el-table-column label="项目名称" align="center" prop="project.projectName" width="200px" />
      <el-table-column label="用车事由" align="center" prop="applyReason" />
      <el-table-column label="乘车人数" align="center" prop="passengers" />
      <el-table-column label="开始日期" align="center" prop="beginDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="天数" align="center" prop="days" />
      <!-- <el-table-column label="部门审批人" align="center" prop="deptUser.nickName" /> -->
      <el-table-column label="申请日期" align="center" prop="applyDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="返回日期" align="center" prop="returnDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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-view" @click="handleView(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" />

    <!-- 添加或修改cmc用车审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="65%" append-to-body>
      <car-form :taskForm="taskForm" :taskName="''" :flowDisabled="false"></car-form>
    </el-dialog>
  </div>
</template>

<script>
import { listCarApproval, getCarApproval, delCarApproval, addCarApproval, updateCarApproval } from "@/api/oa/car/carApproval";
import { listProject } from '@/api/oa/project/project';
import carForm from '../../flowable/form/oa/carForm.vue';
import { mapGetters } from 'vuex'

export default {
  components: { carForm },
  name: "CarApproval",
  computed: {
    ...mapGetters(['roles', 'deptId']),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc用车审批表格数据
      carApprovalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applier: null,
        useDept: null,
        cars: null,
        drivers: null,
        projectId: null,
        carUsage: null,
        applyReason: null,
        passengers: null,
        beginDate: null,
        endDate: null,
        days: null,
        deptUserId: null,
        deptComment: null,
        deptTime: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        unionUserId: null,
        unionComment: null,
        unionTime: null,
        gmUserId: null,
        gmComment: null,
        gmTime: null,
        dispatcher: null,
        dispatchComment: null,
        dispatchTime: null,
        estimateCost: null,
        applyDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      projectList: [],
      taskForm: {
        formId: ''
      }
    };
  },
  created() {
    this.queryParams.carUsage = '0'
    this.getList();
  },
  methods: {
    /** 查询cmc用车审批列表 */
    getList() {
      this.loading = true;
      if (this.roles.some(element => element === "dept")) {        
        if (this.queryParams.carUsage == '0') {
          this.queryParams.undertakingDept = this.deptId;
          this.queryParams.useDept = null;
        }
        else {
          this.queryParams.useDept = this.deptId;
          this.queryParams.undertakingDept = null;
        }
      }
      if (this.roles.some(e => e === 'leader')) {
        this.queryParams.useDept = null;
        this.queryParams.undertakingDept = null;
      }
      listCarApproval(this.queryParams).then(response => {
        this.carApprovalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      listProject({
        pageNum: 1,
        pageSize: 20,
        projectNumber: val
      }).then(res => {
        this.projectList = res.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carApplyId: null,
        applier: null,
        useDept: null,
        cars: null,
        drivers: null,
        projectId: null,
        carUsage: null,
        applyReason: null,
        passengers: null,
        beginDate: null,
        endDate: null,
        days: null,
        deptUserId: null,
        deptComment: null,
        deptTime: null,
        managerUserId: null,
        managerComment: null,
        managerTime: null,
        unionUserId: null,
        unionComment: null,
        unionTime: null,
        gmUserId: null,
        gmComment: null,
        gmTime: null,
        dispatcher: null,
        dispatchComment: null,
        dispatchTime: null,
        estimateCost: null,
        applyDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carApplyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc用车审批";
    },
    handleView(row) {
      this.open = true;
      this.taskForm.formId = row.carApplyId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carApplyId = row.carApplyId || this.ids
      getCarApproval(carApplyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc用车审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carApplyId != null) {
            updateCarApproval(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarApproval(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carApplyIds = row.carApplyId || this.ids;
      this.$modal.confirm('是否确认删除cmc用车审批编号为"' + carApplyIds + '"的数据项？').then(function () {
        return delCarApproval(carApplyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/carApproval/export', {
        ...this.queryParams
      }, `carApproval_${new Date().getTime()}.xlsx`)
    },
    setCarUsage(val) {
      if (val == '0') {
        return '项目用车'
      } else if (val == '1') {
        return '非项目用车'
      } else if (val == '2') {
        return '工会用车'
      } else if (val == '3') {
        return '党委用车'
      } else if (val == '4') {
        return '团委用车'
      }
    },
    setTypeCarUsage(val) {
      if (val == '0') {
        return 'success'
      } else if (val == '1') {
        return 'warning'
      } else {
        return ''
      }
    }
  }
};
</script>
