<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <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="fUniqueCode">
        <el-input
          v-model="queryParams.fUniqueCode"
          placeholder="请输入任务唯一编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务归属" prop="fTaskBelong">
        <el-input
          v-model="queryParams.fTaskBelong"
          placeholder="请输入任务归属"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="fTaskType">
        <el-select v-model="queryParams.fTaskType" placeholder="请选择任务类型" clearable size="small">
          <el-option
            v-for="dict in fTaskTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否下发" prop="fDistributeFlag">
        <el-select v-model="queryParams.fDistributeFlag" placeholder="请选择是否下发" clearable size="small">
          <el-option
            v-for="dict in fDistributeFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="任务状态" prop="fStatus">
        <el-select v-model="queryParams.fStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in fStatusOptions"
            :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>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          type="info"
          @click="getGovermentTaskInfo"
        >从省平台获取数据</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="fUniqueCode" :show-overflow-tooltip="true"/>
      <el-table-column label="任务归属" align="center" prop="fTaskBelong" :show-overflow-tooltip="true"/>
      <el-table-column label="任务类型" align="center" prop="fTaskType">
        <template slot-scope="scope">
          <span v-if="scope.row.fTaskType">{{ fTaskTypeFormat(scope.row) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="是否下发" align="center" prop="fDistributeFlag" :formatter="fDistributeFlagFormat" />
      <el-table-column label="任务开始时间" align="center" prop="fStartTime">
        <template slot-scope="scope">
          <span v-if="scope.row.fStartTime">{{ scope.row.fStartTime }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="任务截至时间" align="center" prop="fEndTime">
        <template slot-scope="scope">
          <span v-if="scope.row.fEndTime">{{ scope.row.fEndTime }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="fStatus" :formatter="fStatusFormat" />
      <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="110px">
        <el-row class="el-row-table">
          <el-col :span="12">
            <el-form-item label="任务名称" prop="fName">
              <el-input v-model="form.fName" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务唯一编码" prop="fUniqueCode">
              <el-input v-model="form.fUniqueCode" placeholder="请输入任务唯一编码" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务归属" prop="fTaskBelong">
              <el-input v-model="form.fTaskBelong" placeholder="请输入任务归属" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务类型" prop="fTaskType">
              <el-select v-model="form.fTaskType" placeholder="请选择任务类型" style="width: 100%">
                <el-option
                  v-for="dict in fTaskTypeOptions"
                  :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="fDistributeFlag">
              <el-select v-model="form.fDistributeFlag" placeholder="请选择是否下发" style="width: 100%">
                <el-option
                  v-for="dict in fDistributeFlagOptions"
                  :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="fDesc">
              <el-input type="textarea" v-model="form.fDesc" placeholder="请输入任务说明" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务开始时间" prop="fStartTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.fStartTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择任务开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="任务截至时间" prop="fEndTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.fEndTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择任务截至时间">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务状态" prop="fStatus">
              <el-select v-model="form.fStatus" placeholder="请选择任务状态" style="width: 100%">
                <el-option
                  v-for="dict in fStatusOptions"
                  :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="fDeleteFlag">
              <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="12">
            <el-form-item label="日常检查频率" prop="inspectionFrequency">
              <el-input v-model="form.inspectionFrequency" placeholder="请输入日常检查频率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="对象数量比例" prop="quantityRatio">
              <el-input v-model="form.quantityRatio" placeholder="请输入对象数量比例" />
            </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/task";
import DetailInfo from "./components/DetailInfo";
import { getTask } from "@/api/govermentdata/GovermentData";
import moment from "moment";
export default {
  name: "Infor",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      fUniqueCodes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查任务表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 任务类型 0-专项 1-日常 2-第三方评估抽查 3-随机抽查字典
      fTaskTypeOptions: [],
      // 是否下发 0-不下发 1 - 下发字典
      fDistributeFlagOptions: [],
      // 任务状态，NEW-未开始，ING-进行中，DON-已完结，OVD-超期未完成字典
      fStatusOptions: [],
      // 删除标记，0-可用，1-已删除字典
      fDeleteFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fUniqueCode: null,
        fTaskBelong: null,
        fTaskType: null,
        fDistributeFlag: null,
        fName: null,
        fStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fName: [
          { required: true, message: "任务唯名称不能为空", trigger: "blur" }
        ],
        fUniqueCode: [
          { required: true, message: "任务唯一编码不能为空", trigger: "blur" }
        ],
        fTaskBelong: [
          { required: true, message: "任务归属不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_supervision_task_type").then(response => {
      this.fTaskTypeOptions = response.data;
    });
    this.getDicts("t_distribute_flag").then(response => {
      this.fDistributeFlagOptions = response.data;
    });
    this.getDicts("t_supervision_task_status").then(response => {
      this.fStatusOptions = 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;
      });
    },
    // 任务类型 0-专项 1-日常 2-第三方评估抽查 3-随机抽查字典翻译
    fTaskTypeFormat(row, column) {
      return this.selectDictLabel(this.fTaskTypeOptions, row.fTaskType);
    },
    // 是否下发 0-不下发 1 - 下发字典翻译
    fDistributeFlagFormat(row, column) {
      return this.selectDictLabel(this.fDistributeFlagOptions, row.fDistributeFlag);
    },
    // 任务状态，NEW-未开始，ING-进行中，DON-已完结，OVD-超期未完成字典翻译
    fStatusFormat(row, column) {
      return this.selectDictLabel(this.fStatusOptions, row.fStatus);
    },
    // 删除标记，0-可用，1-已删除字典翻译
    fDeleteFlagFormat(row, column) {
      return this.selectDictLabel(this.fDeleteFlagOptions, row.fDeleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fInsTaskInforId: null,
        fUniqueCode: null,
        fTaskBelong: null,
        fTaskType: null,
        fDistributeFlag: null,
        fName: null,
        fDesc: null,
        fStartTime: null,
        fEndTime: null,
        fStatus: null,
        fDeleteFlag: null,
        fRemark: null,
        fCreateTime: null,
        fCreateBy: null,
        fUpdateTime: null,
        fModifyBy: null,
        inspectionFrequency:null,
        quantityRatio: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.fInsTaskInforId);
      this.fUniqueCodes = selection.map(item => item.fUniqueCode);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检查任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fInsTaskInforId = row.fInsTaskInforId || this.ids
      getInfor(fInsTaskInforId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检查任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fInsTaskInforId != 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 fInsTaskInforIds = row.fInsTaskInforId || this.ids;
      const fUniqueCodes = row.fUniqueCode || this.fUniqueCodes;
      this.$confirm('是否确认删除任务唯一编码为"' + fUniqueCodes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfor(fInsTaskInforIds);
        }).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(() => {});
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fInsTaskInforId);
    },
    //获取数据
    getGovermentTaskInfo(){
      this.$confirm('是否确认从省平台获取数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return getTask({updateTime:moment().format('YYYY-MM-DD HH:mm:ss'),pageIndex: 1,pageSize: 1000});
      }).then(() => {
        this.getList();
        this.msgSuccess("从省平台获取数据成功");
      }).catch(() => {});
    }
  }
};
</script>
