<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="createNew">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">科研立项申报</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="state_value">
          <el-select
              class="mr10"
              v-model="state_value"
              placeholder="请选择状态"
              @change="getListData"
          >
            <el-option
                v-for="item in state_list"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
              placeholder="请输入搜索关键字"
              v-model="keyword"
              clearable
              @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
        >搜索
        </el-button
        >
      </el-form>
    </div>
    <el-tabs v-model="activeName">
      <el-tab-pane label="我申请的" name="first">
        <el-table :data="applyTable" tooltip-effect="dark" style="width: 100%">
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="序号" align="center" type="index" width="50">
          </el-table-column>
          <el-table-column
              label="标题"
              show-overflow-tooltip
              prop="kylx_title"
              width="auto"
          >
          </el-table-column>
          <el-table-column
              label="审核人"
              width="200"
              prop="kylx_shr"
          ></el-table-column>
          <el-table-column
              label="日期"
              width="150"
              prop="kylx_sqrq"
          ></el-table-column>
          <el-table-column label="状态" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.kylx_zt_option_k == 1">未提交</span>
              <span v-if="scope.row.kylx_zt_option_k == 2">待审核</span>
              <span v-if="scope.row.kylx_zt_option_k == 3">已通过</span>
              <span v-if="scope.row.kylx_zt_option_k == 4">未通过</span>
            </template>
          </el-table-column>
          <el-table-column label="类别" prop="kylx_lb" width="100">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" @click="editData(scope.row)"
              >编辑
              </el-button>
              <el-button
                  type="text"
                  :disabled="scope.row.kylx_zt_option_k > 1"
                  @click="submitData(scope.row)"
              >提交审核
              </el-button>
              <el-button type="text" @click="delData(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="待我审核的" name="second">
        <el-table :data="auditTable" tooltip-effect="dark" style="width: 100%">
          <el-table-column type="selection" align="center" width="55">
          </el-table-column>
          <el-table-column label="序号" align="center" type="index" width="50">
          </el-table-column>
          <el-table-column
              label="标题"
              show-overflow-tooltip
              prop="kylx_title"
              width="auto"
          >
          </el-table-column>
          <el-table-column
              label="申请人"
              width="200"
              prop="kylx_sqr"
          ></el-table-column>
          <el-table-column
              label="日期"
              width="150"
              prop="kylx_sqrq"
          ></el-table-column>
          <el-table-column label="状态" width="100">
            <template slot-scope="scope">
              <span v-if="scope.row.kylx_zt_option_k == 1">未提交</span>
              <span v-if="scope.row.kylx_zt_option_k == 2">待审核</span>
              <span v-if="scope.row.kylx_zt_option_k == 3">已通过</span>
              <span v-if="scope.row.kylx_zt_option_k == 4">未通过</span>
            </template>
          </el-table-column>
          <el-table-column label="类别" prop="kylx_lb" width="100">
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="verify(3, scope.row)"
              >通过
              </el-button>
              <el-button type="text" @click="verify(4, scope.row)"
              >不通过
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
    <footer
        class="flex-item-none"
        style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
          background
          layout="total,prev, pager, next"
          :page-size="limit"
          :total="total"
          :current-page.sync="cur_page"
          @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="750px">
      <el-form :model="dialogForm" label-width="70px">
        <el-form-item label="标题" prop="title">
          <el-input
              v-model="dialogForm.title"
              placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="lb">
          <el-select
              style="width: 100%"
              v-model="dialogForm.lb"
              placeholder="请选择类别"
          >
            <el-option
                v-for="item in lbList"
                :key="item.kylb_id"
                :label="item.kylb_title"
                :value="item.kylb_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人员" prop="shr">
          <el-select
              style="width: 100%"
              v-model="dialogForm.shr"
              placeholder="请选择审核人员"
          >
            <el-option
                v-for="item in userList"
                :key="item.user_id"
                :label="item.user_realname"
                :value="{value:item.user_id,label:item.user_realname}"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <Tinymce
              :key="timestamp"
              v-if="dialogFormVisible"
              v-model="dialogForm.content"
              :height="300"
          ></Tinymce>
        </el-form-item>
        <el-form-item label="日期" prop="time">
          <el-date-picker
              style="width: 400px"
              v-model="dialogForm.time"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="其他附件">
          <FileUpload :limit="5" :full.sync="dialogForm.files" :key="timestamp" multiple>
            <div slot="tip" class="el-upload__tip">
              支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。
            </div>
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {useUserStore} from "@/stores/user";

