<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="fName">
        <el-input
          v-model="queryParams.fName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="对象分类" prop="fObjectType">
        <el-select v-model="queryParams.fObjectType" placeholder="请选择对象分类" clearable size="small">
          <el-option
            v-for="dict in fObjectTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="抽查要求" prop="fSpotCheckType">
        <el-select v-model="queryParams.fSpotCheckType" placeholder="请选择抽查要求" clearable size="small">
          <el-option
            v-for="dict in fSpotCheckTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		     :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inforList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="fName" :show-overflow-tooltip="true"/>
      <el-table-column label="对象分类" align="center" prop="fObjectType" :formatter="fObjectTypeFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="检查表实例关联Id" align="center" prop="fHazardCheckTableRecordId" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fHazardCheckTableRecordId">{{ scope.row.fHazardCheckTableRecordId }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="抽查要求" align="center" prop="fSpotCheckType">
        <template slot-scope="scope">
          <span v-if="scope.row.fSpotCheckType">{{ fSpotCheckTypeFormat(scope.row) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="城区标识" align="center" prop="fSpecialFlag" :formatter="fSpecialFlagFormat">
        <template slot-scope="scope">
          <span v-if="scope.row.fSpecialFlag">{{ fSpecialFlagFormat(scope.row) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="此对象应检数据" align="center" prop="fLotCount">
        <template slot-scope="scope">
          <span v-if="scope.row.fLotCount">{{ scope.row.fLotCount }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="fCreateTime" />
      <el-table-column label="创建人" align="center" prop="fCreateBy" />
      <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-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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" width="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row class="el-row-table">
          <el-col :span="12">
            <el-form-item label="任务名称" prop="fHazardCheckTaskId">
              <el-select
                v-model="form.fHazardCheckTaskId"
                placeholder="请选择任务名称"
                style="width: 100%"
              >
                <el-option
                  v-for="task in taskData"
                  :key="task.fInsTaskInforId"
                  :label="task.fName"
                  :value="task.fInsTaskInforId"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="对象分类" prop="fObjectType">
              <el-select v-model="form.fObjectType" placeholder="请选择对象分类" style="width: 100%">
                <el-option
                  v-for="dict in fObjectTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查表实例关联Id" prop="fHazardCheckTableRecordId">
              <el-input type="number" v-model="form.fHazardCheckTableRecordId" placeholder="请输入检查表实例关联Id" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="抽查要求" prop="fSpotCheckType">
              <el-select v-model="form.fSpotCheckType" placeholder="请选择抽查要求" style="width: 100%">
                <el-option
                  v-for="dict in fSpotCheckTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="检查范围数据" prop="fSpotCheck">
              <el-input v-model="form.fSpotCheck" placeholder="请输入检查范围数据" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="城区标识" prop="fSpecialFlag">
              <el-select v-model="form.fSpecialFlag" placeholder="请选择城区标识" style="width: 100%">
                <el-option
                  v-for="dict in fSpecialFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="此对象应检数据" prop="fLotCount">
              <el-input type="number" v-model="form.fLotCount" placeholder="请输入此对象应检数据" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="抽查细类" prop="fSubobjecttypeCheckTypeDesc">
              <el-input v-model="form.fSubobjecttypeCheckTypeDesc" placeholder="请输入抽查细类" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="删除标记">
              <el-select v-model="form.fDeleteFlag" placeholder="请选择删除标记"  style="width: 100%">
                <el-option
                  v-for="dict in fDeleteFlagOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="描述" prop="fRemark">
              <el-input type="textarea" v-model="form.fRemark" placeholder="请输入描述" />
            </el-form-item>
          </el-col>

        </el-row>

      </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>

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listInfor, getInfor, delInfor, addInfor, updateInfor, exportInfor } from "@/api/supervision/scope.js";
import { taskList } from "@/api/supervision/task";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "scope",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查范围表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 对象分类
      fObjectTypeOptions: [],
      // 抽查要求 1：抽查;0-全覆盖字典
      fSpotCheckTypeOptions: [],
      // T-主城区(默认)，F-三市字典
      fSpecialFlagOptions: [],
      // 删除标记，0-可用，1-已删除字典
      fDeleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fObjectType: null,
        fSpotCheckType: null,
        fName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fHazardCheckTaskId: [
          { required: true, message: "关联检查任务表不能为空", trigger: "blur" }
        ],
        fObjectType: [
          { required: true, message: "对象分类", trigger: "change" }
        ],
      },
      taskData: []
    };
  },
  created() {
    this.getList();
    this.getDicts("t_object_type").then(response => {
      this.fObjectTypeOptions = response.data;
    });
    this.getDicts("t_spot_check_type").then(response => {
      this.fSpotCheckTypeOptions = response.data;
    });
    this.getDicts("t_special_flag").then(response => {
      this.fSpecialFlagOptions = response.data;
    });
    this.getDicts("t_delete_flag").then(response => {
      this.fDeleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询检查范围列表 */
    getList() {
      this.loading = true;
      listInfor(this.queryParams).then(response => {
        this.inforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 对象分类
    fObjectTypeFormat(row, column) {
      return this.selectDictLabel(this.fObjectTypeOptions, row.fObjectType);
    },
    // 抽查要求 1：抽查;0-全覆盖字典翻译
    fSpotCheckTypeFormat(row, column) {
      return this.selectDictLabel(this.fSpotCheckTypeOptions, row.fSpotCheckType);
    },
    // T-主城区(默认)，F-三市字典翻译
    fSpecialFlagFormat(row, column) {
      return this.selectDictLabel(this.fSpecialFlagOptions, row.fSpecialFlag);
    },
    // 删除标记，0-可用，1-已删除字典翻译
    fDeleteFlagFormat(row, column) {
      return this.selectDictLabel(this.fDeleteFlagOptions, row.fDeleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fScopeInforId: null,
        fHazardCheckTaskId: null,
        fObjectType: null,
        fHazardCheckTableRecordId: null,
        fSpotCheckType: null,
        fSpotCheck: null,
        fSpecialFlag: null,
        fLotCount: null,
        fSubobjecttypeCheckTypeDesc: null,
        fDeleteFlag: 0,
        fRemark: null,
        fCreateTime: null,
        fCreateBy: null,
        fUpdateTime: null,
        fModifyBy: 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.fScopeInforId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTaskList();
      this.open = true;
      this.title = "添加检查范围";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fScopeInforId = row.fScopeInforId || this.ids
      getInfor(fScopeInforId).then(response => {
        this.form = response.data;
        this.getTaskList();
        this.open = true;
        this.title = "修改检查范围";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fScopeInforId != null) {
            updateInfor(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfor(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fScopeInforIds = row.fScopeInforId || this.ids;
      this.$confirm('是否确认删除检查范围编号为"' + fScopeInforIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfor(fScopeInforIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有检查范围数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfor(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //任务列表
    getTaskList(){
      taskList().then(res =>{
        if(res.code == 200 && res.data){
           this.taskData = res.data;
        }
      })
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fScopeInforId);
    },
  }
};
</script>
