<template>
  <div class="workbench-box">
    <header>
      <div class="workbench-serch" v-if="userType == '02'">
        <div class="workbench-title">
          <div class="title-item" :class="checked == index ? 'checked-item' : ''" v-for="(item,index) in tabs" :key="index" @click="changeTab(index)">
            {{ item.value }}
          </div>
          <!-- <div class="title-item">
            查项目
          </div>
          <div class="title-item">
            查荣誉
          </div> -->
        </div>
        <div class="workbench-btn">
          <el-autocomplete 
            placeholder="请输入企业名称、项目名称、荣誉，查询" 
            v-model="search" 
            :fetch-suggestions="querySearch" 
            :trigger-on-focus="false" 
            @select="handleSelect"
            class="input-with-select"
          >
            <template slot="append" class="work-btn">搜索</template>
          </el-autocomplete>
        </div>
      </div>
    </header>
    <main>
      <el-row class="main-row">
        <el-col :span="16" class="work-main-left">
          <div class="main-left">
            
          </div>
        </el-col>
        <el-col :span="8" class="work-main-right">
          <div class="main-right">
            <div class="right-header">
              <div class="title">【待办提醒】</div>
              <el-button @click="MoreRecords" type="text" v-if="backlogArray.length > 0">>> 更多记录</el-button>
            </div>
            <div class="right-main" v-if="backlogArray.length > 0" v-loading="loading">
              <div class="main-items" v-for="(item,index) in backlogArray" :key="index" :class="index % 2 == 0 ? '':'main-item'">
                <div>{{ item.matter || item.enterpriseDirectory}}</div>
                <div>{{ item.enterpriseName }}</div>
                <!-- <div>审批【知识产权】材料</div> -->
                <div @click="MoreRecords">{{ userType == '02' ? '去审批' : userType == '01' ? '去填报' : '' }}</div>
              </div>
            </div>
            <div class="right-main" v-else>
              <el-divider content-position="center">暂未待审核</el-divider>
            </div>
          </div>
        </el-col>
      </el-row>
    </main>
    <!-- 申报审核对话框 -->
    <!-- <el-dialog title="申报审核" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="left">
        <el-row>
          <el-col :span="24">
            <el-form-item label="审批状态" prop="approvalStatus">
              <el-radio-group v-model="form.approvalStatus">
                <el-radio label="1">审批通过</el-radio>
                <el-radio label="2">审批驳回</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审批意见" prop="approvalOpinions">
              <el-input v-model="form.approvalOpinions" type="textarea" placeholder="请输入审批意见" maxlength="200"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="审批附件" prop="fileList">
              <el-upload
                class="upload-demo"
                :show-file-list="false"
                action=""
                accept=".xlsx,.XLSX,.XSL,.doc,.xls,.ppt,.pdf,.png,.jpg,.jpeg,.rar,.zip,.docx"
                :http-request="handleFileUpload"
                :file-list="fileList"
                >
                <el-button slot="trigger" size="small" type="success">上传文件</el-button>
                <div slot="tip" class="el-upload__tip">请上传格式为<span style="color: red;">doc/xls/ppt/pdf/png/jpg/jpeg/rar/zip/docx/xlsx</span>的文件</div>
              </el-upload>
              <ul v-if="fileList.length >0" class="upload_ul">
                <li v-for="item in uploadList" :key="item.url" class="upload_li">
                  <img src="@/assets/images/uploadBgc.jpg" class="li_img" alt="">
                  <span class="icon_name">{{ item.name }}</span>
                  <el-button type="text" style="color: red;" @click="deleteUpload(item)">删除</el-button>
                </li>
              </ul>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>
