<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>任务详情</span>
      </div>
      <div style="margin-left: 10px; margin-right: 10px">
        <el-row :gutter="20">
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">任务名称</span>
              <span class="desc"> {{ task.name }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">截止日期</span>
              <span class="desc"> {{ task.end_date }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">创建人</span>
              <span class="desc" v-if="task.create_by_">
                {{ task.create_by_.name }}/</span
              >
              <span class="desc" v-if="task.belong_dept_">{{
                task.belong_dept_.name
              }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">任务状态</span>
              <span class="desc">
                <el-tag
                  effect="plain"
                  v-if="task.state == '创建中'"
                  type="warning"
                  >{{ task.state }}</el-tag
                >
                <el-tag
                  effect="plain"
                  v-else-if="task.state == '执行中'"
                  type="primary"
                  >{{ task.state }}</el-tag
                >
                <el-tag effect="plain" v-else type="success">{{
                  task.state
                }}</el-tag>
              </span>
            </div>
          </el-col>
          <!-- <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">报送/确认率</span>
              <span class="desc">
                {{ task.up_rate }}% -- {{ task.confirm_rate }}%</span
              >
            </div>
          </el-col> -->
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">备注</span>
              <span class="desc"> {{ task.note }}</span>
            </div>
          </el-col>
        </el-row>
        <div style="margin-top: 4px">
          <el-button
            type="primary"
            @click="init()"
            v-if="task.state == '创建中'"
            size="small"
            >初始化任务</el-button
          >
          <el-button
            type="primary"
            @click="append()"
            v-if="task.state == '执行中' || task.state == '待发布'"
            size="small"
            >追加任务</el-button
          >
          <el-button
            type="primary"
            @click="starttask()"
            v-if="task.state == '待发布'"
            size="small"
            >发布任务</el-button
          >
        </div>
      </div>
    </el-card>

    <el-card style="margin-top: 2px">
      <div slot="header" class="clearfix">
        <span>执行组织</span>
      </div>
      <el-table
        v-loading="listLoading"
        :data="taskdeptall"
        border
        fit
        stripe
        highlight-current-row
        max-height="300px"
        @row-click="clickRow"
      >
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column label="公司名称">
          <template slot-scope="scope" v-if="scope.row.dept_">
            <span style="color:darkblue">{{scope.row.dept_.sort}}</span>-
            {{
            scope.row.dept_.name
          }}
          
          </template>
        </el-table-column>
        <el-table-column label="报送率">
          <template slot-scope="scope"> {{ scope.row.up_rate }}% </template>
        </el-table-column>
        <el-table-column label="确认率">
          <template slot-scope="scope">
            {{ scope.row.confirm_rate }}%
          </template>
        </el-table-column>
         <el-table-column label="是否反馈">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_yes" effect="plain">是</el-tag>
          <el-tag type="danger" effect="plain" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="240px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link
              v-if="
                scope.row.confirm_rate != 100  &&
                checkPermission(['record_confirm'])
              "
              type="warning"
              size="small"
              @click="handleConfirmDept(scope)"
              >确认</el-link
            >
             <el-link
            v-if="
             scope.row.confirm_rate == 100  &&
                checkPermission(['task_update'])
              "
             type="primary"
              size="small"
              @click="handleConfirmFK(scope)"
              >反馈</el-link
            >
            <el-link
              v-if="
                checkPermission(['task_update'])
              "
              type="danger"
              size="small"
              @click="handleDeleteDept(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
      :visible.sync="dialogVisibles"    
     
    >
      <el-form
        ref="Forms"
        :model="Taskdept"
        label-width="80px"
        label-position="right"
      >
        
        <el-form-item label="反馈文件" prop="template" v-if="dialogVisibles">
          <el-upload
          ref="upload"
          :action="upUrl"
          :on-preview="handlePreview"
          :on-success="handleUpSuccess"
          :on-remove="handleRemove"
          :headers="upHeaders"
          :file-list="fileList"
          :limit="1"
          accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx"
        >
          <el-button size="small" type="primary">上传文件</el-button>
        </el-upload>
        </el-form-item>
      
      
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisibles = false">取消</el-button>
        <el-button type="primary" @click="confirmfk('Forms')">确认</el-button>
      </div>
    </el-dialog>
    </el-card>
    <el-card style="margin-top: 2px">
      <div slot="header" class="clearfix">
        <span>执行详情</span>
      </div>
      <div>
        <el-button type="primary" @click="handleUp2" size="small" v-if="checkPermission(['task_update'])">批量修改备注</el-button>
      </div>
      <el-table
        v-loading="listLoading2"
        :data="recordList"
        border
        fit
        stripe
        highlight-current-row
        max-height="400px"
        style="margin-top:2px"
        @selection-change="handleSelectRecords"
      >
        <el-table-column type="selection" align="center" width="55" />
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column label="材料">
          <template slot-scope="scope">{{ scope.row.content_name }}</template>
        </el-table-column>
        <el-table-column
          label="材料详情"
        >
          <template slot-scope="scope">{{ scope.row.content_desc }}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.state == '待报送'">{{
              scope.row.state
            }}</el-tag>
            <el-tag
              type="warning"
              v-else-if="
                scope.row.state == '待整改' || scope.row.state == '待发布'
              "
              >{{ scope.row.state }}</el-tag
            >
            <el-tag type="success" v-else-if="scope.row.state == '已确认'">{{
              scope.row.state
            }}</el-tag>
            <el-tag v-else-if="scope.row.state == '已报送'">{{
              scope.row.state
            }}</el-tag>
          </template>
        </el-table-column>
         <el-table-column label="二级单位确认">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.dept_yes" effect="plain">已确认</el-tag>
          <el-tag type="danger" effect="plain" v-else>未确认</el-tag>
          </template>
      </el-table-column>
        <el-table-column label="报送要求/备注">
          <template slot-scope="scope">{{ scope.row.note }}</template>
        </el-table-column>
        <el-table-column label="是否适用">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_yes" effect="plain">是</el-tag>
            <el-tag type="danger" effect="plain" v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="报送情况">
          <template slot-scope="scope" v-if="scope.row.up_user_"
            >{{ scope.row.up_user_.name }}/{{ scope.row.up_date }}</template
          >
        </el-table-column>
        <el-table-column label="报送说明">
          <template slot-scope="scope" >{{ scope.row.noteb}}</template
          >
        </el-table-column>
        <el-table-column label="文件">
          <template slot-scope="scope" v-if="scope.row.files">
            <el-link
              v-if="scope.row.files.length > 1"
              @click="handleRecord({ action: 'view', record: scope.row })"
              >有
              <span style="color: red">{{ scope.row.files.length }}</span>
              个文件</el-link
            >
            <div v-else 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
          align="center"
          label="操作"
          width="240px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link
              v-if="
                (scope.row.state == '待报送' || scope.row.state == '待发布') &&
                checkPermission(['record_update'])
              "
              type="warning"
              size="small"
              @click="handleRecord({ action: 'update', record: scope.row })"
              >编辑</el-link
            >
            <el-link
              v-if="
                (scope.row.state == '待报送' || scope.row.state == '待整改') &&
                checkPermission(['record_up'])
              "
              type="primary"
              size="small"
              @click="handleRecord({ action: 'up', record: scope.row })"
              >报送</el-link
            >
            <el-link
              v-if="
                scope.row.state != '已确认' &&
                checkPermission(['record_confirm'])
              "
              type="success"
              size="small"
              @click="handleRecord({ action: 'confirm', record: scope.row })"
              >确认</el-link
            >
            <el-link
              v-if="
                scope.row.state == '已报送' &&
                checkPermission(['record_reject'])
              "
              type="danger"
              size="small"
              @click="handleRecord({ action: 'reject', record: scope.row })"
              >驳回</el-link
            >
            <el-link
              v-if="checkPermission(['record_view'])"
              size="small"
              @click="handleRecord({ action: 'view', record: scope.row })"
              >查看</el-link
            >
            <el-link
              v-if="checkPermission(['record_delete'])"
              type="danger"
              size="small"
              @click="handleRecord({ action: 'delete', record: scope.row })"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dgaction.name"
      :close-on-click-modal="false"
      width="70%"
    >
      <taskinit ref="taskinit" @handleChose="chooseComplete" ></taskinit>
    </el-dialog>
    <el-drawer
      title="记录"
      :visible.sync="drawer"
      :with-header="false"
      size="40%"
    >
      <recorddo
        ref="recorddo"
        :data="data"
        @handleDo="handleDo"
        v-if="drawer"
      ></recorddo>
    </el-drawer>
  </div>
</template>
<style >
.term {
  color: rgba(0, 0, 0, 0.85);
  font-weight: bold;
  font-size: 16px;
}
.desc {
  color: rgba(0, 0, 0, 0.65);
  font-weight: bold;
  font-size: 16px;
}
.litem {
  margin-bottom: 4px;
  margin-left: 2px;
  cursor: pointer;
  color: #409eff;
}
</style>
<script>
import { gettask, inittask, gettaskdeptall, starttask, appendtask, confirmTaskdept,updateTaskdept, deleteTaskdept } from "@/api/task";
import { getRecordList, updateRecords } from "@/api/record";
import Pagination from "@/components/Pagination";
import taskinit from "@/views/supervision/taskinit";
import checkPermission from "@/utils/permission";
import recorddo from "@/views/supervision/recorddo";
import { upUrl, upHeaders } from "@/api/file";
export default {
  components: { Pagination, taskinit, recorddo },
  data() {
    return {
      task: { id: 0 },
      dialogVisible: false,
       dialogVisibles: false,
        dialogType: "new",
      activeName: "contenttab",
      contents: [],
      depts: [],
      listLoading: false,
      listLoading2: false,
      recordList: [],
      nowcontent: {},
      nowdept: {},
      drawer: false,
      data: {},
      taskdeptall: [],
      listQuery:{},
      selectRecords:[],
      dgaction:{
        action:'init',
        name:'初始化任务'
      }
      ,
        upHeaders: upHeaders(),
      upUrl: upUrl(),
      fileList:[],
      Taskdept:{is_yes:true},
    };
  },
  created() {
    this.task.id = this.$route.params.id;
    this.gettask();
    this.gettaskdeptall();
  },
  methods: {
    checkPermission,
    gettask() {
      gettask(this.task.id).then((res) => {
        this.task = res.data;
        if (this.task.state == "创建中") {
          this.dialogVisible = true;
        }
      });
    },
    gettaskdeptall() {
      this.listLoading = true;
      gettaskdeptall(this.task.id)
        .then((res) => {
          this.listLoading = false;
          this.taskdeptall = res.data;
        })
        .catch((e) => {
          this.listLoading = false;
        });
    },
    init() {
      this.dgaction ={
        action:'init',
        name:'初始化任务'
      }
      this.dialogVisible = true;
    },
    append() {
      this.dgaction ={
        action:'append',
        name:'追加任务'
      }
      this.dialogVisible = true;
    },
    chooseComplete(data) {
      this.dialogVisible = false;
      if(this.dgaction.action == 'init'){
      const rLoading = this.openLoading("正在初始化任务,请稍等...");
      inittask(this.task.id, data).then((res) => {
        rLoading.close();
        this.$message.success("成功");
        this.$router.go(0);
      }).catch(e=>{rLoading.close();});
      }else if(this.dgaction.action == 'append'){
        const rLoading = this.openLoading("正在追加任务,请稍等...");
      appendtask(this.task.id, data).then((res) => {
        rLoading.close();
        this.$message.success("成功");
        this.$router.go(0);
      }).catch(e=>{rLoading.close();});
      }
      
    },
    starttask() {
      this.$confirm("确认发布任务吗?", "提示")
        .then(async () => {
          await starttask(this.task.id);
          location.reload();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    handleRecord(data) {
      this.data = data;
      this.drawer = true;
    },
    handleDo(data) {
      this.drawer = false;
      this.gettask();
      this.gettaskdeptall();
      this.getRecordList();
    },
    getRecordList() {
      getRecordList(this.listQuery)
        .then((res) => {
          this.listLoading2 = false;
          this.recordList = res.data;
        })
        .catch((e) => {
          this.listLoading2 = false;
        });
    },
    clickRow(row, col, e) {
      this.listLoading2 = true;
      this.listQuery = { pageoff: true, task: row.task, belong_dept: row.dept }
      this.getRecordList();
    },
    handleSelectRecords(val){
      let selects = [];
      for (var i = 0; i < val.length; i++) {
        selects.push(val[i].id);
      }
      this.selectRecords = selects;
    },
    handleConfirmDept(scope){
      confirmTaskdept(scope.row.id).then(res=>{
        this.getRecordList()
        this.gettaskdeptall()
      })
    },
    //反馈意见
     handlePreview(file) {
      if ("url" in file) {
        window.open(file.url);
      } else {
        window.open(file.response.data.path);
      }
    },
    handleUpSuccess(res, file, filelist) {
      this.Taskdept.file = res.data.path;
       this.filename = res.data.name;
    },
    handleRemove(file, filelist){
      this.Taskdept.file = null;
    },
    handleConfirmFK(scope)
    {
        this.Taskdept = Object.assign({}, scope.row); // copy obj
     
      this.dialogVisibles = true;
      if (this.Taskdept.file) {
        this.fileList = [
          {
            name:"反馈文件",
            url: this.Taskdept.file,
          },
        ];
      }
      this.$nextTick(() => {
        this.$refs["Forms"].clearValidate();
      });
      
    },

   async confirmfk(form) {
     
           this.$refs[form].validate((valid) => {
       
            console.log(this.Taskdept)
            this.Taskdept.is_yes=true;
              updateTaskdept(this.Taskdept.id,this.Taskdept).then((res) => {
              if (res.code >= 200) {
                  this.gettaskdeptall()
                this.dialogVisibles = false;
                this.$message.success("成功");
              }
            });
         
       
      });
      
    },


    handleDeleteDept(scope){
      deleteTaskdept(scope.row.id).then(res=>{
        this.gettaskdeptall()
      })
    },
    handleUp2(){
      if (this.selectRecords.length) {
        this.$prompt('请输入备注内容', '提示').then(({ value }) => {
          updateRecords({note:value, ids:this.selectRecords}).then(res=>{
            this.$message.success('成功')
            this.getRecordList();
          })
        }).catch(() => {      
        });
      } else {
        this.$message({
          message: "请先选择",
          type: "warning",
        });
      }
    }
  },
};
</script>