<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="110px"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="业务申请号" prop="businessApplicantNo">
            <el-input
              v-model="queryParams.businessApplicantNo"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="论文名称" prop="name">
            <el-input
              v-model="queryParams.name"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请人" prop="applicantName">
            <el-input
              v-model="queryParams.applicantName"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="审核状态" prop="status">
            <el-select v-model="queryParams.status">
              <el-option v-for="(name, key) in APPROVE_STATUS_DICT" :key="key" :label="name" :value="Number(key)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="作者姓名" prop="authorName">
            <el-input
              v-model="queryParams.authorName"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="期刊类型" prop="journalType">
            <el-input
              v-model="queryParams.journalType"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="期刊级别" prop="journalLvl">
            <el-input
              v-model="queryParams.journalLvl"
              clearable
            />
          </el-form-item>
        </el-col>
        <!-- TODO，发表日期未确定 -->
        <el-col :span="6">
          <el-form-item label="发表日期" prop="publicationDate">
            <el-date-picker
              v-model="queryParams.publicationDate"
              placeholder="请选择"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请日期" prop="applicantTime">
            <el-date-picker
              v-model="queryParams.applicantTime"
              placeholder="请选择"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="onSearch">
              搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="onReset">重置</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="primary" plain icon="el-icon-plus" size="mini" @click="handleAddClick">
          新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="listData" border>
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务申请号" align="center" prop="businessApplicantNo" />
      <el-table-column label="论文名称" align="center" prop="name" />
      <el-table-column label="作者姓名（按贡献排序）" align="center" prop="authorName" />
      <el-table-column label="通讯作者" align="center" prop="communicationAuthor" />
      <el-table-column label="期型名称" align="center" prop="journalName" />
      <el-table-column label="刊号" align="center" prop="journalNo" />
      <el-table-column label="操作" align="center" prop="operate">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEditClick(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelClick(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"
    />

    <el-dialog :title="dialogTitle" :visible.sync="visible" width="1200px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-row>
          <el-col :span="8">
            <el-form-item label="论文名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="作者姓名" prop="authorName">
              <el-input v-model="form.authorName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="通讯作者" prop="communicationAuthor">
              <el-input v-model="form.communicationAuthor" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="第一作者单位" prop="firstAuthorDept">
              <el-input v-model="form.firstAuthorDept" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期刊名称" prop="journalName">
              <el-input v-model="form.journalName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期刊号" prop="journalNo">
              <el-input v-model="form.journalNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="期刊类型" prop="journalType">
              <el-input v-model="form.journalType" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="期刊级别" prop="journalLvl">
              <el-input v-model="form.journalLvl" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请人姓名" prop="applicantName">
              <el-input v-model="form.applicantName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="申请人邮箱" prop="applicantEmail">
              <el-input v-model="form.applicantEmail" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请人电话" prop="applicantPhone">
              <el-input v-model="form.applicantPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="申请日期" prop="applicantTime">
              <el-date-picker
                v-model="form.applicantTime"
                placeholder="请选择"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="论文申请单位" prop="paperApplicantDept">
              <el-input v-model="form.paperApplicantDept" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="关联项目号" prop="associationProjectNo">
              <el-input v-model="form.associationProjectNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            
            <el-form-item label="关联项目名称" prop="associationProjectName">
              <el-input v-model="form.associationProjectName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="业务申请号" prop="businessApplicantNo">
              <el-input v-model="form.businessApplicantNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建人工号" prop="createNo">
              <el-input v-model="form.createNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
          
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="创建人" prop="createName">
              <el-input v-model="form.createName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建日期" prop="createDate">
              <el-date-picker
                v-model="form.createDate"
                placeholder="请选择"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发表日期" prop="publicationDate">
              <el-date-picker
                v-model="form.publicationDate"
                placeholder="请选择"
                value-format="yyyy-MM-DD"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="简要内容描述" prop="content">
              <el-input type="textarea" v-model="form.content" placeholder="请输入" :rows="5" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 * as paperManageService from "@/api/achievementManage/paperManage";

const APPROVE_STATUS_DICT = {
  0: "草稿",
  1: "待审核",
  2: "已审核"
};

export default {
  name: "PaperManage",
  data() {
    return {
      loading: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      listData: [],
      visible: false,
      formData: {},
      dialogTitle: "新增",
      APPROVE_STATUS_DICT,
      rules: {
        name: { required: true, message: "请输入论文名称" },
        authorName: { required: true, message: "请输入作者姓名" },
        applicantEmail: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        applicantPhone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      const params = { ...this.queryParams };
      this.loading = true;
      paperManageService.listData(params).then(res => {
        this.loading = false;
        if (res.code == 200) {
          this.listData = res.rows;
          this.total = res.total;
        }
      }).catch(err => {
        this.loading = false;
      });
    },
    onSearch() {
      this.getList();
    },
    onReset() {
      this.queryParams = {};
      this.getList();
    },
    saveOrUpdateData() {
      let func = this.form.id ? paperManageService.updateData: paperManageService.addData;
      let operateMsg = this.form.id ? "修改" : "新增";
      const params = { ...this.form };
      func(params).then(res => {
        if (res.code == 200) {
          this.$message.success(operateMsg + "成功!");
          this.cancel();
          this.getList();
        }
      });
    },
    handleAddClick() {
      this.visible = true;
    },
    handleEditClick(rowData) {
      this.visible = true;
      this.form = { ...rowData, dateRange: [] };
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveOrUpdateData();
        }
      })
    },
    cancel() {
      this.visible = false;
      this.form = {};
      this.resetForm("form");
    },
    handleDelClick(rowData) {
      this.$modal
        .confirm('是否确认删除论文名称为"' + rowData.name + '"的数据项？')
        .then(function () {
          return paperManageService.delData(rowData.id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