<script>
import { approvalList, approvalEdit, uploadFile } from "@/api/onlineDeclartion/pendingReview"
import { listRecords } from "@/api/onlineDeclartion/records"
import { selectAll, workSearch } from "@/api/jin_ji_hu/home"
export default {
  data() {
    return {
      // 判断登陆用户是何身份⌈ 01 ： 企业  02 ： 政务 ⌋
      userType: this.$store.state.user.userType,
      search:"",
      backlogArray:[],
      pagination: {
        pageNum:1,
        pageSize:10,
        approvalStatusStr: 0,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        creditCode: this.$store.state.user.name,
        statusStr: [0, 10],
      },
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        approvalOpinions: ''
      },
      // 表单校验
      rules: {
        approvalStatus: [
          { required: true, message: "请选择审批状态", trigger: "blur" }
        ],
        approvalOpinions: [
          { required: true, message: "名录维护方式不能为空", trigger: "blur" }
        ],
      },
      fileList: [],
      uploadList: [],
      tabs: [
        {
          value:"查企业"
        },{
          value:"查项目"
        },{
          value:"查荣誉"
        },
      ],
      checked: 0,
      loading:false,
    }
  },
  mounted(){
    if(this.userType == "02") {
      this.getList();
    } else if(this.userType == "01") {
      this.getListTwo();
    }
    
  },
  methods:{
    // 待审批
    getList(){
      this.loading = true;
      approvalList(this.pagination).then(res=>{
        // console.log(res);
        this.backlogArray = res.rows
        this.loading = false;
      })
    },
    // 待填报
    getListTwo(){
      this.loading = true;
      listRecords(this.queryParams).then(response => {
        this.backlogArray = response.rows
        this.loading = false;
      });
    },
    // 更多记录
    MoreRecords(){
      if(this.userType == "02") {
        this.$router.push({
          name: 'PendingReview',
          params: { approvalStatusStr: "0" }
        })
      } else if(this.userType == "01") {
        this.$router.push({
          name: 'Records',
          params: { statusChange: "0" }
        })
      }
    },
    // 切换查询tab
    changeTab(index){
      this.checked = index;
      this.search = "";
    },
    // 审批弹框
    handleApproval(row) {
      this.reset();
      this.form.declarationRecordsId = row.declarationRecordsId
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        approvalOpinions: '',
        approvalStatus: '1',
        approvalAttachment: '',
      };
      this.resetForm("form");
      this.uploadList = []
    },
    querySearch(queryString, cb) {
      let results = []
      if(this.checked == 0) {
        workSearch({enterpriseInfo: queryString}).then(res => {
          console.log(res,"res");
        })
      } else if(this.checked == 1) {
        selectAll({ name: queryString }).then(res => {
          res.data.forEach(item => {
            results.push({
              value: item.projectName,
              id: item.id
            })
          })
        })
      } else if(this.checked == 2) {
        
      }
      // queryString 拿到搜索到的数据
      /**
       * 通过queryString 调用接口参数搜索到的数据【数据处理成所需的列表⌈ 对象带value ⌋】使用callback返回列表数据
       */
      // var restaurants = this.restaurants;
      // var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    // 点击搜索到的数据
    handleSelect(item) {
      console.log(item);
    },
    handleFileUpload(params) {
      const file = params.file
      let form = new FormData()
      form.append('file', file)
      uploadFile(form).then(response => {
        this.fileList = []
        this.fileList.push(file)
        this.form.fileList = this.fileList
        let data = {}
        data.name = response.originalFilename;
        data.url = response.fileName;
        this.uploadList.push(data)
        // console.log('this.fileList: ', this.fileList);
      })
    },
    deleteUpload(value) {
      this.uploadList =  this.uploadList.filter(item => {
        return item.url != value.url
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.uploadList.forEach((item,index) => {
            if(this.uploadList.length-1 == index){
              this.form.approvalAttachment+=`${item.url}`
            } else {
              this.form.approvalAttachment+=`${item.url},`
            }
          })
          // console.log("sdas", this.form);
          approvalEdit(this.form).then(response => {
            this.$modal.msgSuccess("审核完成");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
}
</script>
<style lang="scss" scoped>

</style>
