<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ggpx:TaskList:add']"
        >新增任务
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd2"
          v-hasPermi="['ggpx:TaskList:add']"
        >新建评价表
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['ggpx:TaskList:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['ggpx:TaskList:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['ggpx:TaskList:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="TaskListList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" v-if="checkPermi(['ggpx:TaskList:add'])"/>-->
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="任务名称" align="center" prop="taskName" min-width="160">
        <template slot-scope="scope">
          <router-link style="color: #1c84c6"
                       :to="'/TaskData/'+scope.row.id+'?taskName='+scope.row.taskName+'&status='+scope.row.status">
            {{ scope.row.taskName }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="样品数" align="center">
        <template slot-scope="scope">
          {{ scope.row.sampleCount }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success">正在评价</el-tag>
          <el-tag v-if="scope.row.status === 0" type="info">评价结束</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column label="作为模板" align="center" prop="remark" v-if="checkPermi(['ggpx:TaskList:add'])"/>-->
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="100">
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="100" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['ggpx:TaskList:edit']"
          >
            <router-link :to="'/TaskBase/'+scope.row.id+'?taskName='+scope.row.taskName">指标体系</router-link>
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ggpx:TaskList:edit']"
          >修改任务
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['ggpx:TaskData:add']"
            v-if="scope.row.status === 1"
          >
            <router-link
              :to="'/TaskData/'+scope.row.id+'?taskName='+scope.row.taskName+'&status='+scope.row.status">
              填写任务
            </router-link>
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ggpx:TaskList:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" fullscreen append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称"/>
        </el-form-item>
        <!--        <el-form-item label="备注" prop="remark">-->
        <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="状态" prop="status" v-if="title.includes('修改')">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正在评价</el-radio>
            <el-radio :label="0">评价结束</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="任务类型" prop="type" v-if="title.includes('新')">-->
        <!--          <el-radio-group disabled v-model="form.type">-->
        <!--            <el-radio-->
        <!--              v-for="dict in dict.type.ggpx_task_type"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.value"-->
        <!--              @change="handleTypeChange"-->
        <!--            >{{ dict.label }}-->
        <!--            </el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
        <el-form-item label="设为模板" prop="isTemplate" v-if="title.includes('修改')">
          <el-radio-group v-model="form.isTemplate">
            <el-radio :label="1" :key="1">是</el-radio>
            <el-radio :label="0" :key="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="统计分析" prop="statistics"
                      v-if="form.isTemplate===1 && checkPermi(['ggpx:TaskDataStatistics:query'])">
          <el-input type="textarea" v-model="form.statistics" placeholder="请输入统计分析"></el-input>
        </el-form-item>
        <el-form-item label="任务模板" v-if="title.includes('新')">
          <el-select v-model="templateId" clearable placeholder="请选择模板" @click.native="getTemplateList">
            <el-option
              v-for="item in templateList"
              :key="item.id"
              :label="item.taskName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div v-if="templateId && title.includes('新')">
        <h2>模板预览</h2>
        <el-form label-width="80px">
          <!--        <el-form-item label="任务ID" prop="taskId">-->
          <!--          <el-input v-model="form.taskId" placeholder="请输入任务ID"/>-->
          <!--        </el-form-item>-->
          <!--        <el-form-item label="样品名称" prop="sampleName">-->
          <!--          <el-input v-model="form.sampleName" placeholder="请输入样品名称"/>-->
          <!--        </el-form-item>-->
          <!-- 遍历fields,动态创建表单 -->
          <div v-for="(field, index) in templateData" :key="index">
            <div v-if="!field.children">
              <el-divider></el-divider>
              <el-form-item :label="field.fieldName" :prop="field.fieldName">
                <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 8}" v-if="field.fieldType === 'textarea'"
                          v-model="field.value"/>
                <el-input v-if="field.fieldType === 'input'" v-model="field.value"/>
                <el-input-number v-if="field.fieldType === 'number'" v-model="field.value"
                                 :min="0" :max="field.maxVal"/>
                <div v-if="field.fieldType === 'number'">满分:{{ field.maxVal }}</div>
                <el-radio-group v-if="field.fieldType === 'radio_sf'" v-model="field.value">
                  <el-radio label="是">是</el-radio>
                  <el-radio label="否">否</el-radio>
                </el-radio-group>
                <el-radio-group v-if="field.fieldType === 'radio_diy'" v-model="field.value">
                  <el-radio style="margin-top: 15px" v-for="(item,index) in field.remark.split('/')" :label="item">
                    {{ item }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </div>
            <div v-if="field.children && field.children.length > 0">
              <el-divider></el-divider>
              <h3 style="color: #1c84c6">{{ field.fieldName }}</h3>
              <div v-for="(field, index) in field.children" :key="index">
                <el-form-item :label="field.fieldName" :prop="field.fieldName">
                  <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 8}" v-if="field.fieldType === 'textarea'"
                            v-model="field.value" placeholder="请输入内容"/>
                  <el-input v-if="field.fieldType === 'input'" v-model="field.value"/>
                  <el-input-number v-if="field.fieldType === 'number'" v-model="field.value" :min="0"
                                   :max="field.maxVal"></el-input-number>
                  <div v-if="field.fieldType === 'number'">满分:{{ field.maxVal }}</div>
                  <el-radio-group v-if="field.fieldType === 'radio_sf'" v-model="field.value">
                    <el-radio label="是">是</el-radio>
                    <el-radio label="否">否</el-radio>
                  </el-radio-group>
                  <el-radio-group style="margin-top: 15px" v-if="field.fieldType === 'radio_diy'" v-model="field.value">
                    <el-radio v-for="(item,index) in field.remark.split('/')" :label="item">{{ item }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </div>
          </div>
          <el-divider></el-divider>
          <!--        <el-form-item label="任务数据" prop="data">-->
          <!--          <el-input v-model="form.data" placeholder="请输入任务数据"/>-->
          <!--        </el-form-item>-->
          <el-form-item label="组长" prop="groupLeader">
            <el-radio-group v-model="form.groupLeader">
              <el-radio label="是">是</el-radio>
              <el-radio label="否">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <!--        <el-form-item label="备注" prop="remark">-->
          <!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
          <!--        </el-form-item>-->
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTaskBase,
  getTaskBase,
  delTaskBase,
  addTaskBase,
  updateTaskBase,
  addTaskBaseBatch
} from "@/api/ggpx/TaskBase";
import {
  listTaskList,
  getTaskList,
  delTaskList,
  addTaskList,
  updateTaskList,
} from "@/api/ggpx/TaskList";
import {listTaskDataCount} from "@/api/ggpx/TaskData";
import {checkPermi, checkRole} from "../../../utils/permission";

export default {
  name: "TaskList",
  dicts: ["ggpx_task_type", "ggpx_task_analysis"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 任务列表表格数据
      TaskListList: [],
      templateId: null,
      templateData: [],
      templateList: [],
      // 弹出层标题
      title: "新增任务",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          {required: true, message: "任务名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    templateId: {
      handler: function (val) {
        if (val) {
          listTaskBase({taskId: val}).then(response => {
            console.log(response.data);
            this.templateData = this.handleTree(response.data, "id", "parentId");
          });
        }
      },
      immediate: true,
      deep: true
    },
  },
  methods: {
    checkRole,
    checkPermi,
    getTemplateList() {
      listTaskList({isTemplate: 1}).then(response => {
        this.templateList = response.rows;
      });
    },
    /** 查询任务列表列表 */
    getList() {
      this.loading = true;
      listTaskList(this.queryParams).then(response => {
        this.TaskListList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        type: "1", // 1: 常规任务
        status: 1, // 1: 正在评价
        statistics: null,
        isTemplate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "新增任务";
      this.reset();
      this.form.type = "1";
      this.open = true;
      this.templateId = null;
    },
    /** 新增按钮操作 */
    handleAdd2() {
      this.title = "新增评价表";
      this.reset();
      this.form.type = "2";
      this.open = true;
      this.templateId = null;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = "修改任务";
      this.reset();
      const id = row.id || this.ids
      getTaskList(id).then(response => {
        this.form = response.data;
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTaskList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$router.push({path: '/TaskBase/' + this.form.id, query: {taskName: this.form.taskName}});
              this.getList();
              // 跳转页面
            });
          } else {
            if (!this.templateId) {
              this.form.status = 0; // 没有选择模板不允许评价
            } else {
              this.form.parentId = this.templateId;
            }
            addTaskList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              console.log(response);
              if (response.code === 200) {
                if (!this.templateId) {
                  this.$router.push({path: '/TaskBase/' + response.data, query: {taskName: this.form.taskName}});
                }
              }
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务列表编号为"' + ids + '"的数据项？').then(function () {
        return delTaskList(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('TaskList/export', {
        ...this.queryParams
      }, `TaskList_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
