<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="returnNumber">
        <el-input
            v-model="queryParams.returnNumber"
            placeholder="请输入退料单号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库单号" prop="outNumber">
        <el-input
          v-model="queryParams.outNumber"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修单号" prop="repairNumber">
        <el-input
          v-model="queryParams.repairNumber"
          placeholder="请输入报修单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input
          v-model="queryParams.plateNumber"
          placeholder="请输入车牌号"
          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="['business:pickingReturn:add']"
        >新增</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="['business:pickingReturn: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="['business:pickingReturn:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialPickingReturnList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="退料单号" align="center" prop="returnNumber" />
      <el-table-column label="出库单号" align="center" prop="outNumber" />
      <el-table-column label="报修单号" align="center" prop="repairNumber" />
      <el-table-column label="车牌号" align="center" prop="plateNumber" />
      <el-table-column label="出库金额" align="center" prop="outMoney" />
      <el-table-column label="退料时间" align="center" prop="returnTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outTime, '{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"-->
<!--            :disabled="scope.row.state === 2"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:pickingReturn:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:pickingReturn:remove']"-->
<!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</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="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <span style="font-weight: bold; font-size: 18px">退料信息</span>
        <el-divider />
        <el-row>
          <el-col :span="6">
            <el-form-item label="退货单号" prop="returnNumber">
              <el-input v-model="form.returnNumber" disabled placeholder="自动生成" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="报修单" prop="repairFormId">
              <el-select v-model="form.repairFormId" placeHolder="请选择报修单" @change="repairFormChange">
                <el-option v-for="item in repairFormOpt" :key="item.id" :value="item.id" :label="item.repairNumber" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="出库单号" prop="outNumber">
              <el-select v-model="form.outNumber" placeHolder="请选择出库单号" @change="outNumberChange">
                <el-option v-for="item in pickingOpt" :key="item.id" :value="item.id" :label="item.outNumber" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="退料金额" prop="outMoney">
              <el-input v-model="form.outMoney" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="客户名称" prop="clientName">
              <el-input v-model="form.clientName" disabled  />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="车牌号" prop="plateNumber">
              <el-input v-model="form.plateNumber" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门" prop="repairerDeptName">
              <el-input v-model="form.repairerDeptName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="班组" prop="repairerGroupName">
              <el-input v-model="form.repairerGroupName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="操作人" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入操作人" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="操作时间" prop="createTime">
              <el-date-picker v-model="form.createTime" type="datetime" style="width: 100%" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择操作时间" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span style="font-weight: bold; font-size: 18px">退料明细</span>
      <el-divider />
      <el-table :data="form.pickingInfoList">
<!--        <el-table-column label="分类名称" align="center" prop="materialTypeName" />-->
<!--        <el-table-column label="货位类型" align="center" prop="warehouseLocationType" />-->
        <el-table-column label="供应商" align="center" prop="vendorName" />
        <el-table-column label="物资编码" align="center" prop="materialCode" />
        <el-table-column label="物资名称" align="center" prop="materialName" />
        <el-table-column label="物资规格" align="center" prop="materialSpecifications" />
        <el-table-column label="物资单位" align="center" prop="materialUnit" />
        <el-table-column label="质保期" align="center" prop="materialWarrantyPeriod" />
        <el-table-column label="退料数量" align="center" prop="collectNumber" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open1" width="80%" append-to-body>
      <span style="font-weight: bold; font-size: 18px">退料信息</span>
      <el-divider />
      <el-descriptions border :column="3">
        <el-descriptions-item label="退货单号">{{form.returnNumber}}</el-descriptions-item>
        <el-descriptions-item label="报修单号">{{form.repairNumber}}</el-descriptions-item>
        <el-descriptions-item label="出库单号">{{form.outNumber}}</el-descriptions-item>
        <el-descriptions-item label="车牌号">{{form.plateNumber}}</el-descriptions-item>
        <el-descriptions-item label="退料金额">{{form.outMoney}}</el-descriptions-item>
        <el-descriptions-item label="退料时间">{{form.returnTime}}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{form.clientName}}</el-descriptions-item>
        <el-descriptions-item label="部门">{{form.repairerDeptName}}</el-descriptions-item>
        <el-descriptions-item label="班组">{{form.repairerGroupName}}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{form.createBy}}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{form.createTime}}</el-descriptions-item>
      </el-descriptions>
      <span style="font-weight: bold; font-size: 18px">退料明细</span>
      <el-divider />
      <el-table :data="form.pickingInfoList">
