<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_belong_dept_">{{
                task.belong_belong_dept_.name
              }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">任务状态</span>
              <span class="desc"> {{ task.state }}</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
          >
        </div>
      </div>
    </el-card>

    <el-card style="margin-top: 2px">
      <div slot="header" class="clearfix">
        <span>执行详情</span>
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane label="按清单" name="contenttab" lazy>
          <el-row :gutter="10">
            <el-col :span="4">
              <el-card
                shadow="never"
                style="height: 600px; overflow-y: auto; overflow-x: auto"
              >
                <div
                  v-for="item in contents"
                  v-bind:key="item.id"
                  class="litem"
                  @click="chosecontent(item)"
                >
                  {{ item.name }}
                </div>
              </el-card>
            </el-col>
            <el-col :span="20">
              <el-card
                shadow="never"
                style="height: 600px; overflow-y: auto; overflow-x: auto"
              >
              <div style="margin-bottom:6px">
                当前选择 
                <span style="color:red">{{nowcontent.name}}</span>
              </div>
                <el-table
                  v-loading="listLoading"
                  :data="recordList1"
                  border
                  fit
                  stripe
                  highlight-current-row
                  max-height="600px"
                >
                  <el-table-column label="序号" type="index" align="center" />
                  <el-table-column label="单位">
                    <template slot-scope="scope">{{
                      scope.row.belong_dept_.name
                    }}</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 }}</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"
                      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"> </template>
                  </el-table-column>
                  <el-table-column align="center" label="操作" width="220px" fixed="right">
                    <template slot-scope="scope">
                      <el-button
                        v-if="
                          scope.row.state == '待上报' &&
                          checkPermission(['record_update'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'update', record:scope.row})"
                        >编辑</el-button
                      >
                      <el-button
                        v-if="
                          (scope.row.state == '待上报' || scope.row.state == '待整改')&&
                          checkPermission(['record_up'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'up', record:scope.row})"
                        >上报</el-button
                      >
                      <el-button
                        v-if="
                          scope.row.state == '已上报' &&
                          checkPermission(['record_confirm'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'confirm', record:scope.row})"
                        >确认</el-button
                      >
                      <el-button
                        v-if="
                          scope.row.state == '已上报' &&
                          checkPermission(['record_reject'])
                        "
                        type="danger"
                        size="small"
                        @click="handleRecord({action:'reject', record:scope.row})"
                        >驳回</el-button
                      >
                      <el-button
                        v-if="checkPermission(['record_view'])"
                        size="small"
                        @click="handleRecord({action:'view', record:scope.row})"
                        >查看</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="按单位" name="depttab" lazy>
          <el-row :gutter="10">
            <el-col :span="4">
              <el-card
                shadow="never"
                style="height: 600px; overflow-y: auto; overflow-x: auto"
              >
                <div
                  v-for="item in depts"
                  v-bind:key="item.id"
                  class="litem"
                  @click="chosedept(item)"
                >
                  {{ item.name }}
                </div>
              </el-card>
            </el-col>
            <el-col :span="20">
              <el-card
                shadow="never"
                style="height: 600px; overflow-y: auto; overflow-x: auto"
              >
              <div style="margin-bottom:6px">
                当前选择 
                <span style="color:red">{{nowdept.name}}</span>
              </div>
                <el-table
                  v-loading="listLoading"
                  :data="recordList2"
                  border
                  fit
                  stripe
                  highlight-current-row
                  max-height="600px"
                >
                  <el-table-column label="序号" type="index" align="center" />
                  <el-table-column label="材料">
                    <template slot-scope="scope">{{
                      scope.row.content_.name
                    }}</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 }}</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"
                      v-if="scope.row.up_user_">{{ scope.row.up_user }}/{{ scope.row.up_date }}</template
                    >
                  </el-table-column>
                  <el-table-column label="文件">
                    <template slot-scope="scope">

                    </template>
                  </el-table-column>
                  <el-table-column align="center" label="操作" width="220px" fixed="right">
                    <template slot-scope="scope">
                      <el-button
                        v-if="
                          scope.row.state == '待上报' &&
                          checkPermission(['record_update'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'update', record:scope.row})"
                        >编辑</el-button
                      >
                      <el-button
                        v-if="
                          (scope.row.state == '待上报' || scope.row.state == '待整改')
                          &&
                          checkPermission(['record_up'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'up', record:scope.row})"
                        >上报</el-button
                      >
                      <el-button
                        v-if="
                          scope.row.state == '已上报' &&
                          checkPermission(['record_confirm'])
                        "
                        type="primary"
                        size="small"
                        @click="handleRecord({action:'confirm', record:scope.row})"
                        >确认</el-button
                      >
                      <el-button
                        v-if="
                          scope.row.state == '已上报' &&
                          checkPermission(['record_reject'])
                        "
                        type="danger"
                        size="small"
                        @click="handleRecord({action:'reject', record:scope.row})"
                        >驳回</el-button
                      >
                      <el-button
                        v-if="checkPermission(['record_view'])"
                        size="small"
                        @click="handleRecord({action:'view', record:scope.row})"
                        >查看</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog
      :visible.sync="dialogVisible"
      title="初始化任务"
      :close-on-click-modal="false"
    >
      <taskinit ref="taskinit" @handleChose="chooseComplete"></taskinit>
    </el-dialog>
    <el-drawer title="记录" :visible.sync="drawer" :with-header="false">
      <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, gettaskcontents, gettaskdepts } from "@/api/task";
