<template>
  <div class="container-accountability scoringWeightListPage">
	<el-form :model="formInline" :inline="true"  ref="formInline" :rules="rule">
	  <div style="height: 40px;font-size: 18px;font-weight: 600;">
	  	<span>评分表名称：{{formInline.pfbmc}}</span>
	  	<span style="float: right;margin-right: 10px;">考核年度：{{formInline.khnd}}</span>
	  </div>
    <el-row>
      <el-col :span="24">
        <el-table
		 height="420"
          :data="tableList"
          v-loading="loading"
          border
          stripe
          highlight-current-row
        >
          <el-table-column align="center" label="序号" width="60">
            <template slot-scope="scope">
              <span>{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="评价单位" :show-overflow-tooltip="true">
            <template slot-scope="scope">
				<!-- <el-button type="text">{{scope.row.qymc}}</el-button> -->
              <span>{{scope.row.qymc}}</span>
            </template>
          </el-table-column>
		  <el-table-column align="center" label="企业自评" :show-overflow-tooltip="true">
		    <template slot-scope="scope">
		  	  <el-button type="text" @click="CkqyzpbClick(scope.row)">{{scope.row.qyzp}}</el-button>
		      <!-- <span>{{scope.row.year}}</span> -->
		    </template>
		  </el-table-column>
		  <el-table-column align="center" label="区粮办评价" :show-overflow-tooltip="true">
		    <template slot-scope="scope">
		      <span>{{scope.row.qjpf}}</span>
		    </template>
		  </el-table-column>
		  <el-table-column align="center" label="市储备公司评分" :show-overflow-tooltip="true">
		    <template slot-scope="scope">
		  	  <el-input v-if="$route.query.type == 'XG'" v-model="scope.row.cbgspf" type="text"></el-input>
		      <span v-else>{{scope.row.cbgspf}}</span>
		    </template>
		  </el-table-column>
		  <el-table-column align="center" label="考评结果" :show-overflow-tooltip="true">
		    <template slot-scope="scope">
			  <el-select
			   v-if="$route.query.type == 'XG'" 
			    placeholder="请选择"
			    v-model="scope.row.kpjg"
			    clearable>
			    <el-option
			      v-for="item in kpjgOptions"
			      :key="item.name"
			      :label="item.name"
			      :value="item.name"
			      ></el-option>
			  </el-select>
		      <span v-else>{{scope.row.kpjg}}</span>
		    </template>
		  </el-table-column>
        </el-table>
      </el-col>
	  <el-col v-if="$route.query.type == 'HZ'" style="margin-top: 30px;">
		  <el-form-item label="评分报告:" prop=''>
		  <el-upload
		  v-model="formInline.filename"
		    v-if="$route.query.type == 'HZ'"
		    :on-success="handleSuccessJJ"
		    class="upload-demo"
		    :action="fileUrl"
		    :on-preview="handlePreviewJJ"
		    :on-remove="handleRemoveJJ"
		    :before-remove="beforeRemoveJJ"
		    :file-list="fileListBL"
		  >
		    <el-button
		  	  v-show="$route.query.type == 'HZ'"
		      type="primary"
		      size="small"
		      class="iconfont icon"
		      >上传</el-button
		    >
		  </el-upload>
		 <!-- <div v-if="$route.query.type == 'CK' && " v-for="item in fileListBL" :key="item.id">
		    <span>{{ item.name }}</span>
		    <el-button type="text" size="small" @click="XzPic(item.id)"
		      >下载</el-button
		    >
		  </div> -->
		  </el-form-item>
	  </el-col>
	 <el-col style="text-align: center;margin-top: 20px;">
		  <span >
		  	<el-button
			v-if="$route.query.type == 'XG'"
			type="primary"
			size="small"
			class="iconfont icontijiao"
			@click="saveClick('bc')"
			>保存</el-button>
			<el-button
			v-if="$route.query.type == 'HZ'"
			type="primary"
			size="small"
			class="iconfont icontijiao"
			@click="saveClick('hz')"
			>汇总</el-button>
		  	<el-button
			type="primary"
			size="small"
			class="iconfont iconfanhui"
			@click="returnList"
			>返回</el-button>
		  </span> 
	  </el-col>
    </el-row>
	</el-form>
  </div>
</template>
<script>
import {getKhqybList,updateKhqybDetailBatchForSj,createHzb} from "@/api/evaluationMan/index";
import { files } from "@/api/index";
export default {
  data() {
    return {
		 fileUrl: `${files}/file/upload`,
		 fileListBL:[],
		kpjgOptions:[
			{
				name:'优秀',
				value:'优秀'
			},
			{
				name:'良好',
				value:'良好'
			},
			{
				name:'合格',
				value:'合格'
			},
			{
				name:'不合格',
				value:'不合格'
			}
		],
		
      formInline: {
        pfbmc:'',
        khnd:'',
      },
      dateyear: [],
      tableList: [],
      loading: true,
      listLoading: false,
	  rule:{
		filename: [
		  {
		    required: true,
		    message: "评估报告不能为空",
		    trigger: ["blur", "change"],
		  },
		],  
	  },
    };
  },
  methods: {
	  //查看企业自评表
	  CkqyzpbClick(row){
		this.$router.push({
		    path:"/accountability/component/qyzpb",
		    query:{
		        id:row.id,
				orgCode:row.orgCode,
				khnd:this.formInline.khnd,
				pfbmc:this.formInline.pfbmc,
		    }
		})
	  },
	  //保存
	  saveClick(state){
		  if(state == 'bc'){
			this.tableList.forEach(item =>{
			  item.cbgspjrq=this.dateFormat(new Date())
			  item.cbgspjr=sessionStorage.getItem('userName')
			})
			updateKhqybDetailBatchForSj(this.tableList).then(res =>{
			  if(res.infoCode == 200){
				this.$router.push({
				   path:"/accountability/cityEvaluation/index",
				})  
			  }else{
				this.$message({
				  type: 'error',
				  duration: 2000,
				  message: res.msg
				}) 
			  }
			}) 
		  }else{
			  var data={
				  ...this.formInline
			  }
			  let arrBL=[]
			  this.fileListBL.map((item) => {
			    arrBL.push(item.response ? item.response.data.id : item.id);
			  });
			  data.pfbgfjid = arrBL.join();
			  this.formInline.filename= arrBL.join()
			console.log(data,this.formInline.filename,'hhhfhf---------')
			this.$refs['formInline'].validate((valid) => {
			  if (valid) {
				  
				  data.khzbid=this.$route.query.id
				  data.khzmc=data.pfbmc
				createHzb(data).then(res =>{
					if(res.infoCode == 200){
						this.$router.push({
						   path:"/accountability/cityEvaluation/index",
						})  
						this.$message({
						  type: 'success',
						  duration: 2000,
						  message: res.msg
						}) 
					}else{
						this.$message({
						  type: 'error',
						  duration: 2000,
						  message: res.msg
						}) 
					}
				})			
			}  
		  })
		  
		}
	  },
	  //返回
	  returnList(){
		 this.$router.push({
		    path:"/accountability/cityEvaluation/index",
		 }) 
	  },
	  //追加附件上传
	  handleSuccessJJ(res, file, fileList) {
	    this.fileListBL = fileList;
	  },
	  handleRemoveJJ(file, fileList) {
	    this.fileListBL = fileList;
	  },
	  handlePreviewJJ(file) {
	    // console.log(file);
	    let fileLink = file.response ? file.response.data.uri : file.link;
	    if (
	      ["png", "jpg", "jpeg", "bmp", "pdf"].includes(
	        fileLink.split(".")[fileLink.split(".").length - 1]
	      )
	    ) {
	      // console.log(`${window.location.origin}/file/${fileLink}`);
	      window.open(`${window.location.origin}/file/${fileLink}`);
	    } else {
	      window.location.href = `${FileApi}/file/download?id=${
	        file.response ? file.response.data.id : file.id
	      }`;
	    }
	  },
	  beforeRemoveJJ(file, fileList) {
	    return this.$confirm(`确定移除 ${file.name}？`);
	  },
	  
	  //预览
	  // YlPic(link) {
	  //   window.open(`${window.location.origin}/file/${link}`);
	  // },
	  //下载
	  XzPic(id) {
	    window.location.href = `${files}/file/download?id=${id}`;
	  },
   //获取详情
    getTabList(type) {
	  this.loading = false;
	  var data={
		  id:this.$route.query.id,
		  pageType:'5'
	  }
      getKhqybList(data)
        .then(res => {
            this.loading = false;
            if (res.infoCode==200) {
                this.tableList=res.data
            } else {
              this.$message({
                type: 'error',
                duration: 2000,
                message: res.msg
              })
            }
        });
    },
	dateFormat(date) {
	  // let date = new Date();
	  let year = date.getFullYear();
	  /* 在日期格式中，月份是从0开始的，因此要加0
	   * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
	   * */
	  let month =
	    date.getMonth() + 1 < 10
	      ? "0" + (date.getMonth() + 1)
	      : date.getMonth() + 1;
	  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
	  let hours =
	    date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
	  let minutes =
	    date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
	  let seconds =
	    date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	  // 拼接
	  return year + "-" + month + "-" + day + ' ' + hours + ':' + minutes + ':' +seconds;
	},
  },
  created() {
    let date = new Date();
    this.year = date.getFullYear();
    for (var i = 0; i < 10; i++) {
      this.dateyear.push(this.year);
      this.year = this.year - 1;
    }
	this.formInline.pfbmc=this.$route.query.pfbmc
	this.formInline.khnd=this.$route.query.khnd
    // this.formInline.year= date.getFullYear()
    this.getTabList('start')
    

  }
};
</script>
<style lang="scss">
.scoringWeightListPage {
  .el-select {
    width: 100%;
  }
  .titleUseStyle{
    // position: absolute;
    // top: 0;
    font-size: 14px;
    width: 100%;
    height: 30px;
    line-height: 30px;
    color: #fff;
    font-weight: 400;
    background: #3d62c1 !important;
    padding-left: 8px;
  }
}
</style>
