<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="任务模板id" prop="taskId">
              <el-input
                v-model="queryParams.taskId"
                placeholder="请输入任务模板id"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="标签名称" prop="tagName">
              <el-input
                v-model="queryParams.tagName"
                placeholder="请输入标签名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="是否需要拍照" prop="isPhoto">
              <el-input
                v-model="queryParams.isPhoto"
                placeholder="请输入是否需要拍照"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="拍照数量要求" prop="photoNum">
              <el-input
                v-model="queryParams.photoNum"
                placeholder="请输入拍照数量要求"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="照片存放目录" prop="saveDir">
              <el-input
                v-model="queryParams.saveDir"
                placeholder="请输入照片存放目录"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="附件id" prop="fileId">
              <el-input
                v-model="queryParams.fileId"
                placeholder="请输入附件id"
                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="['task:tag: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="['task:tag: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="['task:tag: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="['task:tag:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tagList"  @sort-change="handleSortChange"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="id" align="center" prop="id" sortable='custom' />
              <el-table-column label="任务模板id" align="center" prop="taskId" sortable='custom' />
              <el-table-column label="标签名称" align="center" prop="tagName" sortable='custom' />
              <el-table-column label="标签说明" align="center" prop="tagDesc" sortable='custom' />
              <el-table-column label="是否需要拍照" align="center" prop="isPhoto" sortable='custom' />
              <el-table-column label="拍照数量要求" align="center" prop="photoNum" sortable='custom' />
              <el-table-column label="照片存放目录" align="center" prop="saveDir" sortable='custom' />
              <el-table-column label="附件id" align="center" prop="fileId" sortable='custom' />
              <el-table-column label="备注" align="center" prop="remarks" sortable='custom' />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['task:tag:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['task:tag:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      hide-on-single-page
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务标签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-col :span="12">
                  <el-form-item label="任务模板id" prop="taskId">
                    <el-input v-model="form.taskId" placeholder="请输入任务模板id" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="标签名称" prop="tagName">
                    <el-input v-model="form.tagName" placeholder="请输入标签名称" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="标签说明" prop="tagDesc">
                    <el-input v-model="form.tagDesc" type="textarea" placeholder="请输入内容" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="是否需要拍照" prop="isPhoto">
                    <el-input v-model="form.isPhoto" placeholder="请输入是否需要拍照" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="拍照数量要求" prop="photoNum">
                    <el-input v-model="form.photoNum" placeholder="请输入拍照数量要求" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="照片存放目录" prop="saveDir">
                    <el-input v-model="form.saveDir" placeholder="请输入照片存放目录" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="附件id" prop="fileId">
                    <el-input v-model="form.fileId" placeholder="请输入附件id" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
                  </el-form-item>
                </el-col>
      </el-form>
      <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 { pageListTag, getTag, delTag, addTag, updateTag } from "@/api/task/tag";

  export default {
    name: "TaskTag",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 选中名字
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 任务标签表格数据
              tagList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
                    taskId: null,
                    tagName: null,
                    tagDesc: null,
                    isPhoto: null,
                    photoNum: null,
                    saveDir: null,
                    fileId: null,
                    remarks: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
                        taskId: [
                    { required: true, message: "任务模板id不能为空", trigger: "blur" }
                  ],
                        tagName: [
                    { required: true, message: "标签名称不能为空", trigger: "blur" }
                  ],
                        tagDesc: [
                    { required: true, message: "标签说明不能为空", trigger: "blur" }
                  ],
                        isPhoto: [
                    { required: true, message: "是否需要拍照不能为空", trigger: "blur" }
                  ],
                        photoNum: [
                    { required: true, message: "拍照数量要求不能为空", trigger: "blur" }
                  ],
                        saveDir: [
                    { required: true, message: "照片存放目录不能为空", trigger: "blur" }
                  ],
                        fileId: [
                    { required: true, message: "附件id不能为空", trigger: "blur" }
                  ],
                        remarks: [
                    { required: true, message: "备注不能为空", trigger: "blur" }
                  ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询任务标签列表 */
      getList() {
        this.loading = true;
        pageListTag(this.queryParams).then(response => {
          this.tagList = response.rows;
          this.total = response.total;
          this.loading = false;
        }).catch(e => {
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
                    id: null,
                    taskId: null,
                    tagName: null,
                    tagDesc: null,
                    isPhoto: null,
                    photoNum: null,
                    saveDir: null,
                    fileId: null,
                    remarks: null,
        };
        this.resetForm("form");
      },
      //排序
      handleSortChange(col) {
        this.$sortBy(col, this.queryParams);
        this.getList();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.names = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加任务标签";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getTag(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改任务标签";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateTag(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTag(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const names = row.id || this.names;
        this.$modal.confirm('是否确认删除任务标签为"' + names + '"的数据项？').then(function() {
          return delTag(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
  /** 导出按钮操作 */
  handleExport() {
    this.download('task/tag/export', {
      ...this.queryParams
    }, `tag_${new Date().getTime()}.xlsx`)
  }
  }
  };
</script>
