<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="盘点计划ID" prop="flowCheckplanId">-->
<!--        <el-input-->
<!--          v-model="queryParams.flowCheckplanId"-->
<!--          placeholder="请输入盘点计划ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备ID" prop="deviceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deviceId"-->
<!--          placeholder="请输入设备ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="盘点时间" prop="checkTime">
        <el-date-picker clearable
          v-model="queryParams.checkTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择盘点时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="盘点结果(0:默认未盘点,1:已盘点)" prop="variationFlag">-->
<!--        <el-input-->
<!--          v-model="queryParams.variationFlag"-->
<!--          placeholder="请输入盘点结果(0:默认未盘点,1:已盘点)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="盘点结果" prop="checkState">
        <el-select
          v-model="queryParams.dms_variation_flag"
          placeholder="盘点状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.dms_variation_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="跳过原因" prop="skipCause">-->
<!--        <el-input-->
<!--          v-model="queryParams.skipCause"-->
<!--          placeholder="请输入跳过原因"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="盘点人" prop="checkPerson">-->
<!--        <el-input-->
<!--          v-model="queryParams.checkPerson"-->
<!--          placeholder="请输入盘点人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="上次盘点时间" prop="lastCheckTime">
        <el-date-picker clearable
          v-model="queryParams.lastCheckTime"
          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="['dms:detail: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="['dms:detail: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="['dms:detail: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="['dms:detail:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="checkplanDetailId" />-->
<!--      <el-table-column label="盘点计划ID" align="center" prop="flowCheckplanId" />-->
<!--      <el-table-column label="设备ID" align="center" prop="deviceId" />-->
      <el-table-column label="设备名称" align="center" prop="deviceId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in ledgerList"
              :key="index"
              :value="item.ledgerList"
              v-if="scope.row.deviceId == item.deviceId"
            >
              {{ item.deviceName }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="盘点时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="盘点结果(0:默认未盘点,1:已盘点)" align="center" prop="variationFlag" />-->
      <el-table-column label="盘点结果" align="center" prop="variationFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_variation_flag" :value="scope.row.variationFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="跳过原因" align="center" prop="skipCause" />
      <el-table-column label="盘点人" align="center" prop="checkPerson" />
      <el-table-column label="上次盘点时间" align="center" prop="lastCheckTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastCheckTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:detail:remove']"
          >删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="盘点计划ID" prop="flowCheckplanId">-->
<!--          <el-input v-model="form.flowCheckplanId" placeholder="请输入盘点计划ID" :disabled=isDis />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备ID。关联dms_base_device_ledger的device_id" prop="deviceId">-->
<!--          <el-input v-model="form.deviceId" placeholder="请输入设备ID。关联dms_base_device_ledger的device_id" />-->
<!--        </el-form-item>-->
        <el-form-item label="设备名称" prop="deviceId" >
          <el-select v-model="form.deviceId" placeholder="请选择设备" >
            <el-option
              v-for="item in ledgerList"
              :key="item.deviceId"
              :label="item.deviceName"
              :value="item.deviceId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点时间" prop="checkTime">
          <el-date-picker clearable
            v-model="form.checkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择盘点时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="盘点结果(0:默认未盘点,1:已盘点)" prop="variationFlag">-->
<!--          <el-input v-model="form.variationFlag" placeholder="请输入盘点结果(0:默认未盘点,1:已盘点)" />-->
<!--        </el-form-item>-->
        <el-form-item label="盘点结果" prop="variationFlag">
          <el-radio-group v-model="form.variationFlag">
            <el-radio
              v-for="dict in dict.type.dms_variation_flag"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="跳过原因" prop="skipCause">
          <el-input v-model="form.skipCause" placeholder="请输入跳过原因" :disabled=isDis />
        </el-form-item>
        <el-form-item label="盘点人" prop="checkPerson">
          <el-input v-model="form.checkPerson" placeholder="请输入盘点人":disabled=isDis  />
        </el-form-item>
        <el-form-item label="上次盘点时间" prop="lastCheckTime">
          <el-date-picker clearable
            v-model="form.lastCheckTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次盘点时间">
          </el-date-picker>
        </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>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail } from "@/api/dms/detail";
import { listLedger } from '@/api/dms/ledger'

export default {
  name: "Detail",
  dicts: ['dms_variation_flag'],
  data() {
    return {
      flowCheckplanIdCheck:"",
      //修改标识
      isDis:false,
      //设备集合
      ledgerList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备盘点明细表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowCheckplanId: null,
        deviceId: null,
        checkTime: null,
        variationFlag: null,
        skipCause: null,
        checkPerson: null,
        lastCheckTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flowCheckplanId: [
          { required: true, message: "盘点计划ID，关联dms_flow_checkplan的flow_checkplan_id不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备ID。关联dms_base_device_ledger的device_id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const flowCheckplanId = this.$route.params && this.$route.params.flowCheckplanId;
    this.queryParams.flowCheckplanId = flowCheckplanId;
    this.form.flowCheckplanId = flowCheckplanId;
    this.flowCheckplanIdCheck = flowCheckplanId;
    this.getDeviceLedger();
    this.getList(flowCheckplanId);

  },
  methods: {
    /** 查询设备台账信息列表 */
    getDeviceLedger() {
      this.loading = true;
      listLedger(this.queryParams).then(response => {
        console.log(response)
        this.ledgerList = response.rows;
      });
    },
    /** 查询设备盘点明细列表 */
    getList() {
      this.loading = true;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkplanDetailId: null,
        flowCheckplanId: null,
        deviceId: null,
        checkTime: null,
        variationFlag: null,
        skipCause: null,
        checkPerson: null,
        lastCheckTime: 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.checkplanDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isDis = false;
      this.open = true;
      this.title = "添加设备盘点明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isDis = true;
      const checkplanDetailId = row.checkplanDetailId || this.ids
      getDetail(checkplanDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备盘点明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkplanDetailId != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.flowCheckplanId = this.flowCheckplanIdCheck;
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkplanDetailIds = row.checkplanDetailId || this.ids;
      this.$modal.confirm('是否确认删除设备盘点明细编号为"' + checkplanDetailIds + '"的数据项？').then(function() {
        return delDetail(checkplanDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
