<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <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="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入工作人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="startDate">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </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:workrecard: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:workrecard: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:workrecard: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:workrecard: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="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" min-width="60" show-overflow-tooltip/>
      <el-table-column label="标题" align="center" prop="title" min-width="180" show-overflow-tooltip/>
      <el-table-column label="所属网格" align="center" prop="deptName" min-width="120" show-overflow-tooltip/>
      <el-table-column label="工作人员" align="center" prop="workerName" min-width="100" show-overflow-tooltip/>
      <el-table-column label="提报日期" align="center" prop="submitDate" min-width="100" 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="startDate" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center" prop="type" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_gzjl_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <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="180" show-overflow-tooltip/>
      <el-table-column label="审核状态" align="center" prop="gzStatus" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.gzStatus"/>
        </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.gzStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:workrecard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:workrecard:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.gzStatus==='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.gzStatus==='2'"
            size="mini"
            type="text"
            @click="handleUpdate1(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="900px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <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="workerName">
              <el-input v-model="form.workerName" 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="startDate">
            <el-date-picker clearable style="width: 100%"
                            v-model="form.startDate"
                            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="endDate">
            <el-date-picker clearable style="width: 100%"
                            v-model="form.endDate"
                            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="type">
              <el-select
                         v-model="form.type"
                         placeholder="类别"
                         clearable
                         style="width:100%"
              >
                <el-option
                  v-for="dict in dict.type.szxc_gzjl_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="24">
            <el-form-item label="图片" prop="picture">
              <image-upload v-model="form.picture"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <editor v-model="form.content" :min-height="192"/>
            </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>

    <!-- 审核对话框 -->
    <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 { listWorkrecard, getWorkrecard, delWorkrecard, addWorkrecard, updateWorkrecard } from "@/api/szxc/workrecard";
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: "Workrecard",
  components: { Treeselect },
  dicts: ['szxc_gzjl_type', 'szxc_audit_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作记录表格数据
      list: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workerName: null,
        submitDate: null,
        startDate: null,
        endDate: null,
        type: null,
        title: null,
        picture: null,
        content: null,
        gzStatus: null,
        shRemark: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属网格不能为空", trigger: "blur" }
        ],
        workerName: [
          { required: true, message: "工作人员不能为空", trigger: "blur" }
        ],
        submitDate: [
          { required: true, message: "提报日期不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类别不能为空", trigger: "blur" }
        ],
        gzStatus: [
          { required: true, message: "审核状态不能为空", trigger: "blur" }
        ],
        picture: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
        shRemark: [
          { 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;
      listWorkrecard(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,
        workerName: null,
        submitDate: null,
        startDate: null,
        endDate: null,
        type: null,
        title: null,
        picture: null,
        content: null,
        gzStatus: 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
      getWorkrecard(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) {
            updateWorkrecard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.gzStatus = '0'
            addWorkrecard(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 delWorkrecard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/workrecard/export', {
        ...this.queryParams
      }, `workrecard_${new Date().getTime()}.xlsx`)
    },
    /** 审核按钮操作 */
    handleShenHe(row) {
      this.shenHe.row = row;
      this.resetForm("shenHeForm");
      this.shenHe.form = {
        id: null,
        shRecardType: '0',
        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.gzStatus = '1'
          }
          if(this.shenHe.form.auditResult==='2'){
            this.shenHe.row.gzStatus = '2'
          }
          updateWorkrecard(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: '0',
        jlId: this.shjl.selectedRow.id
      }).then(response => {
        this.shjl.list = response.rows
      });
    },
    /** 重新提交按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id
      getWorkrecard(id).then(response => {
        this.form = response.data;
        this.form.gzStatus = '0';
        this.form.auditName = '';
        this.form.auditResult = '';
        this.form.auditReason = '';
        this.form.shRemark = '';
        this.open = true;
        this.title = "修改工作记录";
      });
    },
  }
};
</script>
