<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="el-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item label="登记记录号:" class="el-form-item">
              <el-input v-model="filterQuery.orderId" placeholder="请输入登记记录号" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="登记部门:">
              <el-select v-model="filterQuery.fromDeptCode" placeholder="请选择登记部门" clearable="true"
                         style="width: 90%"
                         @change="getDeptList"
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="登记人:">
              <el-input v-model="filterQuery.receiveUserName" placeholder="请输入登记人" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="登记时间:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                style="width: 90%"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addReceiveOrder">新增设备登记</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                @current-change="handleChange" border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="登记记录号" prop="orderId"></el-table-column>
        <el-table-column label="登记部门" prop="fromDeptName"></el-table-column>
        <el-table-column label="设备来源" prop="fromInvName" v-if="paramValue == 0"></el-table-column>
        <el-table-column label="登记人" prop="receiveUserName"></el-table-column>
        <el-table-column label="登记时间" prop="createTime"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="状态" prop="status" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="editOrder(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click.native.stop="deleteDialog(scope.row.id)">删除</el-button>
            <el-button type="text" size="small" @click.native.stop="printOrder(scope.row)">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

    <el-dialog
      :title="formMap[formName]"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="85%"
      v-if="formVisible"
    >
      <deviceReceiveOrderModify
        :deviceReceiveOrder="deviceReceiveOrder"
        :closeDialog="closeDialog"
      ></deviceReceiveOrderModify>
    </el-dialog>
    <el-card class="el-card">
      <el-table v-loading="loading" :data="detailList" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="设备名称" prop="productName" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="DI/物资编码" prop="nameCode" width="200"></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="150"></el-table-column>
        <el-table-column label="生产日期" prop="productionDate" width="150"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="150"></el-table-column>
        <el-table-column label="生产企业" prop="ylqxzcrbarmc" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="300" show-overflow-tooltip></el-table-column>
        <el-table-column label="供应商" prop="supName" width="300" show-overflow-tooltip></el-table-column>
        <!--        <el-table-column label="操作" width="100">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-button-->
        <!--                type="text"-->
        <!--                size="small"-->
        <!--                @click.native.stop="datailSelect(scope.row)"-->
        <!--            >详情-->
        <!--            </el-button-->
        <!--            >-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <pagination
        v-show="detailTotal>0"
        :total="detailTotal"
        :limit.sync="detailQuery.limit"
        :page.sync="detailQuery.page"
        @pagination="getDetailList"
      ></pagination>
    </el-card>

  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import deviceReceiveOrderModify from "@/views/dev/DeviceReceiveOrderModify.vue";
import {
  submitAudit,
  deleteDeviceReceiveOrder,
  getDeviceReceiveOrderList,
  getConfig, getDeviceReceiveOrderDetailList, printOrder, verifyTemplateFile
} from "@/api/dev/deviceReceiveOrder";
import {isBlank} from "@/utils/strUtil";
import {filterByorderIdFk} from "@/api/dev/deviceRepairOrderDetail";


export default {
  name: "DeviceReceiveOrderNew",
  data() {
    return {
      filterQuery: {
        orderId: null,
        fromDeptCode: this.$store.getters.locDeptCode,
        fromInvCode: null,
        receiveUserName: null,
        fromInvSpaceCode: null,
        statusList: [0, 1, 3],
        page: 1,
        limit: 20,
      },
      list: [],
      detailQuery: {
        orderIdFk: null,
        page: 1,
        limit: 20,
      },
      total: 0,
      detailTotal: 0,
      deptList: [],
      invList: [],
      detailList: [],
      spaceList: [],
      actDateRange: [],
      loading: false,
      formVisible: false,
      codeTableLoading: false,
      showSearch: true,
      paramValue: null,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      formName: null,
      formMap: {
        add: "新增设备登记",
        edit: "编辑设备登记"
      },
      deviceReceiveOrder: {},
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        fromDeptCode: this.$store.getters.locDeptCode,
        fromInvCode: null,
        fromInvSpaceCode: null,
        statusList: [0, 1, 3],
        page: 1,
        limit: 20,
      };
      this.actDateRange = [];
      this.spaceList = [];
      // this.getList();
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      if (this.actDateRange !== null) {
        this.filterQuery.startTime = this.actDateRange[0];
        this.filterQuery.endTime = this.actDateRange[1];
      } else {
        this.filterQuery.startTime = null;
        this.filterQuery.endTime = null;
      }
      getDeviceReceiveOrderList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getList();
      });
    },
    invChange() {
      this.spaceList = [];
      this.filterQuery.fromInvSpaceCode = null;
      this.getSpaceList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.fromDeptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {
        invWarehouseCode: this.filterQuery.fromInvCode,
        invStorageCode: this.filterQuery.fromInvCode.fromDeptCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
        this.getList();
      })
    },
    printOrder(row) {
      let query = {id: 5};
      this.loading = true;
      verifyTemplateFile(query).then((res) => {
        if (res.code === 20000) {
          let printParams = {
            templateId: res.data,
            id: row.id
          };
          printOrder(printParams).then((response) => {
            //将pdf文件转换为url。
            const binaryData = [];
            binaryData.push(response);
            //获取blob链接。
            let url = window.URL.createObjectURL(
              new Blob(binaryData, {type: "application/pdf"})
            );
            this.loading = false;
            window.open(url);//打开新标签页，预览pdf。
          })
        } else {
          this.loading = false;
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
      })
    },
    addReceiveOrder() {
      this.formVisible = true;
      this.formName = 'add';
      this.deviceReceiveOrder = {};
    },
    editOrder(row) {
      this.formVisible = true;
      this.formName = 'edit';
      this.deviceReceiveOrder = row;
    },
    submitAudit(row) {
      let params = {id: row.id};
      submitAudit(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("提交成功！");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该领用记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteDeviceReceiveOrder(params).then((res) => {
          this.loading = false;
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getList();
            this.detailList = []
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
    datailSelect(row) {
      this.detailQuery = row
      this.deviceProjectSelectVisible = true
    },
    handleChange(row) {
      this.detailQuery.orderIdFk = row.orderId;
      this.detailQuery.page = 1;
      this.getDetailList();
    },
    getDetailList() {
      getDeviceReceiveOrderDetailList(this.detailQuery).then((res) => {
        if (res.code === 20000) {
          this.detailList = res.data.list || [];
          this.detailTotal = res.data.total || 0;
        } else {
          this.detailList = [];
          this.detailTotal = 0;
        }
      }).catch(() => {
        this.detailList = [];
        this.detailTotal = 0;
      });
    },
  }
  ,
  components: {
    deviceReceiveOrderModify
  }
  ,
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  }
  ,
  created() {
    //查询配置项
    getConfig().then((res) => {
      if (res.code == 20000 && !isBlank(res.data.paramValue)) {
        this.paramValue = res.data.paramValue
      }
    })
    this.getDeptList();
  }
  ,
}
;
</script>

<style type="text/scss" lang="scss">
</style>

