<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="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目主键" prop="projId">
        <el-input
          v-model="queryParams.projId"
          placeholder="请输入项目主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请类型" prop="applyType">
        <el-select v-model="queryParams.applyType" placeholder="请选择申请类型" clearable>
          <el-option
            v-for="dict in dict.type.pro_apply_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请状态" prop="applyStatus">
        <el-select v-model="queryParams.applyStatus" placeholder="请选择申请状态" clearable>
          <el-option
            v-for="dict in dict.type.pro_apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请用户主键" prop="applyUser">
        <el-input
          v-model="queryParams.applyUser"
          placeholder="请输入申请用户主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用时间" prop="useTime">
        <el-date-picker clearable
          v-model="queryParams.useTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择使用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDel">
        <el-select v-model="queryParams.isDel" placeholder="请选择是否删除" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_del"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:projectApply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:projectApply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:projectApply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:projectApply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="部门主键" align="center" prop="deptId" />
      <el-table-column label="项目主键" align="center" prop="projId" />
      <el-table-column label="申请类型" align="center" prop="applyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_apply_type" :value="scope.row.applyType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pro_apply_status" :value="scope.row.applyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="申请原因" align="center" prop="applyReason" />
      <el-table-column label="申请附件" align="center" prop="applyFiles" />
      <el-table-column label="申请用户主键" align="center" prop="applyUser" />
      <el-table-column label="使用时间" align="center" prop="useTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isDel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_del" :value="scope.row.isDel"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:projectApply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:projectApply:remove']"
          >删除</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="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门主键" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门主键" />
        </el-form-item>
        <el-form-item label="项目主键" prop="projId">
          <el-input v-model="form.projId" placeholder="请输入项目主键" />
        </el-form-item>
        <el-form-item label="申请类型" prop="applyType">
          <el-select v-model="form.applyType" placeholder="请选择申请类型">
            <el-option
              v-for="dict in dict.type.pro_apply_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请状态" prop="applyStatus">
          <el-select v-model="form.applyStatus" placeholder="请选择申请状态">
            <el-option
              v-for="dict in dict.type.pro_apply_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请原因" prop="applyReason">
          <el-input v-model="form.applyReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请附件" prop="applyFiles">
          <el-input v-model="form.applyFiles" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请用户主键" prop="applyUser">
          <el-input v-model="form.applyUser" placeholder="请输入申请用户主键" />
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-date-picker clearable
            v-model="form.useTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否删除" prop="isDel">
          <el-select v-model="form.isDel" placeholder="请选择是否删除">
            <el-option
              v-for="dict in dict.type.sys_is_del"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">项目申请明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddProProjectApplyDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteProProjectApplyDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="proProjectApplyDetailList" :row-class-name="rowProProjectApplyDetailIndex" @selection-change="handleProProjectApplyDetailSelectionChange" ref="proProjectApplyDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="大类主键" prop="superTypeId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.superTypeId" placeholder="请输入大类主键" />
            </template>
          </el-table-column>
          <el-table-column label="大类名称" prop="superTypeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.superTypeName" placeholder="请输入大类名称" />
            </template>
          </el-table-column>
          <el-table-column label="类型主键" prop="typeId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.typeId" placeholder="请输入类型主键" />
            </template>
          </el-table-column>
          <el-table-column label="类型名称" prop="typeName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.typeName" placeholder="请输入类型名称" />
            </template>
          </el-table-column>
          <el-table-column label="资产主键" prop="assetsId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.assetsId" placeholder="请输入资产主键" />
            </template>
          </el-table-column>
          <el-table-column label="资产名称" prop="assetsName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.assetsName" placeholder="请输入资产名称" />
            </template>
          </el-table-column>
          <el-table-column label="资产单位" prop="assetsUnit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.assetsUnit" placeholder="请输入资产单位" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="number" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="使用时间" prop="useTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.useTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择使用时间" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="price" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入单价" />
            </template>
          </el-table-column>
          <el-table-column label="总价" prop="totalPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.totalPrice" placeholder="请输入总价" />
            </template>
          </el-table-column>
          <el-table-column label="是否删除" prop="isDel" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.isDel" placeholder="请选择是否删除">
                <el-option
                  v-for="dict in dict.type.sys_is_del"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectApply, getProjectApply, delProjectApply, addProjectApply, updateProjectApply } from "@/api/project/projectApply";

export default {
  name: "ProjectApply",
  dicts: ['pro_apply_status', 'pro_apply_type', 'sys_is_del'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedProProjectApplyDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目申请表格数据
      projectApplyList: [],
      // 项目申请明细表格数据
      proProjectApplyDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        projId: null,
        applyType: null,
        applyStatus: null,
        applyUser: null,
        useTime: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目申请列表 */
    getList() {
      this.loading = true;
      listProjectApply(this.queryParams).then(response => {
        this.projectApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        projId: null,
        applyType: null,
        applyStatus: null,
        applyReason: null,
        applyFiles: null,
        applyUser: null,
        useTime: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.proProjectApplyDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProjectApply(id).then(response => {
        this.form = response.data;
        this.proProjectApplyDetailList = response.data.proProjectApplyDetailList;
        this.open = true;
        this.title = "修改项目申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.proProjectApplyDetailList = this.proProjectApplyDetailList;
          if (this.form.id != null) {
            updateProjectApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目申请编号为"' + ids + '"的数据项？').then(function() {
        return delProjectApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 项目申请明细序号 */
    rowProProjectApplyDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 项目申请明细添加按钮操作 */
    handleAddProProjectApplyDetail() {
      let obj = {};
      obj.superTypeId = "";
      obj.superTypeName = "";
      obj.typeId = "";
      obj.typeName = "";
      obj.assetsId = "";
      obj.assetsName = "";
      obj.assetsUnit = "";
      obj.number = "";
      obj.useTime = "";
      obj.useReason = "";
      obj.price = "";
      obj.totalPrice = "";
      obj.isDel = "";
      obj.remark = "";
      this.proProjectApplyDetailList.push(obj);
    },
    /** 项目申请明细删除按钮操作 */
    handleDeleteProProjectApplyDetail() {
      if (this.checkedProProjectApplyDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的项目申请明细数据");
      } else {
        const proProjectApplyDetailList = this.proProjectApplyDetailList;
        const checkedProProjectApplyDetail = this.checkedProProjectApplyDetail;
        this.proProjectApplyDetailList = proProjectApplyDetailList.filter(function(item) {
          return checkedProProjectApplyDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleProProjectApplyDetailSelectionChange(selection) {
      this.checkedProProjectApplyDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/projectApply/export', {
        ...this.queryParams
      }, `projectApply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
