<template>
  <basic-container>
    <avue-crud
      v-bind="bindVal"
      v-on="onEvent"
      v-model="form"
      :page.sync="page"
      :before-open="handleBeforeOpen"
      @row-dblclick="handleRowDbClick"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleCreateClick"
        >
          创建立项项目
        </el-button>
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          导出
        </el-button>
        <print-table-btn @click="printTable" />
        
        <div style="display: flex; align-items: center;">
          <year-month-select v-model="params.yearAndMonth" :showAllYear="false"></year-month-select>
        </div>
      </template>

      <template slot="lxbg" slot-scope="{ row }">
        <span v-if="row.lxbg === '未提交'" style="color: red;">未提交</span>
        <span v-if="row.lxbg === '已提交'">已提交</span>
      </template>

      <template slot="jdxbg" slot-scope="{ row }">
        <span v-if="row.jdxbg === '未提交'" style="color: red;">未提交</span>
        <span v-if="row.jdxbg === '已提交'">已提交</span>
      </template>

      <template slot="ysbg" slot-scope="{ row }">
        <span v-if="row.ysbg === '未提交'" style="color: red;">未提交</span>
        <span v-if="row.ysbg === '已提交'">已提交</span>
      </template>
      
    </avue-crud>

    <WideTablePrinter
      ref="printWideTable"
      :columns="wideTableColumns"
      :data="data"
      :print-title="printTitle"
      :rows-per-page="30"
      :default-landscape="true"
    />

    <el-drawer
      :visible.sync="drawerVisible"
      title="项目信息"
      modal-append-to-body
      append-to-body
      size="1280px"
      destroy-on-close
    >
      <el-tabs type="border-card">
        <el-tab-pane label="1、项目立项" lazy>
          <apply-form :projectId="currProjectId" :operateType="operateType" @success="handleApplySuccess"></apply-form>
        </el-tab-pane>
        <el-tab-pane label="2、科研预算" lazy>
          <project-budget :projectId="currProjectId"></project-budget>
        </el-tab-pane>
        <el-tab-pane label="3、项目实施" lazy>
          <implement :projectId="currProjectId"></implement>
        </el-tab-pane>
        <el-tab-pane label="4、项目变更" lazy>
          <change :projectId="currProjectId"></change>
        </el-tab-pane>
        <el-tab-pane label="5、项目结题" lazy>
          <accept-form :projectId="currProjectId"></accept-form>
        </el-tab-pane>
        <el-tab-pane label="6、项目成果" lazy>
          <result-table :projectId="currProjectId"></result-table>
        </el-tab-pane>
        <el-tab-pane label="7、委外材料" lazy>
          <outsourcing-doc :projectId="currProjectId"></outsourcing-doc>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </basic-container>
</template>

<script>
import {exportBloByPost} from "@/api/common";
import YearMonthSelect from "@/components/year-month-select";
import moment from "moment";
import {getToken} from "@/util/auth";
import {downloadXls} from "@/util/util";
import applyForm from "./components/apply-form.vue";
import projectBudget from "./components/project-budget.vue";
import implement from './implement.vue';
import change from "./change.vue";
import acceptForm from "./components/accept-form.vue";
import resultTable from "./result-table.vue";
import outsourcingDoc from "./components/outsourcing-doc.vue";
import { Row } from "element-ui";


export default window.$crudCommon({
  components: {
    YearMonthSelect,
    applyForm,
    projectBudget,
    implement,
    change,
    acceptForm,
    resultTable,
    outsourcingDoc,
  },
  data() {
    return {
      params: {
        yearAndMonth: moment(new Date()).format('YYYYMM'),
      },

      wideTableColumns: [],
      printTitle: "",

      drawerVisible: false,

      applyType: '', // add || edit
      currProjectId: '',
    };
  },
  watch: {
    'params.yearAndMonth'(newVal) {
      this.page.currentPage = 1;
      this.loadData(this.page);
    }
  },
  methods: {
    handleCreateClick() {
      this.operateType = 'add',
      this.currProjectId = '';
      this.drawerVisible = true;
    },
    handleRowDbClick(data) {
      this.operateType = 'edit';
      this.currProjectId = data.id;
      this.drawerVisible = true;
    },
    handleApplySuccess() {
      this.drawerVisible = false;
      this.loadData();
    },
    handleExport() {
      exportBloByPost(`/api/kd-scientific/technician/export?${this.website.tokenHeader}=${getToken()}`, this.params).then(res => {
        downloadXls(res.data, `科研项目汇总表${this.params.yearAndMonth}.xlsx`);
      });
    },
    /**
     * 打印表格
     * @param isLandscape 是否横向打印
     */
    printTable(isLandscape) {
      this.wideTableColumns = this.$refs.crud.columnOption;
      this.printTitle = `科研项目汇总表${this.params.yearAndMonth}`;
      this.$nextTick(() => {
        this.$refs.printWideTable.printTable(isLandscape);
      })
    },
  },
}, {
  // 模块路径
  name: 'projectManage/projectList',
  res: ({ data }) => {
    data.records = data.records.map(item => {
      item.lxbg = item.lxbgCount > 0 ? '已提交' : '未提交';
      item.jdxbg = item.jdxbgCount > 0 ? '已提交' : '未提交';
      item.ysbg = item.ysbgCount > 0 ? '已提交' : '未提交';
      return item;
    });
    return data;
  },
});
</script>
