<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属网格" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :disable-branch-nodes="true" style="width:215px;"
                    placeholder="请选择所属网格"/>
      </el-form-item>
      <el-form-item label="巡查主题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入巡查主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡查人" prop="xcName">
        <el-input
          v-model="queryParams.xcName"
          placeholder="请输入巡查人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡查类别" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="巡查类别"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_xc_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提报日期" prop="submitDate">
        <el-date-picker clearable
          v-model="queryParams.submitDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择提报日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="巡查日期" prop="xcDate">
        <el-date-picker clearable
          v-model="queryParams.xcDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择巡查日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="被巡查对象" prop="xcObj">
        <el-input
          v-model="queryParams.xcObj"
          placeholder="请输入被巡查对象"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="随行巡查人" prop="flowPeople">
        <el-input
          v-model="queryParams.flowPeople"
          placeholder="请输入随行巡查人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否需要整改" prop="reform">
        <el-select
          v-model="queryParams.reform"
          placeholder="巡查类别"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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"
          v-hasPermi="['szxc:xcrecard: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="['szxc:xcrecard: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="['szxc:xcrecard: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="['szxc:xcrecard:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属网格" align="center" prop="deptName" min-width="120" show-overflow-tooltip/>
      <el-table-column label="巡查主题" align="center" prop="title" min-width="120" show-overflow-tooltip/>
      <el-table-column label="巡查人" align="center" prop="xcName" min-width="120" show-overflow-tooltip/>
      <el-table-column label="提报日期" align="center" prop="submitDate" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡查日期" align="center" prop="xcDate" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xcDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡查类别" align="center" prop="type" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_xc_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="被巡查对象" align="center" prop="xcObj" min-width="120" show-overflow-tooltip/>
      <el-table-column label="随行巡查人" align="center" prop="flowPeople" min-width="120" show-overflow-tooltip/>
      <el-table-column label="是否需要整改" align="center" prop="reform" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_yes_no" :value="scope.row.reform"/>
        </template>
      </el-table-column>
      <el-table-column label="整改进度" align="center" prop="zgProgress" min-width="120" show-overflow-tooltip/>
      <el-table-column label="图片" align="center" prop="picture" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="巡查情况描述" align="center" prop="content" min-width="200" show-overflow-tooltip/>
      <el-table-column label="审核状态" align="center" prop="xcStatus" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.xcStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核部门" align="center" prop="auditDept" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核结果" align="center" prop="auditResult" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.auditResult"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditName" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核原因" align="center" prop="auditReason" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核备注" align="center" prop="shRemark" min-width="180" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.xcStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:xcrecard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:xcrecard:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.reform==='0'"
            size="mini"
            type="text"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['szxc:xcrecard:edit']"
          >整改进度</el-button>
          <el-button
            v-if="scope.row.xcStatus==='0'&& ($store.state.user.deptId===scope.row.auditDeptid || $store.state.user.admin)"
            size="mini"
            type="text"
            @click="handleShenHe(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >审核</el-button>
          <el-button
            v-if="scope.row.xcStatus==='2'"
            size="mini"
            type="text"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >重新提交</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSHJL(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >审核记录</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="巡查主题" prop="title">
              <el-input v-model="form.title" placeholder="请输入巡查主题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属网格" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :disable-branch-nodes="true"
                          placeholder="请选择所属网格" style="width:100%;" @select="selectedTree"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="巡查人" prop="xcName">
              <el-input v-model="form.xcName" placeholder="请输入巡查人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提报日期" prop="submitDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.submitDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择提报日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="巡查日期" prop="xcDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.xcDate"
                              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="type">
              <el-select
                v-model="form.type"
                placeholder="巡查类别"
                clearable
                style="width:100%"
              >
                <el-option
                  v-for="dict in dict.type.szxc_xc_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="被巡查对象" prop="xcObj">
              <el-input v-model="form.xcObj" placeholder="请输入被巡查对象" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="随行巡查人" prop="flowPeople">
              <el-input v-model="form.flowPeople" placeholder="请输入随行巡查人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="是否需要整改" prop="reform">
              <el-radio-group v-model="form.reform">
                <el-radio
                  v-for="dict in dict.type.szxc_yes_no"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="图片" prop="picture">
          <image-upload v-model="form.picture"/>
        </el-form-item>
        <el-form-item label="巡查情况描述" prop="content">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </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>

    <!-- 整改进度 -->
    <el-dialog title="整改进度" :visible.sync="zg_open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="整改进度" prop="zgProgress">
          <el-input v-model="form.zgProgress" type="textarea" placeholder="请输入整改进度" />
        </el-form-item>
      </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>

    <!-- 审核对话框 -->
    <el-dialog title="审核" :visible.sync="shenHe.open" width="500px" append-to-body>
      <el-form ref="shenHeForm" :model="shenHe.form" :rules="shenHe.rules" label-width="90px">
        <el-form-item label="审核结果" prop="auditResult">
          <el-select
            v-model="shenHe.form.auditResult"
            placeholder="请选择审核结果"
            clearable
            style="width:100%"
          >
            <el-option label="通过" value="1"/>
            <el-option label="不通过" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="审核原因" prop="auditReason">
          <el-input v-model="shenHe.form.auditReason" type="textarea" placeholder="请输入审核原因" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="shenHe.form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shenHeSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核记录" :visible.sync="shjl.visible" width="1000px" append-to-body>
      <el-table v-loading="shjl.loading" :data="shjl.list" border>
        <el-table-column label="审核部门" align="center" prop="auditDept" />
        <el-table-column label="审核人" align="center" prop="auditName" />
        <el-table-column label="审核结果" align="center" prop="auditResult" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.auditResult"/>
          </template>
        </el-table-column>
        <el-table-column label="审核原因" align="center" prop="auditReason" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listXcrecard, getXcrecard, delXcrecard, addXcrecard, updateXcrecard } from "@/api/szxc/xcrecard";
import { listXyrecard, addXyrecard } from "@/api/szxc/xyrecard";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Xcrecard",
  components: { Treeselect },
  dicts: ['szxc_yes_no', 'szxc_audit_status', 'szxc_xc_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡查记录表格数据
      list: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示整改进度弹出层
      zg_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xcName: null,
        submitDate: null,
        xcDate: null,
        type: null,
        jmId: null,
        xcObj: null,
        title: null,
        picture: null,
        content: null,
        flowPeople: null,
        reform: null,
        zgProgress: null,
        xcStatus: null,
        shRemark: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "巡查主题不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属网格不能为空", trigger: "blur" }
        ],
        xcName: [
          { required: true, message: "巡查人不能为空", trigger: "blur" }
        ],
        submitDate: [
          { required: true, message: "提报日期不能为空", trigger: "blur" }
        ],
        xcDate: [
          { required: true, message: "巡查日期不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "巡查类别不能为空", trigger: "blur" }
        ],
        xcObj: [
          { required: true, message: "被巡查对象不能为空", trigger: "blur" }
        ],
        flowPeople: [
          { required: true, message: "随行巡查人不能为空", trigger: "blur" }
        ],
        reform: [
          { required: true, message: "是否需要整改不能为空", trigger: "blur" }
        ],
        xcStatus: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        picture: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "巡查情况描述不能为空", trigger: "blur" }
        ],
        shRemark: [
          { required: true, message: "审核备注不能为空", trigger: "blur" }
        ],
        zgProgress: [
          { required: true, message: "整改进度不能为空", trigger: "blur" }
        ],
      },
      // 审核
      shenHe:{
        row: null,
        // 是否显示审核弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          auditResult: [
            { required: true, message: "审核结果不能为空", trigger: "blur" }
          ],
          auditReason: [
            { required: true, message: "审核原因不能为空", trigger: "blur" }
          ],
        }
      },
      // 审核记录信息
      shjl: {
        visible: false,
        // 遮罩层
        loading: false,
        // 查询列表
        list: [],
        // 选中信息
        selectedRow: null,
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询组织下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    selectedTree(node){
      this.form.deptId = node.id;
      this.form.deptName = node.label;
    },
    /** 查询巡查记录列表 */
    getList() {
      this.loading = true;
      listXcrecard(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xcName: null,
        submitDate: null,
        xcDate: null,
        type: null,
        jmId: null,
        xcObj: null,
        title: null,
        picture: null,
        content: null,
        flowPeople: null,
        reform: null,
        zgProgress: null,
        xcStatus: null,
        shRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: null,
        userId: 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.reset();
      this.open = true;
      this.title = "添加巡查记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getXcrecard(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡查记录";
      });
    },
    /** 整改进度按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids
      getXcrecard(id).then(response => {
        this.form = response.data;
        this.zg_open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateXcrecard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.zg_open = false;
              this.getList();
            });
          } else {
            this.form.xcStatus = '0'
            addXcrecard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除巡查记录编号为"' + ids + '"的数据项？').then(function() {
        return delXcrecard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/xcrecard/export', {
        ...this.queryParams
      }, `xcrecard_${new Date().getTime()}.xlsx`)
    },
    /** 审核按钮操作 */
    handleShenHe(row) {
      this.shenHe.row = row;
      this.resetForm("shenHeForm");
      this.shenHe.form = {
        id: null,
        shRecardType: '2',
        jlId: row.id,
        auditDept: row.auditDept,
        auditName: this.$store.state.user.nickName,
        auditResult: null,
        auditReason: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.shenHe.open = true
    },
    // 审核提交
    shenHeSubmit(){
      this.$refs["shenHeForm"].validate(valid => {
        if (valid) {
          this.shenHe.row.auditName = this.shenHe.form.auditName;
          this.shenHe.row.auditResult =  this.shenHe.form.auditResult;
          this.shenHe.row.auditReason =  this.shenHe.form.auditReason;
          this.shenHe.row.shRemark =  this.shenHe.form.remark;
          if(this.shenHe.form.auditResult==='1'){
            this.shenHe.row.xcStatus = '1'
          }
          if(this.shenHe.form.auditResult==='2'){
            this.shenHe.row.xcStatus = '2'
          }
          updateXcrecard(this.shenHe.row).then(response1 => {
            addXyrecard(this.shenHe.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.shenHe.open = false;
              this.getList();
            });
          });
        }
      });
    },
    // 审核记录信息
    handleSHJL(row){
      this.shjl.selectedRow = row;
      this.shjl.visible = true;
      this.getlistXyrecard();
    },
    /** 查询审核记录信息列表 */
    getlistXyrecard() {
      listXyrecard({
        pageNum: 1,
        pageSize: 100000000,
        shRecardType: '2',
        jlId: this.shjl.selectedRow.id
      }).then(response => {
        this.shjl.list = response.rows
      });
    },
    /** 重新提交按钮操作 */
    handleUpdate2(row) {
      this.reset();
      const id = row.id
      getXcrecard(id).then(response => {
        this.form = response.data;
        this.form.xcStatus = '0';
        this.form.auditName = '';
        this.form.auditResult = '';
        this.form.auditReason = '';
        this.form.shRemark = '';
        this.open = true;
        this.title = "修改巡查记录";
      });
    },
  }
};
</script>