const {real_name, dept_ids} = useUserStore();
import Tinymce from "@/components/TinymceVue/index.vue";
import FileUpload from "@/components/FileUpload/index.vue";
import {
  org_users,
  kylb_list,
  kylx_list,
  kylx_add,
  kylx_edit,
  kylx_del,
  kylx_verify,
} from "./api";

export default {
  name: "index",
  components: {
    Tinymce,
    FileUpload,
  },
  data() {
    return {
      state_value: "",
      state_list: [
        {
          label: "全部",
          value: "0",
        },
        {
          label: "未提交",
          value: "1",
        },
        {
          label: "待审核",
          value: "2",
        },
        {
          label: "已通过",
          value: "3",
        },
        {
          label: "未通过",
          value: "4",
        },
      ],
      keyword: "",
      title: "",
      activeName: "first",
      applyTable: [],
      auditTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogFormVisible: false,
      lbList: [],
      dialogForm: {
        kylx_id: "",
        title: "",
        shr:{
          label:'',
          value:''
        },
        content: "",
        time: "",
        lb: "",
        files: "",
      },
      isEdit: false,
      timestamp: Date.now(),
      userList: []
    };
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999
      }
      org_users(data).then(res => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      })
    },
    getkylbList() {
      let data = {
        page: 1,
        limit: 999,
      };
      kylb_list(data).then((res) => {
        this.lbList = res.data.page_data;
      });
    },
    getListData() {
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        kylx_zt: this.state_value,
      };
      kylx_list(data).then((res) => {
        this.applyTable = res.data.page_data;
        this.auditTable = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    createNew() {
      this.isEdit = false;
      this.title = "科研立项申报";
      this.dialogForm = {
        kylx_id: "",
        title: "",
        content: "",
        time: "",
        lb: "",
        files: '',
        shr: {
          label:'',
          value:''
        }
      };
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.isEdit = true;
      this.title = "编辑科研立项";
      this.dialogForm = {
        kylx_id: item.kylx_id,
        title: item.kylx_title,
        content: item.kylx_content,
        time: item.kylx_sqrq,
        lb: item.kylx_lb,
        files: item.kylx_fj,
        shr:{
           label:item.kylx_shr,
           value: item.kylx_shr_user_id
        }
      };
      this.timestamp = Date.now();
      this.dialogFormVisible = true;
    },
    formSubmit() {
      let data = {
        kylx_title: this.dialogForm.title,
        kylx_content: this.dialogForm.content,
        kylx_sqr: real_name,
        kylx_sqrq: this.dialogForm.time,
        kylx_lb: this.dialogForm.lb,
        kylx_fj: this.dialogForm.files,
        kylx_shr: this.dialogForm.shr.label,
        kylx_shr_user_id: this.dialogForm.shr.value
      };
      if (!this.isEdit) {
        kylx_add(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "科研立项新建成功！",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      } else {
        data.kylx_id = this.dialogForm.kylx_id;
        kylx_edit(data).then((res) => {
          if (res.code == 1) {
            this.$message({
              message: "科研立项编辑成功！",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getListData();
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
      }
    },
    delData(item) {
      this.$confirm("确定要删除该申请?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            let data = {
              kylx_id: item.kylx_id,
            };
            kylx_del(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "申请删除成功！",
                  type: "success",
                });
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          })
          .catch(() => {
          });
    },
    verify(type, item) {
      let data = {
        kylx_zt: type,
        kylx_id: item.kylx_id,
      };
      kylx_verify(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "审核成功！",
            type: "success",
          });
          this.getListData();
        }
      });
    },
    submitData(item) {
      let data = {
        kylx_zt: 2,
        kylx_id: item.kylx_id,
      };
      kylx_verify(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "提交审核成功！",
            type: "success",
          });
          this.getListData();
        }
      });
    },
  },
  mounted() {
    this.getUserList();
    this.getkylbList();
    this.getListData();
  },
};
</script>

<style scoped>
</style>