import { getRecordList } 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";
export default {
  components: { Pagination, taskinit, recorddo },
  data() {
    return {
      task: { id: 0 },
      dialogVisible: false,
      activeName: "contenttab",
      contents: [],
      depts: [],
      listLoading: false,
      recordList1: [],
      recordList2: [],
      nowcontent:{},
      nowdept:{},
      drawer: false,
      data: {},
    };
  },
  created() {
    this.task.id = this.$route.params.id;
    this.gettask();
    this.gettaskcontents();
    this.gettaskdepts();
  },
  methods: {
    checkPermission,
    gettask() {
      gettask(this.task.id).then((res) => {
        this.task = res.data;
        if (this.task.state == "创建中") {
          this.dialogVisible = true;
        }
      });
    },
    init() {
      this.dialogVisible = true;
    },
    chooseComplete(data) {
      this.dialogVisible = false;
      const rLoading = this.openLoading("正在初始化任务,请稍等...");
      inittask(this.task.id, data).then((res) => {
        rLoading.close();
        this.$message.success("成功");
        this.$router.go(0);
      });
    },
    gettaskcontents() {
      gettaskcontents(this.task.id).then((res) => {
        this.contents = res.data;
        this.listLoading = true;
        this.nowcontent = {id:this.contents[0].id, name:this.contents[0].name}
        getRecordList({
          pageoff: true,
          content: this.contents[0].id,
          task: this.task.id,
        }).then((res) => {
          this.listLoading = false;
          this.recordList1 = res.data;
        });
      });
    },
    gettaskdepts() {
      gettaskdepts(this.task.id).then((res) => {
        this.depts = res.data;
      });
    },
    chosecontent(item) {
      this.nowcontent = item
      var data = {
        pageoff: true,
        task: this.task.id,
        content:item.id
      };
      this.listLoading = true;
      getRecordList(data).then((res) => {
        this.listLoading = false;
        this.recordList1 = res.data;
      });
    },
    chosedept(item) {
      this.nowdept = item
      var data = {
        pageoff: true,
        task: this.task.id,
        belong_dept:item.id
      };
      this.listLoading = true;
      getRecordList(data).then((res) => {
        this.listLoading = false;
        this.recordList2 = res.data;
      });
    },
    handleRecord(data){
      this.data = data;
      this.drawer = true;
    },
    handleDo(data) {
      this.drawer = false;
      this.gettask()
      if(this.activeName == 'contenttab'){
        this.chosecontent(this.nowcontent)
      }else if(activeName == 'depttab'){
        this.chosedept(this.nowdept)
      }
    },
  },
};
</script>