<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="backUnit">
        <el-select
          v-model="queryParams.backUnit"
          placeholder="请选择单位名称"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in unitInfoSelectList"
            :key="dict.unitId"
            :label="dict.unitName"
            :value="dict.unitId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工程名称" prop="backPro">
        <el-select
          v-model="queryParams.backPro"
          placeholder="请选择工程名称"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in projectSelectList"
            :key="dict.proId"
            :label="dict.proName"
            :value="dict.proId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工机具类型" prop="type">
        <selectTree
          ref="mychild"
          style="width: 240px"
          :treeList="deptList"
          :treeProps="{
            children: 'children',
            label: 'label',
          }"
          nodeKey="id"
          :defaultSelect="true"
          :defaultData="defaultData"
          :clearable="false"
          :placeholder="'请选择'"
          @handleNodeClick="selectDrop"
        ></selectTree>
      </el-form-item>
      <el-form-item label="报废单号" prop="backCode">
        <el-input
          v-model="queryParams.backCode"
          placeholder="请选择报废单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修状态" prop="repairStatus">
        <el-select
          v-model="queryParams.repairStatus"
          placeholder="请选择维修状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dicSelectList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="任务创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-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" style="margin-left: 3%">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          v-hasPermi="['store:labelType:add']"
          @click="checkClick"
          >批量审核</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:labelType:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" sortable align="center" type="index" />
      <el-table-column
        label="报废审核单号"
        align="center"
        sortable
        prop="scrapNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="单位名称"
        align="center"
        prop="unitName"
        sortable
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工程名称"
        sortable
        align="center"
        prop="projectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="机具类型"
        align="center"
        prop="itemType"
        sortable
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="任务创建人"
        sortable
        align="center"
        prop="createBy"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="任务创建时间"
        sortable
        align="center"
        prop="createTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="报废单号"
        align="center"
        sortable
        prop="repairNum"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="审核状态"
        align="center"
        sortable
        prop="taskStatusName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="不通过原因"
        align="center"
        sortable
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-zoom-in"
            @click="handleSee(scope.row, 'see')"
            v-hasPermi="['store:labelType:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-circle-check"
            @click="handleUpdate(scope.row, 'update')"
            v-hasPermi="['store:labelType: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="dialogTitle"
      :visible.sync="open"
      width="1000px"
      append-to-body
      :before-close="onClose"
    >
      <el-form
        :model="dialogQueryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="100px"
      >
        <el-row>
          <el-form-item label="关键字" prop="keyword">
            <el-input
              v-model="dialogQueryParams.keyword"
              placeholder="请输入关键字"
              clearable
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item label="类型名称" prop="typeName">
            <!-- <treeselect
              style="width: 250px"
              v-model="dialogQueryParams.typeName"
              :options="deptList"
              :normalizer="normalizer"
              placeholder="请选择类型名称"
            /> -->
            <selectTree
              ref="mychildDialog"
              style="width: 240px"
              :treeList="deptList"
              :treeProps="{
                children: 'children',
                label: 'label',
              }"
              nodeKey="id"
              :defaultSelect="true"
              :defaultData="defaultData"
              :clearable="false"
              :placeholder="'请选择'"
              @handleNodeClick="selectDropDialog"
            ></selectTree>
          </el-form-item>
          <el-form-item label="规格型号" prop="type">
            <!-- <treeselect
              style="width: 250px"
              v-model="dialogQueryParams.type"
              :options="deptTypeList"
              :normalizer="normalizerType"
              placeholder="请选择类型名称"
            /> -->
            <selectTree
              ref="mychildDialogTwo"
              style="width: 240px"
              :treeList="deptTypeList"
              :treeProps="{
                children: 'children',
                label: 'label',
              }"
              nodeKey="id"
              :defaultSelect="true"
              :defaultData="defaultData"
              :clearable="false"
              :placeholder="'请选择'"
              @handleNodeClick="selectDropDialogTwo"
            ></selectTree>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getdialogList"
              >查询</el-button
            >
            <el-button
              icon="el-icon-refresh"
              size="mini"
              @click="resetQueryDialog"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>

      <el-table
        v-loading="loading"
        :data="dialogList"
        height="500px"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" sortable align="center" type="index" />
        <el-table-column
          label="类型名称"
          align="center"
          prop="machineTypeName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="规格型号"
          align="center"
          prop="specificationType"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="编码"
          align="center"
          prop="dictName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="维修总量"
          align="center"
          prop="repairNum"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="维修数量"
          align="center"
          prop="repairedNum"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="报废数量"
          align="center"
          prop="scrapNum"
          :show-overflow-tooltip="true"
        />
      </el-table>

      <pagination
        v-show="dialogTotal > 0"
        :total="dialogTotal"
        :page.sync="dialogQueryParams.pageNum"
        :limit.sync="dialogQueryParams.pageSize"
        @pagination="getList"
      />

      <div
        slot="footer"
        class="dialog-footer-btn"
        style="text-align: center"
        v-if="type == 'update' && dialogList.length !== 0"
      >
        <el-button type="primary" @click="submitForm(1)">通过</el-button>
        <el-button @click="returnSubmit(1)">不通过</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-loading.fullscreen.lock="fullscreenLoading"
      :title="title"
      :visible.sync="openOne"
      append-to-body
      width="400px"
    >
      <div class="submit_box">
        <div>
          <i
            class="el-icon-circle-check"
            style="color: #ff9900; font-size: 30px"
          ></i>
        </div>
        <div class="submit_box_title">
          <div>{{ openTextOne }}</div>
          <div>{{ openTextTwo }}</div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitOpenOneFeturn">返回</el-button>
        <el-button type="primary" @click="submitOpenOneForm()">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-loading.fullscreen.lock="fullscreenLoading"
      :title="title"
      :visible.sync="openTwo"
      append-to-body
      width="400px"
    >
      <div class="submit_box_two">
        <div>
          <i
            class="el-icon-circle-check"
            style="color: #00c196; font-size: 30px"
          ></i>
        </div>
        <div class="submit_box_title">{{ openTextThree }}</div>
      </div>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
    <el-dialog
      v-loading.fullscreen.lock="fullscreenLoading"
      :title="title"
      :visible.sync="openThree"
      append-to-body
      width="800px"
    >
      <div class="submit_box">
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
          <el-form-item label="请输入驳回原因：" prop="remark">
            <el-input
              style="width: 600px"
              type="textarea"
              v-model="form.remark"
              maxlength="200"
              show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formCancel">返回</el-button>
        <el-button type="primary" @click="formSubmit">保存</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-loading.fullscreen.lock="fullscreenLoading"
      :title="title"
      :visible.sync="openFour"
      append-to-body
      width="400px"
    >
      <div class="submit_box_openFour">
        你选择了{{ selectionList.length }}条数据,请进行审批
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm(2)" type="primary">通过</el-button>
        <el-button @click="returnSubmit(2)">不通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getQuestListApi, getRepairAuditListApi, addDetailsAuditApi } from "@/api/repairTest/brokenExamine";
