<template>
  <div class="app-container">
    <el-card class="filterTop">
      <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"> {{ qtask.name }}</span>
            </div>
          </el-col>
          <el-col :span="8">
            <div style="margin-bottom: 6px">
              <span class="term">截止日期</span>
              <span class="desc"> {{ qtask.end_date }}</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="qtask.state == '待发布'"
                  type="primary"
                >
                  {{ qtask.state }}
                </el-tag>
                <el-tag
                  effect="plain"
                  v-else-if="qtask.state == '执行中'"
                  type="success"
                >{{ qtask.state }}
                </el-tag>
                <el-tag effect="plain" v-else type="warning">
                  {{qtask.state}}
                </el-tag>
              </span>
            </div>
          </el-col>
        </el-row>
        <div style="margin-top: 4px">
          <el-button
            type="primary"
            @click="handleQtaskStart()"
            v-if="qtask.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="qtaskDepts"
        border
        fit
        stripe
        highlight-current-row
        :height="tableHeight"
        @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.org_">
            <span style="color:darkblue">{{scope.row.org_.sort}}</span>-
            {{scope.row.org_.name}}
          </template>
        </el-table-column>
        <el-table-column label="报送数">
          <template slot-scope="scope"> {{ scope.row.count }}</template>
        </el-table-column>
        <el-table-column label="确认数">
          <template slot-scope="scope">
            {{ scope.row.count_confirmed }}
          </template>
        </el-table-column>
        <!--<el-table-column
          align="center"
          label="操作"
          width="240px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link
              type="primary"
              size="small"
              @click="handleConfirmFK(scope)"
            >反馈
            </el-link
            >
            <el-link
              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>
      <el-table
        v-loading="listLoading2"
        :data="recordList"
        border
        fit
        stripe
        highlight-current-row
        :height="tableHeight"
        style="margin-top:2px"
        @selection-change="handleSelectRecords"
      >
        <el-table-column label="序号" type="index" align="center" width="55"/>
        <el-table-column label="报送类型">
          <template slot-scope="scope">
            <span v-if="scope.row.action==='ablity:create'">新增能力</span>
            <span v-else-if="scope.row.action==='service:update'">更新服务</span>
            <span v-else-if="scope.row.action==='quali:update'">更新资质</span>
            <span v-else-if="scope.row.action==='quali:create'">新增资质</span>
            <span v-else-if="scope.row.action==='ability:nochange'">能力无新增</span>
            <span v-else-if="scope.row.action==='quali:nochange'">资质无新增</span>
          </template>
        </el-table-column>
        <el-table-column label="确认情况">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.confirmed" 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">
            <span v-if=" scope.row.create_by_">{{ scope.row.create_by_.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template slot-scope="scope">{{ scope.row.update_time }}</template>
        </el-table-column>
        <el-table-column label="文件">
          <template slot-scope="scope">
            <el-link v-if="scope.row.file_" :href="scope.row.file_.file" target="_blank" type="primary">
              {{scope.row.file_.name}}
            </el-link>
            <span v-if="scope.row.value1">
               <el-link v-if="scope.row.value1.file_" :href="scope.row.value1.file_.file" target="_blank" type="primary">
                {{scope.row.value1.file_.name}}
               </el-link>
            </span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="操作"
          width="240px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-link
              v-if="!scope.row.confirmed"
              type="success"
              size="small"
              @click="handleRecord('confirm', scope.row )"
            >确认
            </el-link>
            <el-link
              size="small"
              @click="handleRecord( 'view', scope.row )"
            >查看
            </el-link>
            <el-link
              v-if="!scope.row.confirmed"
              type="danger"
              size="small"
              @click="handleDelete( scope.row.id )"
            >删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-drawer
      title="记录"
      :visible.sync="drawer"
      :with-header="false"
      size="40%"
    >
      <div
        v-if="showData.type==='quali:nochange'"
        style="padding: 10px 20px;position: relative"
      >
        <p style="font-size: 20px;font-weight: bold;">资质无新增</p>
        <el-button
          v-if="actionType==='confirm'"
          style="position: absolute;right:40px;margin-top: 20px"
          type="primary"
          @click="confirmClick"
        >
          确认
        </el-button>
      </div>
      <div
        v-if="showData.type==='ability:nochange'"
        style="padding: 10px 20px;position: relative"
      >
        <p style="font-size: 20px;font-weight: bold;">能力无新增</p>
        <el-button
          v-if="actionType==='confirm'"
          style="position: absolute;right:40px;margin-top: 20px"
          type="primary"
          @click="confirmClick"
        >
          确认
        </el-button>
      </div>
      <div
        v-if="showData.type==='service:update'"
        style="padding: 10px 20px;position: relative"
      >
        <p style="font-size: 20px;font-weight: bold;">更新服务</p>
        <p><span style="display: inline-block;width: 100px;font-weight: bold;">原始内容：</span>{{showData.data.value1}}</p>
        <p><span style="display: inline-block;width: 100px;font-weight: bold;">更新内容：</span>{{showData.data.value2}}</p>
        <el-divider></el-divider>
        <el-button
          v-if="actionType==='confirm'"
          style="position: absolute;right:40px;margin-top: 20px"
          type="primary"
          @click="confirmClick"
        >
          确认
        </el-button>
      </div>
      <div
        v-if="showData.type==='ablity:create'"
        style="padding: 10px 20px;position: relative"
      >
        <p style="font-size: 20px;font-weight: bold;">新增能力</p>
        <p><span style="display: inline-block;width: 100px;font-weight: bold;">能力类型 ：</span>{{atype_name}}</p>
        <p><span style="display: inline-block;width: 100px;font-weight: bold;">能力领域：</span>{{afield_name}}</p>
        <p><span class="drawerInfoTitle">对象数量：</span>{{showData.data.num}}</p>
        <p><span class="drawerInfoTitle">参数数量：</span>{{showData.data.num2}}</p>
        <p><span class="drawerInfoTitle">方法标准数量：</span>{{showData.data.num3}}</p>
        <p><span class="drawerInfoTitle">产品标准数量：</span>{{showData.data.num4}}</p>
        <p><span style="display: inline-block;width: 100px;font-weight: bold;">上传文件：</span>
          <text v-if="showData.data.file_">{{showData.data.file_.name}}</text>
          <text v-else>无上传文件</text>
        </p>
        <el-link v-if="showData.data.file_" :href="showData.data.file_.file" target="_blank" type="primary">{{showData.data.file_.name}}</el-link>
        <el-divider></el-divider>
        <el-button
          v-if="actionType==='confirm'"
          style="position: absolute;right:40px;margin-top: 20px"
          type="primary"
          @click="confirmClick"
        >
          确认
        </el-button>
      </div>
      <quliShow
        v-if="drawer&&showData.type==='quali:create'"
        ref="quliShow"
        :showData="showData"
        @handleDo="handleDo"
      ></quliShow>
      <quliShow
        v-if="drawer&&showData.type==='quali:update'"
        ref="quliShow"
        :showData="showData"
        :fieldList2="fieldList2"
        @handleDo="handleDo"
      ></quliShow>
    </el-drawer>
  </div>
</template>
<script>
  import {
    qtaskStart, qactionList, qtaskDetail,
    qactionDelete, qactionItem, qactionConfirm
  } from "@/api/ability";
  import {getRecordList, updateRecords} from "@/api/record";
  import Pagination from "@/components/Pagination";
  import checkPermission from "@/utils/permission";
  import quliShow from "@/views/ability/quliShow";
  import {upUrl, upHeaders} from "@/api/file";
  import {getDictList} from "@/api/dict";

  export default {
    name: 'qualityTaskDo',
    components: {Pagination, quliShow},
    data() {
      return {
        dialogVisible: false,
        dialogVisibles: false,
        dialogType: "new",
        activeName: "contenttab",
        contents: [],
        depts: [],
        listLoading: false,
        listLoading2: false,
        recordList: [],
        nowcontent: {},
        nowdept: {},
        drawer: false,
        data: {},
        showData: {},
        listQuery: {},
        selectRecords: [],
        dgaction: {
          action: 'init',
          name: '初始化任务'
        },
        upHeaders: upHeaders(),
        upUrl: upUrl(),
        fileList: [],
        Taskdept: {is_yes: true},
        qtaskId: null,
        qtaskDepts: [],
        qtask: {},
        detailItem: {},
        actionType: null,
        tableHeight: null,
        fieldList2: [],
        atypeOptions: {},
        afieldOptions: {},
        atype_name:'',
        afield_name:'',
        item:{}
      };
    },
    created() {
      this.qtaskId = sessionStorage.getItem('qtaskId');
    },
    mounted() {
      let height1 = document.getElementsByClassName('app-main')[0].clientHeight;
      let height2 = document.getElementsByClassName('filterTop')[0].clientHeight;
      let computedHeight = height1-height2;
      this.tableHeight = computedHeight/2-54;
      this.qtaskId = sessionStorage.getItem('qtaskId');
      this.getOptions();
      this.getQtaskDept();


    },
    methods: {
      //领域类型和领域名称
      getOptions() {
        getDictList({type__code: "field_type", is_used: true}).then((res) => {
          let obj = new Object();
          res.data.forEach(item=>{
            obj[item.id]=item.name;
          });
          this.afieldOptions = obj;
          debugger;
          console.log(this.afieldOptions)
        });
        getDictList({type__code: "quality_type", is_used: true}).then((res) => {
          // this.atypeOptions = res.data;
          let obj1 = new Object();
          res.data.forEach(item1=>{
            obj1[item1.id]=item1.name;
          });
          this.atypeOptions = obj1;
        });
      },
      checkPermission,
      getQtaskDept() {
        qtaskDetail({page:1,page_size:9999,qtask: this.qtaskId}).then((res) => {
          this.qtaskDepts = res.data.results;
          this.qtask = res.data.results[0].qtask_;
        });
      },
      handleQtaskStart() {
        this.$confirm("确认发布任务吗?", "提示")
          .then(async () => {
            await qtaskStart(this.qtaskId);
            location.reload();
            this.$message.success("成功");
          })
          .catch((err) => {
            console.error(err);
          });
      },
      //操作处理
      handleRecord(type, item) {
        this.item = item
        this.showData.id = item.id;
        this.showData.data = item;
        this.showData.type = item.action;
        this.showData.action = type;
        this.actionType = type;
        this.fieldList2 =[];
        if(item.afield){
          debugger;
          this.afield_name = this.afieldOptions[item.afield];
        }
        if(item.atype){
          debugger;
          this.atype_name = this.atypeOptions[item.atype];
        }
        qactionItem(item.id).then(res=>{
          if (res.code>=200) {
            let updateDetail = res.data.update_detail;//更改字段
            updateDetail.forEach(item=>{
              this.fieldList2.push(item.field);//所有字段
            })
            this.drawer = true;
          }
          console.log(this.fieldList2)
        })
      },
      handleDelete(id) {
        this.$confirm("确认删除该操作吗?", "提示")
          .then(async () => {
            await qactionDelete(id).then(res=>{
              if(res.code>=200&&res.code<400){
                this.getQactionList();
                this.$message.success("成功");
              }
            });
          })
          .catch((err) => {
            console.error(err);
          });
      },
      //操作确认
      confirmClick() {
        qactionConfirm(this.showData.id).then(res => {
          if (res.code>=200) {
            this.drawer = false;
            this.getQactionList();
          }
        })
      },
      handleDo(data) {
        console.log(data);
        this.drawer = data;
        this.getQactionList();
      },
      getQactionList() {
        qactionList(this.listQuery)
          .then((res) => {
            this.listLoading2 = false;
            this.recordList = res.data;
          })
          .catch((e) => {
            this.listLoading2 = false;
          });
      },
      clickRow(row, col, e) {
        this.listLoading2 = true;
        this.detailItem = row;
        this.listQuery = {pageoff: true, qtask: row.qtask, belong_dept: row.org};
        this.getQactionList();
      },
      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.getQactionList();
          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.getQactionList();
            })
          }).catch(() => {
          });
        } else {
          this.$message({
            message: "请先选择",
            type: "warning",
          });
        }
      }
    },
  };
</script>
<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>
