<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="6">
            <div style="margin-bottom: 6px">
              <span class="term">任务名称:</span>
              <span class="desc"> {{ task.name }}</span>
            </div>
          </el-col>
           <el-col :span="6">
            <div style="margin-bottom: 6px">
              <span class="term">巡检开始日期:</span>
              <span class="desc"> {{ task.start_date }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <div style="margin-bottom: 6px">
              <span class="term">巡检结束日期:</span>
              <span class="desc"> {{ task.end_date }}</span>
            </div>
          </el-col>
          <el-col :span="6">
            <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-row>
       
      </div>
        <el-button
            type="primary"
            @click="starttask()"
            v-if="task.state != '已完成'"
            size="small"
            >发布任务</el-button
          >
    </el-card>

   
    <el-card style="margin-top: 2px">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-plus" @click="createChildtask()"
        >创建子任务</el-button
      >
      </div>
    
       <el-table
        :data="subinsecttask"
        border
        fit
        stripe
        highlight-current-row
        max-height="300"
      >
        <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">
          <span v-for="item in scope.row.members" v-bind:key="item.member">
           
            <el-tag v-if="item.type=='组长'" effect="plain">{{
                item.member__name
              }}</el-tag>
            <el-tag type="danger" effect="plain" v-else>{{
                item.member__name
              }}</el-tag>
            </span>
           
           </template>
        </el-table-column>
        
       <el-table-column align="center" label="发布状态">
          <template slot-scope="scope">{{ scope.row.state }}</template>
        </el-table-column>
         
        <el-table-column
          align="center"
          label="操作"
          width="200px"
          fixed="right"
        >
          <template slot-scope="scope">
               <el-button @click="handleClick(scope)"   type="primary">执行</el-button>
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope)"
            /> -->
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

 

   <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑任务' : '新增任务'"
    >
      <el-form
        ref="Form"
        :model="inspecttask"
        label-width="80px"
        label-position="right"
      >
        <el-form-item label="子任务名" prop="name">
          <el-input v-model="inspecttask.name" placeholder="子任务名" />
        </el-form-item>
         <el-form-item label="选择组长" prop="leader">
            <el-select
              v-model="inspecttask.leader"
              style="width: 100%"
              allow-create
              default-first-option
              placeholder="请选择组长">
              <el-option
                 v-for="item in leaderOptions"
                  :key="item.value"
                  :label="item.label"
                 :value="item.value">
               </el-option>
           </el-select>
            
        
        </el-form-item>
          <el-form-item label="选择组员" prop="member">
               <el-select
              v-model="inspecttask.members"
              multiple
              filterable
              style="width: 100%"
              allow-create
              default-first-option
              placeholder="请选择组员">
              <el-option
                 v-for="item in memberOptions"
                  :key="item.value"
                  :label="item.label"
                 :value="item.value">
               </el-option>
           </el-select>
            
         
        </el-form-item>
          <el-form-item label="选择组织" prop="leader">
             
          <el-select
              v-model="inspecttask.depts"
              multiple
              filterable
              style="width: 100%"
              allow-create
              default-first-option
              placeholder="请选择组织">
              <el-option
                 v-for="item in deptOptions"
                  :key="item.value"
                  :label="item.label"
                 :value="item.value">
               </el-option>
           </el-select>
        </el-form-item>
       
      </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>
     
  </div>
</template>
<style>
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    width: 50%;
  }
</style>
<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 { getinspecttask } from "@/api/inspectTask";
import { getsubinspecttasklist,createsubinspecttask,starttask,deletesubinspecttask,getsubinspecttaskdep,getinspectrecordlist,addappoint} from "@/api/subinspecttask";
import { getUserList } from "@/api/user";
import { getOrgList } from "@/api/org";
import { genTree } from "@/utils";
import Pagination from "@/components/Pagination";
import checkPermission from "@/utils/permission";

const defaulttask = {
  name: "",
  leader:"",
  members:"",
  depts:"",
};

export default {
  components: { Pagination},
  
  data() {
    return {
      task: { id: 0 },
      inspecttask: defaulttask,
      dialogVisible: false,
      dialogFormVisible:false,
      listLoading: true,
      leaderOptions:[],
      recordList:[],
      ids:[],
      subinsecttask:[],
      memberOptions:[],
      deptOptions:[],
      leaders:[],
      dialogType: "new",
      data: {},
    };
  },
  created() {
    this.task.id = this.$route.params.id;
    this.getinspecttask();//主任务信息
   
    this.getUserList();//组员
     this.getOrgList();//组织
     this.getsubinspecttasklist();//子任务名
  },
  methods: {
    checkPermission,
    //主任务信息展示
    getinspecttask() {
      getinspecttask(this.task.id).then((res) => {
        this.task = res.data;
       
      });
    },
    //子任务信息
    getsubinspecttasklist(){
      getsubinspecttasklist({pageoff:true}).then((res)=>{
        this.subinsecttask=res.data;
      });
    },
  
    //组员列表
    getUserList() {
      getUserList({pageoff:true, roles__name:'质量巡查成员'}).then((res) => {
        this.leaderOptions = genTree(res.data);
        this.memberOptions = genTree(res.data);
      });
    },
    //组织列表
      getOrgList() {
      getOrgList({can_supervision:true}).then((res) => {
        this.deptOptions =  genTree(res.data);
      });
    },

    handleCreate() {
      this.inspecttask = Object.assign({}, defaulttask);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },

    starttask() {
      this.$confirm("确认发布任务吗?", "提示")
        .then(async () => {
          await starttask(this.task.id);
          location.reload();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
     //创建子任务
    async confirm(form) {
     
      this.inspecttask.inspecttask=this.$route.params.id;
      
         createsubinspecttask(this.inspecttask).then((res) => {
             
                this.dialogVisible = false;
                 this.getsubinspecttasklist()
                this.$message.success("成功");
              
            });
    },
    //子任务删除
    handleDelete(scope){
        
         this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deletesubinspecttask(scope.row.id);
          this.getsubinspecttasklist()
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    //子任务编辑
    handleEdit(){
     
    },
    //点击子任务，弹出巡检的公司
     getsubinspecttaskdep() {
      getsubinspecttaskdep(this.id)
        .then((res) => {
          this.recordList = res.data;
          
        })
     
    },
    createChildtask()
    {
      
      this.inspecttask = Object.assign({}, defaulttask);
      this.dialogVisible = true;
      
    },
    clickRow(row, col, e) {
      this.listLoading2 = true;
      this.id =  row.id;
      this.$router.push({name: "myTaskdos", params: { id:  this.id,leaders:row.members }, })
   
      
    },
  

     handleClick(scope) {
      this.listLoading2 = true;
      this.$router.push({name: "myTaskdos", params: { id: scope.row.id,leaders:scope.row.members }, })
    },
    
   
   
  },
};
</script>