<template>
  <div class="app-container">
    <el-row :gutter="6">
      <el-col :span="11">
            <el-card>
     
    
      <el-table
      style="margin-top:4px"
      
        :data="contentOptions"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
        @row-click="recorclickRow"
      >
        <el-table-column type="index" width="50" />
         <el-table-column align="center" label="清单名称">
          <template slot-scope="scope"  >
              {{ scope.row.name }}</template>
        </el-table-column>
          <el-table-column align="center" label="详情">
          <template slot-scope="scope"  >
              {{ scope.row.desc }}</template>
        </el-table-column>
        <el-table-column align="center" label="材料类型">
          <template slot-scope="scope"  >
              {{ scope.row.type_ }}</template>
        </el-table-column>
        
        
         <el-table-column align="center" label="上报模板">
          <template slot-scope="scope" v-if="scope.row.template">
            <el-link :href="scope.row.template" type="primary">下载</el-link>
          </template>
        </el-table-column>
      <el-table-column label="操作" align="center">
      <template slot-scope="scope">
        <el-button
          size="mini"
          type="primary"
          @click="handleAddrecordTable(scope)">上报</el-button>
       
      </template>
    </el-table-column>
      </el-table>
    </el-card>
        </el-col>
        <el-col :span="13">
          <el-card >
      
   
      <el-table
      style="margin-top:4px"
        :data="TableList.results"
         border
        
        stripe
        max-height="700"
      >
        <el-table-column type="index" width="50" />
         <el-table-column align="center" label="上报说明">
            <template slot-scope="scope"  v-if="scope.row.noteb">
             {{ scope.row.noteb }}
        </template>
        </el-table-column>
         <el-table-column label="报送文件">
        <template slot-scope="scope" v-if="scope.row.files">
         
          <div v-for="item in scope.row.files_" v-bind:key="item.id">
            <el-link :href="item.path" target="_blank" type="primary">{{
              item.name
            }}</el-link>
          </div>
        </template>
      </el-table-column>
       <el-table-column
          label="报送单位"
          sortable="custom"
          prop="belong_dept__sort"
        >
          <template slot-scope="scope" v-if="scope.row.belong_dept">{{
            scope.row.belong_dept_.name
          }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="200px"
          fixed="right"
        >
          <template slot-scope="scope">
           
            <el-link
              v-if="
                (scope.row.state == '已报送' || scope.row.state == '待整改') &&
                checkPermission(['record_up'])
              "
              type="primary"
              size="small"
              @click="handleEdit(scope)"
              >重报</el-link
            >
            <el-link
              v-if="
                scope.row.state == '已报送' &&
                checkPermission(['record_confirm'])
              "
              type="success"
              size="small"
              @click="handleRecord(scope)"
              >确认</el-link
            >
            <el-link
              v-if="
                scope.row.state == '已报送' &&
                checkPermission(['record_reject']) &&
                scope.row.belong_dept != $store.state.user.dept
              "
              type="danger"
              size="small"
              @click="handleRecord1(scope)"
              >驳回</el-link
            >
            <el-link
             
              type="danger"
              size="small"
              @click="handleRecord2(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
       <pagination
        v-show="TableList.count > 0"
        :total="TableList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getrecordlist"
      />
    </el-card>
          </el-col>
      </el-row>

      
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑上报' : '新增上报'"
    >
      <el-form
        ref="Form"
        :model="recordTable"
        label-width="80px"
        label-position="right"
       
      >
       
        <el-form-item label="上报说明" prop="noteb">
          <el-input
            type="textarea"
            :rows="4"
            v-model="recordTable.noteb"
            placeholder="详情"
          />
        </el-form-item>
       <el-form-item label="文件列表">
                <el-upload
               
          ref="upload"
          :action="upUrl"
          :before-upload="beforeUpload"
          :on-success="handleUpSuccess"
          :headers="upHeaders"
          multiple
          accept="image/*,.ppt,.pdf,.doc,.docx,.xls,.xlsx"
        >
          <el-button size="small" type="primary">上传文件</el-button>
            <span class="el-upload__tip">
              可上传多个pdf,word,ppt,excel,图片文件,单文件大小不超过50M
            </span>
        </el-upload>
      </el-form-item>
      <div
        v-for="(item, index) in fileList"
        v-bind:key="item.id"
        style="margin-top: 2px;"
      >
        <i
          class="el-icon-delete"
          @click="deleteFile(index)"
          style="color: red"
        ></i>
        <el-link :href="item.path" target="_blank" type="primary">{{
          item.name
        }}</el-link>
      </div>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>


    <el-dialog
      :visible.sync="dialogVisibles"
     
    >
      <el-form
        ref="Forms"
        :model="recordTable"
        label-width="80px"
        label-position="right"
       
      >
       
        <el-form-item label="上报说明" prop="noteb">
          <el-input
            type="textarea"
            :rows="4"
            v-model="recordTable.noteb"
            placeholder="详情"
             :disabled="true"
          />
        </el-form-item>
        <el-form-item label="修改意见" prop="opinion">
      
        <el-input
          v-model="recordTable.opinion"
          placeholder=""
          type="textarea"
         
        />
      </el-form-item>
      <div
        v-for="(item, index) in fileList"
        v-bind:key="item.id"
        style="margin-top: 2px;"
      >
        <i
          class="el-icon-delete"
          @click="deleteFile(index)"
          style="color: red;display:none"
        ></i>
        <el-link :href="item.path" target="_blank" type="primary">{{
          item.name
        }}</el-link>
      </div>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisibles = false">取消</el-button>
        <el-button type="primary" @click="confirmbh('Forms')">驳回</el-button>
      </div>
    </el-dialog>
   
  </div>
</template>
<script>

import { getRecordList, createRecord, updateRecord,
  upRecord,
  rejectRecord,
  confirmRecord,
  deleteRecord, } from "@/api/ability";
import { getContentList } from "@/api/ability";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";
import { upUrl, upHeaders } from "@/api/file";
const defaultContent = {
  noteb:"",
  files:[],
};
export default {
  components: {Pagination },
  data() {
    return {
     
      contentOptions: [],
      listLoading: true,
      dialogVisible: false,
      dialogVisibles:false,
      upHeaders: upHeaders(),
      upUrl: upUrl(),
      fileList: [],
      recordTable:defaultContent,
      TableList:{count: 0},
      listQuery: {
        page: 1,
        page_size: 20,
      },
      listLoading: true,
      dialogType: "new",
     
      filterOrgText: "",
    };
  },
  computed: {},
  watch: {
    filterOrgText(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getLists();
    this.getrecordlist();
  },
  
  
  methods: {
    checkPermission,

   
    //材料清单列表
    getLists() {
      getContentList({ can_doself: true }).then((res) => {
        this.contentOptions = res.data;
      });
    
    },
    getrecordlist(){
      this.listQuery.is_self= true;
      getRecordList(this.listQuery).then((response) => {
        if (response.data) {
          this.TableList = response.data;
        }
      }); 
    },
    //点击材料清大，弹出该清单下对应报送任务
     recorclickRow(row) {
       this.listQuery.is_self= true;
      this.listQuery.content= row.id;
     
      
      getRecordList(this.listQuery).then((response) => {
        if (response.data) {
          this.TableList = response.data;
        }
      }); 
    },
   
 
  initList() {
     this.fileList=[];
      for (var i = 0; i < this.recordTable.files_.length; i++) {
        this.fileList.push({
          id: this.recordTable.files_[i].id,
          name: this.recordTable.files_[i].name,
          path: this.recordTable.files_[i].path,
        });
      }
    },

    
    handleUpSuccess(res, file, filelist) {
      this.fileList.push({
        id: res.data.id,
        name: res.data.name,
        path: res.data.path,
      });
      this.handleRemove(file);
    },
    handleRemove(file) {
      // 实现删除文件
      let fileList = this.$refs.upload.uploadFiles;
      let index = fileList.findIndex((fileItem) => {
        return fileItem.uid === file.uid;
      });
      fileList.splice(index, 1);
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 50;
      if (!isLt2M) {
        this.$message.error("单文件不能超过50MB!");
      }
      return isLt2M;
    },
    deleteFile(index) {
      this.$confirm("确定删除该文件, 是否继续?", { type: "error" })
        .then(() => {
          this.fileList.splice(index, 1);
        })
        .catch((e) => {});
    },
    yeschange(val) {
      if (!val) {
        this.fileList = [];
      }
    },
   
   
    

   
    handleAddrecordTable(scope) {
       this.content = scope.row.id;
      
      this.recordTable = Object.assign({}, defaultContent);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
      this.initList();

         
     
    },
    handleEdit(scope) {
      this.recordTable = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
       this.initList();
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteQualityinspect(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    handleRecord(scope){
      
        confirmRecord(scope.row.id).then((res) => {
          this.$message.success("成功");
        })
    }
 ,
 //驳回
   handleRecord1(scope){
        this.recordTable = Object.assign({}, scope.row); // copy obj
      this.dialogVisibles = true;
         this.initList();

    }
 ,
 //驳回提交
 confirmbh(forms){
    this.recordTable.content=this.content;
   rejectRecord(this.recordTable.id, this.recordTable).then((res) => {
          this.$message.success("成功");
          this.$emit("handleDo",true);
        });
         this.dialogVisibles = false;
                  this.getrecordlist()
 },
  handleRecord2(scope){
        this.$confirm("确认删除?", "警告", {
          type: "error",
        })
          .then(async () => {
            await deleteRecord(scope.row.id);
            this.$message.success("成功");
              this.getrecordlist()
          })
          .catch((err) => {
            console.error(err);
          });
       
    }
 ,
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
           
          if (isEdit) {
            var files = [];
        for (var i = 0; i < this.fileList.length; i++) {
          files.push(this.fileList[i].id);
        }
        this.recordTable.files = files;
             this.recordTable.content=this.content;
            upRecord(this.recordTable.id, this.recordTable).then((res) => {
              if (res.code >= 200) {
                this.dialogVisible = false;
                  this.getrecordlist()
                this.$message.success("成功");
              }
            });
          } else {
            this.recordTable.content=this.content;
            var files = [];
        for (var i = 0; i < this.fileList.length; i++) {
          files.push(this.fileList[i].id);
        }
        this.recordTable.files = files;
       
         
             createRecord(this.recordTable).then((res) => {
              if (res.code >= 200) {
                this.dialogVisible = false;
                  this.getrecordlist()
                this.$message.success("成功");
              }
            });
          }
          
        } else {
          return false;
        }
      });
    },
  },
};
</script>
