<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="盘点单号" prop="checkCode">
        <el-input
          v-model="queryParams.checkCode"
          placeholder="请输入盘点单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划盘点时间" prop="planTime">
        <el-date-picker clearable
          v-model="queryParams.planTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择计划盘点时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="盘点人员" prop="checkBy">-->
<!--        <el-input-->
<!--          v-model="queryParams.checkBy"-->
<!--          placeholder="请输入盘点人员"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备类型ID，关联dms_base_device_type的device_type_id" prop="deviceTypeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deviceTypeId"-->
<!--          placeholder="请输入设备类型ID，关联dms_base_device_type的device_type_id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="盘点数量" prop="checkAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.checkAmount"-->
<!--          placeholder="请输入盘点数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="剩余数量" prop="surplusCheckAmount">-->
<!--        <el-input-->
<!--          v-model="queryParams.surplusCheckAmount"-->
<!--          placeholder="请输入剩余数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="盘点状态" prop="checkState">
        <el-select
          v-model="queryParams.checkState"
          placeholder="盘点状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.dms_check_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="盘点状态(0未盘点1已盘点2盘点中)" prop="checkState">-->
<!--        <el-input-->
<!--          v-model="queryParams.checkState"-->
<!--          placeholder="请输入盘点状态(0未盘点1已盘点2盘点中)"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </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:checkplan: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:checkplan: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:checkplan: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:checkplan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkplanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="盘点计划ID" align="center" prop="flowCheckplanId" />-->
      <el-table-column label="盘点计划ID" align="center" prop="flowCheckplanId" >
        <template slot-scope="scope">
          <router-link :to="'/dms/detail/index/' + scope.row.flowCheckplanId" class="link-type">
            <span>{{ scope.row.flowCheckplanId }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="盘点单号" align="center" prop="checkCode" />
      <el-table-column label="计划盘点时间" align="center" prop="planTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点人员" align="center" prop="checkBy" />
<!--      <el-table-column label="设备类型ID，关联dms_base_device_type的device_type_id" align="center" prop="deviceTypeId" />-->
      <el-table-column label="设备类型" align="center" prop="deviceTypeId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in devicetypeList"
              :key="index"
              :value="item.devicetypeList"
              v-if="scope.row.deviceTypeId == item.deviceTypeId"
            >
              {{ item.typeName }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="盘点数量" align="center" prop="checkAmount" />
      <el-table-column label="剩余数量" align="center" prop="surplusCheckAmount" />
<!--      <el-table-column label="盘点状态(0未盘点1已盘点2盘点中)" align="center" prop="checkState" />-->
      <el-table-column label="盘点状态" align="center" prop="checkState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_check_state" :value="scope.row.checkState"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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:checkplan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:checkplan: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="盘点单号" prop="checkCode">
          <el-input v-model="form.checkCode" placeholder="请输入盘点单号" />
        </el-form-item>
        <el-form-item label="计划盘点时间" prop="planTime">
          <el-date-picker clearable
            v-model="form.planTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划盘点时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="盘点人员" prop="checkBy">
          <el-input v-model="form.checkBy" placeholder="请输入盘点人员" />
        </el-form-item>
<!--        <el-form-item label="设备类型ID，关联dms_base_device_type的device_type_id" prop="deviceTypeId">-->
<!--          <el-input v-model="form.deviceTypeId" placeholder="请输入设备类型ID，关联dms_base_device_type的device_type_id" />-->
<!--        </el-form-item>-->
        <el-form-item label="设备类型" prop="deviceTypeId" >
          <el-select v-model="form.deviceTypeId" placeholder="请输入设备类型">
            <el-option
              v-for="item in devicetypeList"
              :key="item.deviceTypeId"
              :label="item.typeName"
              :value="item.deviceTypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="盘点数量" prop="checkAmount">
          <el-input v-model="form.checkAmount" placeholder="请输入盘点数量" />
        </el-form-item>
        <el-form-item label="剩余数量" prop="surplusCheckAmount">
          <el-input v-model="form.surplusCheckAmount" placeholder="请输入剩余数量" />
        </el-form-item>
<!--        <el-form-item label="盘点状态(0未盘点1已盘点2盘点中)" prop="checkState">-->
<!--          <el-input v-model="form.checkState" placeholder="请输入盘点状态(0未盘点1已盘点2盘点中)" />-->
<!--        </el-form-item>-->
        <el-form-item label="盘点状态" prop="checkState">
          <el-radio-group v-model="form.checkState">
            <el-radio
              v-for="dict in dict.type.dms_check_state"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">设备盘点明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDmsFlowCheckplanDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDmsFlowCheckplanDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="dmsFlowCheckplanDetailList" :row-class-name="rowDmsFlowCheckplanDetailIndex" @selection-change="handleDmsFlowCheckplanDetailSelectionChange" ref="dmsFlowCheckplanDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="设备编号" prop="deviceId" width="150">
            <template slot-scope="scope">
<!--              <el-input v-model="scope.row.deviceId" placeholder="请输入设备ID。关联dms_base_device_ledger的device_id" />-->
              <el-select v-model="scope.row.deviceId" placeholder="请选择设备">
                <el-option
                  v-for="item in ledgerList"
                  :key="item.deviceId"
                  :label="item.deviceName"
                  :value="item.deviceId">
                </el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="盘点时间" prop="checkTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.checkTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择盘点时间" />
            </template>
          </el-table-column>
          <el-table-column label="盘点结果" prop="variationFlag" width="150">
            <template slot-scope="scope">
<!--              <el-input v-model="scope.row.variationFlag" placeholder="请输入盘点结果(0:默认未盘点,1:已盘点)" />-->
                <el-select v-model="scope.row.variationFlag" placeholder="请选择性别">
                  <el-option
                    v-for="dict in dict.type.dms_variation_flag"
                    :key="dict.value"
                    :label="dict.label"
                    :value="Number(dict.value)"
                  ></el-option>
                </el-select>
            </template>
          </el-table-column>
          <el-table-column label="跳过原因" prop="skipCause" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.skipCause" placeholder="请输入跳过原因" />
            </template>
          </el-table-column>
          <el-table-column label="盘点人" prop="checkPerson" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.checkPerson" placeholder="请输入盘点人" />
            </template>
          </el-table-column>
          <el-table-column label="上次盘点时间" prop="lastCheckTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.lastCheckTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择上次盘点时间" />
            </template>
          </el-table-column>
        </el-table>
      </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 { listCheckplan, getCheckplan, delCheckplan, addCheckplan, updateCheckplan } from "@/api/dms/checkplan";
import { listDevicetype } from '@/api/dms/devicetype'
import { listLedger } from '@/api/dms/ledger'

export default {
  name: "Checkplan",
  dicts: ['dms_check_state','dms_variation_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDmsFlowCheckplanDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //设备类型信息
      devicetypeList:"",
      ledgerList:[],
      // 设备盘点计划表格数据
      checkplanList: [],
      // 设备盘点明细表格数据
      dmsFlowCheckplanDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkCode: null,
        planTime: null,
        checkBy: null,
        TypeName: null,
        deviceTypeId: null,
        checkAmount: null,
        surplusCheckAmount: null,
        checkState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceTypeId: [
          { required: true, message: "设备类型ID，关联dms_base_device_type的device_type_id不能为空", trigger: "blur" }
        ],
        checkState: [
          { required: true, message: "盘点状态(0未盘点1已盘点2盘点中)不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDeviceType();
    this.getDevice();
  },
  methods: {
    //查询设备类型
    getDeviceType(){
      listDevicetype().then(response => {
        this.devicetypeList = response.data;
      });
    },
    //查询设备信息
    getDevice(){
      listLedger().then(response => {
        console.log(response)
        this.ledgerList = response.rows;
      })
    },
    /** 查询设备盘点计划列表 */
    getList() {
      this.loading = true;
      listCheckplan(this.queryParams).then(response => {
        this.checkplanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flowCheckplanId: null,
        checkCode: null,
        planTime: null,
        checkBy: null,
        deviceTypeId: null,
        checkAmount: null,
        surplusCheckAmount: null,
        checkState: null,
        TypeName: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.dmsFlowCheckplanDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flowCheckplanId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备盘点计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flowCheckplanId = row.flowCheckplanId || this.ids
      getCheckplan(flowCheckplanId).then(response => {
        this.form = response.data;
        this.dmsFlowCheckplanDetailList = response.data.dmsFlowCheckplanDetailList;
        this.open = true;
        this.title = "修改设备盘点计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.dmsFlowCheckplanDetailList = this.dmsFlowCheckplanDetailList;
          if (this.form.flowCheckplanId != null) {
            updateCheckplan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckplan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flowCheckplanIds = row.flowCheckplanId || this.ids;
      this.$modal.confirm('是否确认删除设备盘点计划编号为"' + flowCheckplanIds + '"的数据项？').then(function() {
        return delCheckplan(flowCheckplanIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 设备盘点明细序号 */
    rowDmsFlowCheckplanDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 设备盘点明细添加按钮操作 */
    handleAddDmsFlowCheckplanDetail() {
      let obj = {};
      obj.deviceId = "";
      obj.checkTime = "";
      obj.variationFlag = "";
      obj.skipCause = "";
      obj.checkPerson = "";
      obj.lastCheckTime = "";
      this.dmsFlowCheckplanDetailList.push(obj);
    },
    /** 设备盘点明细删除按钮操作 */
    handleDeleteDmsFlowCheckplanDetail() {
      if (this.checkedDmsFlowCheckplanDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的设备盘点明细数据");
      } else {
        const dmsFlowCheckplanDetailList = this.dmsFlowCheckplanDetailList;
        const checkedDmsFlowCheckplanDetail = this.checkedDmsFlowCheckplanDetail;
        this.dmsFlowCheckplanDetailList = dmsFlowCheckplanDetailList.filter(function(item) {
          return checkedDmsFlowCheckplanDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDmsFlowCheckplanDetailSelectionChange(selection) {
      this.checkedDmsFlowCheckplanDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/checkplan/export', {
        ...this.queryParams
      }, `checkplan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