<!--        <el-table-column label="分类名称" align="center" prop="materialTypeName" />-->
<!--        <el-table-column label="货位类型" align="center" prop="warehouseLocationType" />-->
        <el-table-column label="供应商" align="center" prop="vendorName" />
        <el-table-column label="物资编码" align="center" prop="materialCode" />
        <el-table-column label="物资名称" align="center" prop="materialName" />
        <el-table-column label="物资规格" align="center" prop="materialSpecifications" />
        <el-table-column label="物资单位" align="center" prop="materialUnit" />
        <el-table-column label="质保期" align="center" prop="materialWarrantyPeriod" />
        <el-table-column label="退料数量" align="center" prop="collectNumber" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMaterialPickingReturn,
  delMaterialPicking,
  getMaterialPicking,
  listMaterialPicking
} from '@/api/business/materialPicking'
import {getRepairForm, listRepairForm} from '@/api/business/repairForm'
import {formatDate} from '@/utils'

export default {
  name: "MaterialPicking",
  dicts: ['sys_is_enable', 'picking_state'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料出库表格数据
      materialPickingReturnList: [],
      repairFormOpt: [],
      pickingOpt: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        state: 3,
        repairFormId: undefined,
        repairNumber: undefined,
        outNumber: undefined,
        returnNumber: undefined,
        carId: undefined,
        plateNumber: undefined,
        outMoney: undefined,
        receiveUserId: undefined,
        receiveUserName: undefined,
        receiveTime: undefined,
        outTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        repairFormId: [
          { required: true, message: "报修单不能为空", trigger: "blur" }
        ],
        outNumber: [
          { required: true, message: "出库单不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料出库列表 */
    getList() {
      this.loading = true;
      listMaterialPicking(this.queryParams).then(response => {
        this.materialPickingReturnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询报修单列表 */
    getRepairList() {
      listRepairForm({pageNum: 1, pageSize: 100, state: 1}).then(response => {
        this.repairFormOpt = response.rows;
      });
    },
    // 报修单更改
    repairFormChange(id) {
      getRepairForm(id).then(res =>{
        this.form.clientName = res.data.clientName
        this.form.repairerDeptName = res.data.repairerDeptName
        this.form.repairerGroupName = res.data.repairerGroupName
        this.form.plateNumber = res.data.car.plateNumber
        this.form.carId = res.data.carId;
        this.getPickingList(id)
      })
    },
    /** 查询物料出库列表 */
    getPickingList(repairFormId) {
      listMaterialPicking({ pageNum: 1, pageSize: 100, stateList:[1, 2], repairFormId }).then(response => {
        this.pickingOpt = response.rows;
      });
    },
    /** 出库单更改 */
    outNumberChange(id) {
      getMaterialPicking(id).then(response => {
        this.form.id = response.data.id;
        this.form.outMoney = response.data.outMoney;
        this.form.outTime = response.data.outTime;
        this.form.pickingInfoList = response.data.pickingInfoList;
      });
    },
    // 详情按钮
    handleDetail(row) {
      getMaterialPicking(row.id).then(response => {
        this.form.id = response.data.id;
        this.form.returnNumber = response.data.returnNumber;
        this.form.repairNumber = response.data.repairNumber;
        this.form.outNumber = response.data.outNumber;
        this.form.returnTime = response.data.returnTime;
        this.form.outMoney = response.data.outMoney;
        this.form.outTime = response.data.outTime;
        this.form.createBy = response.data.createBy;
        this.form.createTime = response.data.createTime;
        this.form.pickingInfoList = response.data.pickingInfoList;
        getRepairForm(response.data.repairFormId).then(res =>{
          this.form.clientName = res.data.clientName
          this.form.repairerDeptName = res.data.repairerDeptName
          this.form.repairerGroupName = res.data.repairerGroupName
          this.form.plateNumber = res.data.car.plateNumber
          this.open1 = true;
          this.title = "出库退料详情";
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        repairFormId: undefined,
        repairNumber: undefined,
        outNumber: undefined,
        returnNumber: undefined,
        carId: undefined,
        plateNumber: undefined,
        outMoney: undefined,
        receiveUserId: undefined,
        receiveUserName: undefined,
        receiveTime: undefined,
        state: undefined,
        outTime: undefined,
        createBy: this.$store.state.user.userInfo.userName,
        createTime: formatDate(new Date().getTime()),
        pickingInfoList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => {return item.id})
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料出库";
      this.getRepairList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getMaterialPicking(id).then(response => {
        this.form = response.data;
        getRepairForm(response.data.repairFormId).then(res =>{
          this.loading = false;
          this.form.clientName = res.data.clientName
          this.form.repairerDeptId = res.data.repairerDeptId
          this.form.repairerDeptName = res.data.repairerDeptName
          this.form.repairerGroupName = res.data.repairerGroupName
          this.form.plateNumber = res.data.car.plateNumber
          this.form.carId = res.data.carId
          this.open = true;
          this.title = "修改物料出库";
          this.getRepairList()
        })
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.materialList = this.form.pickingInfoList
          addMaterialPickingReturn(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物料出库编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delMaterialPicking(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/materialPicking/export', {
        ...this.queryParams
      }, `  出库退料_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
