<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="投资额度" prop="investmentQuota">
        <el-input
          v-model="queryParams.investmentQuota"
          placeholder="请输入投资额度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="投资方联系人" prop="contactsName">
        <el-input
          v-model="queryParams.contactsName"
          placeholder="请输入投资方联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="引荐方联系人" prop="contactsName">
        <el-input
          v-model="queryParams.recommendPhone"
          placeholder="请输入引荐方联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="联系方式" prop="contactsPhone">
        <el-input
          v-model="queryParams.contactsPhone"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="上报状态" prop="reportStatus">
        <el-select v-model="queryParams.reportStatus" placeholder="请选择上报状态" clearable size="small">
          <el-option label="未上报" value="0" />
          <el-option label="已上报" value="1" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="项目id*" prop="bmProjectId">
        <el-input
          v-model="queryParams.bmProjectId"
          placeholder="请输入项目id*"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="是否删除" prop="isDel">
        <el-select v-model="queryParams.isDel" placeholder="请选择是否删除" clearable size="small">
          <el-option label="请选择字典生成" 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="['BmProjectReport:report: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="['BmProjectReport:report:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleReport"
        >上报</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="['BmProjectReport:report: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="['BmProjectReport:report:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="项目上报id" align="center" prop="id" />-->
      <el-table-column label="投资方" align="center" prop="ivestor" />
      <el-table-column label="拟投资项目" align="center" prop="projectName" />
      <el-table-column label="投资额度" align="center" prop="investmentQuota" />
      <el-table-column label="投资方联系人" align="center" prop="contactsName" />
      <el-table-column label="投资方联系方式" align="center" prop="contactsPhone" />
      <el-table-column label="项目引荐单位" align="center" prop="recommendCompany" />
      <el-table-column label="引荐方联系方式" align="center" prop="recommendPhone" />
      <el-table-column label="上报时间"  width="150px" align="center" prop="createTime" />
<!--      <el-table-column label="项目基本内容" align="center" prop="projectIntroduction" />-->
      <el-table-column label="上报状态" align="center" prop="reportStatus" >
        <template slot-scope="scope">
          <div>{{scope.row.reportStatus == 0 ? '未上报' : '已上报'}}</div>
         </template>
      </el-table-column>
<!--      <el-table-column label="附件url" align="center" prop="url"  width="120px">
        <template slot-scope="scope">
          <div><img class="imjuety" v-if="scope.row.url !== null" :src="scope.row.url" alt="">
          <img class="imjuety" v-if="scope.row.url == null" src="../../../assets/404_images/weiy.png" alt=""></div>
         </template>
      </el-table-column>-->
      <!-- <el-table-column label="附件url" align="center" prop="url" />
       <template slot-scope="scope">
         <div>{{scope.row.url}}</div>
        </template>
      </el-table-column> -->
<!--      <el-table-column label="项目id*" align="center" prop="bmProjectId" />-->
<!--      <el-table-column label="是否删除" align="center" prop="isDel" />-->
      <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="['BmProjectReport:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReport(scope.row)"
            v-hasPermi="['BmProjectReport:report:edit']"
          >上报</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['BmProjectReport:report: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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="投资方" prop="ivestor">
          <el-input v-model="form.ivestor" placeholder="请输入投资方" />
        </el-form-item>
        <el-form-item label="拟投资项目" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入拟投资项目" />
        </el-form-item>
        <el-form-item label="投资额度" prop="investmentQuota">
          <el-input v-model="form.investmentQuota" placeholder="请输入投资额度" />
        </el-form-item>
        <el-form-item label="投资方联系人" prop="contactsName">
          <el-input v-model="form.contactsName" placeholder="请输入投资方联系人" />
        </el-form-item>
        <el-form-item label="投资方联系方式" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入投资方联系方式" />
        </el-form-item>
        <el-form-item label="项目引荐单位" prop="recommendCompany">
          <el-input v-model="form.recommendCompany" placeholder="请输入项目引荐单位" />
        </el-form-item>
        <el-form-item label="引荐方联系方式" prop="recommendPhone">
          <el-input v-model="form.recommendPhone" placeholder="请输入引荐方联系方式" />
        </el-form-item>
        <el-form-item label="项目基本内容">
          <editor v-model="form.projectIntroduction" :min-height="192"/>
        </el-form-item>
<!--        <el-form-item label="附件" prop="url">
         <img class="imjue" v-if="form.url !== null" :src="form.url" alt="">
         <img class="imjue" v-if="form.url == null" src="../../../assets/404_images/weiy.png" alt="">
        </el-form-item>-->
        <el-form-item label="是否删除" prop="isDel">
          <el-select v-model="form.isDel" placeholder="请选择是否删除">
            <el-option label="否" value='0'/>
            <el-option label="是" value='1' />
          </el-select>
        </el-form-item>
        <el-form-item label="归属部门" prop="sysDeptId">
          <treeselect :multiple="true" v-model="form.sysDeptId" :options="deptOptionstr"  placeholder="请选择归属部门" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </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 { listReport, getReport, delReport, addReport, updateReport, exportReport } from "@/api/projectReport/index";
import Editor from '@/components/Editor';
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Report",
  components: {
    Editor,
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 部门树选项
      deptOptionstr: undefined,
      // 总条数
      total: 0,
      // 项目上报表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        investmentQuota: null,
        contactsName: null,
        contactsPhone: null,
        projectIntroduction: null,
        reportStatus: null,
        url: null,
        bmProjectId: null,
        // 部门树选项
        deptOptions: undefined,
        isDel: null,
        ivestor: null,
        recommendCompany: null,
        recommendPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询项目上报列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        console.log(this.reportList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        // 部门树选项
        deptOptions: undefined,
        sysDeptId:null,
        projectName: null,
        investmentQuota: null,
        contactsName: null,
        contactsPhone: null,
        projectIntroduction: null,
        reportStatus: "0",
        url: null,
        bmProjectId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        ivestor: null,
        recommendCompany: null,
        recommendPhone: null,
        remark: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      let yhehg = []
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.form.sysDeptId  = JSON.parse(this.form.sysDeptId)
        // if(this.form.sysDeptId == '[]'){
        //   console.log(345)
        //   this.form.sysDeptId = []
        // }else{
        //   this.form.sysDeptId = this.form.sysDeptId.split(',')

        //   this.form.sysDeptId.filter(route=>{
        //     route = route - 0
        //     yhehg.push(route)
        //   })
        //   this.form.sysDeptId = yhehg
        // }

        if(this.form.sysDeptId !== undefined && this.form.sysDeptId !==null){
          console.log(this.form.sysDeptId)
          // if(this.form.sysDeptId !== []){

          // }

        // this.form.sysDeptId = this.form.sysDeptId
        // this.form.sysDeptId = this.form.sysDeptId.split(',')
        console.log(this.form.sysDeptId)
        }else{
          this.form.sysDeptId = []
        }

        console.log(this.form.url)
        this.open = true;
        this.title = "修改项目上报";
      });
    },

    /** 项目上报 */
    handleReport(row){
      this.reset();
      this.form.id = row.id || this.ids[0];
      console.log(this.form)
      this.form.reportStatus = '1';
      updateReport(this.form).then(response => {
        this.msgSuccess("上报成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.sysDeptId  = this.form.sysDeptId.join(',')
      // if(this.form.sysDeptId.length == 0){
      //   this.form.sysDeptId = undefined
      // }
      this.form.sysDeptId  = JSON.stringify(this.form.sysDeptId)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.sysDeptId  = this.form.sysDeptId.join(',')
            addReport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除项目上报编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptionstr = response.data;
      });
    },
    /** 导出按钮操作 */
    /** 导出按钮操作 */
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有参数数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          console.log(12)
          return exportReport(queryParams);
        }).then(response => {
          console.log(34)
          this.download(response.msg);
        })
    },
    // handleExport() {
    //   this.download('/bmProjectReport/report/export', {
    //     ...this.queryParams
    //   }, `post_${new Date().getTime()}.xlsx`)
    // }
  }
};
</script>
<style>
  .imjue{
    width: 200px;
    height: 200px;
  }
  .imjuety{
    width: 100px;
    height: 100px;
  }
</style>