import {
  getUnitInfoSelectApi, getProjectSelectApi, getDicSelectApi, listPartTypeApi, getMaTypeSelectApi
} from "@/api/repairTest/repair";
import selectTree from '../repair/selectTree.vue'
export default {
  name: "Dict",
  components: { selectTree },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      defaultData: null,
      selectionList: [],
      unitInfoSelectList: [],
      projectSelectList: [],
      dicSelectList: [],
      dialogList: [],
      deptList: [],
      deptTypeList: [],
      rowObj: {},
      fullscreenLoading: false,
      openOne: false,
      openTwo: false,
      openThree: false,
      openFour: false,
      openTextOne: '',
      openTextTwo: '',
      openTextThree: '',
      type: '',
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dialogTotal: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      dialogTitle: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      dialogQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        remark: [
          { required: true, message: "原因不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    getUnitInfoSelectApi().then(res => {
      this.unitInfoSelectList = res.data
    })
    getProjectSelectApi().then(res => {
      this.projectSelectList = res.data
    })
    getDicSelectApi({ value: 'wx_task' }).then(res => {
      this.dicSelectList = res.data
    })
    this.getTree()
  },
  updated() {
    this.$refs.mychild.clearSelect();
    this.defaultData = null
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getQuestListApi(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 封装弹框页面查询
    getdialogList() {
      let params = {
        taskId: this.rowObj.taskId,
        pageNum: this.dialogQueryParams.pageNum,
        pageSize: this.dialogQueryParams.pageSize,
      }
      this.loading = true;
      getRepairAuditListApi(params).then(response => {
        if (response.code == 200) {
          this.loading = false;
          this.open = true
          this.dialogList = response.rows
          this.dialogTotal = response.total;
        }
      }).catch(() => { });
    },
    checkClick() {
      this.title = '审批';
      this.openFour = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    onClose() {
      this.open = false
      this.queryParams.type = ''
      this.resetQueryDialog()
      this.getList()
    },
    resetQueryDialog() {
      this.dialogQueryParams = {};
      this.resetForm("dialogQueryParams");
      this.$refs.mychildDialog.clearSelect();
      this.$refs.mychildDialogTwo.clearSelect();
    },
    selectDrop(value) {
      this.queryParams.type = value
    },
    selectDropDialog(value) {
      this.dialogQueryParams.typeName = value
    },
    selectDropDialogTwo(value) {
      this.dialogQueryParams.type = value
    },
    getTree() {
      listPartTypeApi().then(response => {
        this.deptList = response.data
      });
      getMaTypeSelectApi().then(response => {
        this.deptTypeList = response.data
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.mychild.clearSelect();
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionList = selection
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    handleSee(row, type) {
      this.dialogTitle = '查看'
      this.rowObj = row
      this.reset();
      this.type = type
      this.getdialogList()
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.dialogTitle = '审核'
      this.rowObj = row
      this.reset();
      this.type = type
      this.getdialogList()
    },
    /** 提交按钮 */
    submitForm(val) {
      // val 1代表对话框中审核   2代表批量审核
      this.checkResult = val
      this.openOne = true
      this.openFour = false
      this.openTextOne = '确定审批通过么?'
      this.openTextTwo = '确定审批通过么?'
    },
    submitOpenOneForm() {
      this.openOne = false
      // val 1代表对话框中审核   2代表批量审核
      if (this.checkResult == 1) {
        let taskIdList = []
        taskIdList.push(this.rowObj.taskId)
        let params = {
          checkResult: '通过',
          taskIdList: taskIdList
        }
        addDetailsAuditApi(params).then(res => {
          if (res.code == 200) {
            this.openTwo = true
            this.openTextThree = '审批通过！'
            this.getdialogList()
          }
        }).catch(() => { })
      }
      if (this.checkResult == 2) {
        let taskIdList = []
        this.selectionList.map(item => {
          taskIdList.push(item.taskId)
        })
        let params = {
          checkResult: '通过',
          taskIdList: taskIdList
        }
        addDetailsAuditApi(params).then(res => {
          if (res.code == 200) {
            this.openTwo = true
            this.openTextThree = '审批通过！'
            this.getList()
          }
        }).catch(() => { })
      }
    },

    submitOpenOneFeturn() {
      this.openOne = false
    },
    returnSubmit(val) {
      this.checkResult = val
      this.title = '驳回原因'
      this.openThree = true
    },
    formCancel() {
      this.$refs.form.resetFields();
      this.openThree = false
    },
    formSubmit() {
      this.$refs.form.validate(valid => {
        console.log('this.checkResult', this.checkResult);
        if (valid) {
          if (this.checkResult == 1) {
            let taskIdList = []
            taskIdList.push(this.rowObj.taskId)
            let params = {
              checkResult: '不通过',
              taskIdList: taskIdList,
              remark: this.form.remark
            }
            addDetailsAuditApi(params).then(res => {
              if (res.code == 200) {
                this.$refs.form.resetFields();
                this.formCancel()
                this.getdialogList()
              }
            }).catch(() => { })
          }
          if (this.checkResult == 2) {
            let taskIdList = []
            this.selectionList.map(item => {
              taskIdList.push(item.taskId)
            })
            let params = {
              checkResult: '不通过',
              taskIdList: taskIdList,
              remark: this.form.remark
            }
            addDetailsAuditApi(params).then(res => {
              if (res.code == 200) {
                this.$refs.form.resetFields();
                this.openThree = false
                this.openFour = false
                this.getList()
              }
            }).catch(() => { })
          }
        } else {
          return false
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style lang="scss" scoped>
.submit_box {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  .submit_box_title {
    display: flex;
    flex-direction: column;
    margin-left: 15px;

    :first-child {
      font-size: 14px;
      font-weight: 600;
    }

    :last-child {
      margin-top: 6px;
      font-size: 12px;
    }
  }
}

.submit_box_two {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  margin-left: 40%;

  .submit_box_title {
    display: flex;
    flex-direction: column;
    margin-left: 10px;
    font-size: 18px;
    font-weight: 600;
  }
}
::v-deep.el-table .fixed-width .el-button--mini {
  width: 70px !important;
  margin-bottom: 6px;
}
.dialog-footer-btn {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
.submit_box_openFour {
  display: flex;
  justify-content: center;
  align-content: center;
  font-size: 18px;
}
</style>
