<template>
  <div class="hotel-index">
    <h3>待审批信息</h3>
    <el-table
      :data="orderList"
      height="260"
      border
      style="width: 100%"
      :default-sort="{ prop: 'applicationDate', order: 'descending' }"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-descriptions :column="4" border>
              <el-descriptions-item
                v-if="props.row.orderType !== 1"
                label="资产总原值"
              >
                <span>{{ props.row.assetTotalOriginalValue }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType !== 1"
                label="资产总净值"
              >
                <span>{{ props.row.assetTotalNetValue }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 1"
                label="【调整】资产编号"
              >
                <span>{{ props.row.adjustmentAssetNumber }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 1"
                label="【调整】资产原信息"
              >
                <el-button
                  type="text"
                  icon="el-icon-search"
                  @click="
                    onSelectOriginalAssetInfo(
                      props.row.adjustmentOriginalAssetInfo
                    )
                  "
                  >查看</el-button
                >
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType !== 1"
                label="资产明细"
              >
                <el-button
                  type="text"
                  icon="el-icon-search"
                  @click="
                    onSelectopenDetail(
                      props.row
                    )
                  "
                  >查看</el-button
                >
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调出部门"
              >
                <span>{{ props.row.allocationCallOutDepartmentName }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调出经办人"
              >
                <span>{{ props.row.allocationCallOutBy }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调出日期"
              >
                <span>{{
                  parseTime(props.row.allocationCallOutDate, "{y}-{m}-{d}")
                }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调入部门"
              >
                <span>{{ props.row.allocationCallInDepartmentName }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调入经办人"
              >
                <span>{{ props.row.allocationCallInBy }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】调入日期"
              >
                <span>{{
                  parseTime(props.row.allocationCallInDate, "{y}-{m}-{d}")
                }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】新管理人"
              >
                <span>{{ props.row.allocationNewResponsiblePerson }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 2"
                label="【调拨】新功能位置编号"
              >
                <span>{{ props.row.allocationNewLocationNumber }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 3"
                label="【借出】借出部门"
              >
                <span>{{ props.row.lendDepartmentName }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 3"
                label="【借出】经手人"
              >
                <span>{{ props.row.lendBySponsor }}</span>
              </el-descriptions-item>
              <el-descriptions-item
                v-if="props.row.orderType === 3"
                label="【借出】往来单位"
              >
                <span>{{ props.row.lendPassingUnitName }}</span>
              </el-descriptions-item>
              <el-descriptions-item label="原因说明">{{
                props.row.reason
              }}</el-descriptions-item>
              <el-descriptions-item label="所属公司">{{
                props.row.corporationName
              }}</el-descriptions-item>
            </el-descriptions>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="单据编号" align="center" prop="orderNumber" />
      <el-table-column label="单据类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.asset_order_type"
            :value="scope.row.orderType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="申请日期"
        align="center"
        prop="applicationDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="申请部门"
        align="center"
        prop="applicationDepartmentName"
      />
      <el-table-column label="制单人" align="center" prop="preparedBy" />
      <el-table-column
        label="制单部门"
        align="center"
        prop="preparedDepartmentName"
      />
      <el-table-column label="记录状态" align="center" prop="recordStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.asset_record_status"
            :value="scope.row.recordStatus"
          />
        </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="handleAudit(scope.row, 2)"
            v-hasPermi="['order:order:audit']"
            >审批</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleAudit(scope.row, 0)"
            v-hasPermi="['order:order:audit']"
            >撤销</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <h3>待盘点信息</h3>
    <el-table
      :data="inventoryList"
      height="260"
      border
      style="width: 100%"
      :default-sort="{ prop: 'applicationDate', order: 'descending' }"
    >
    <el-table-column  width="55" align="center" />
      <el-table-column label="资产盘点记录编号" align="center" prop="id" />
      <el-table-column label="单据编号" align="center" prop="orderNumber" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <el-table-column label="盘点部门" align="center" prop="deptName" />
      <el-table-column label="盘点人" align="center" prop="inventoryBy" />
      <el-table-column label="盘点日期" align="center" prop="inventoryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inventoryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点位置" align="center" prop="inventoryLocation" />
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <!-- 【调整】资产信息 -->
    <el-dialog
      title="资产调整"
      :visible.sync="openAdjustment"
      width="900px"
      append-to-body
    >
      <AssetInfo
        :title="'资产原信息'"
        :assetInfo="adjustmentOriginalAssetInfo"
      />
    </el-dialog>
    <el-dialog title="单据详细信息" :visible.sync="openDetail" width="1300px" append-to-body>
      <OrderDetail v-if="openDetail" :orderInfo="orderInfo"/>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, auditOrder } from "@/api/order/order";
import { listInventoryDTO } from "@/api/inventory/inventory";
import { getUserProfile } from "@/api/system/user";

export default {
  dicts: ["asset_order_type", "asset_record_status"],
  components: {
    AssetInfo: () => import("@/components/asset/assetCard.vue"),
    OrderDetail: () => import('@/views/order/detail/index.vue'),
  },
  data() {
    return {
      //用户信息
      user: {},
      // 表单参数
      form: {},
      // 资产单据表格数据
      orderList: [],
      inventoryList: [],
      // 资产原信息
      openAdjustment: false,
      adjustmentOriginalAssetInfo: null,
      // 单据详细
      openDetail: false,
      orderInfo: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordStatus: 1,
        inventoryBy: '',
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产单据列表 */
    getList() {
      this.loading = true;
      getUserProfile().then(response => {
        this.user = response.data;
        this.queryParams.inventoryBy = this.user.userName
        listInventoryDTO(this.queryParams).then(response => {
        this.inventoryList = response.rows;
      });
      });
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    onSelectOriginalAssetInfo(info) {
      const json = JSON.parse(info);
      this.adjustmentOriginalAssetInfo = json;
      this.openAdjustment = true;
      console.log("资产原信息:", json);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderType: null,
        assetTotalOriginalValue: null,
        assetTotalNetValue: null,
        applicationDate: null,
        applicationDepartment: null,
        adjustmentAssetNumber: null,
        adjustmentOriginalAssetInfo: null,
        allocationCallOutDepartment: null,
        allocationCallOutBy: null,
        allocationCallOutDate: null,
        allocationCallInDepartment: null,
        allocationCallInBy: null,
        allocationCallInDate: null,
        allocationNewResponsiblePerson: null,
        allocationNewLocationNumber: null,
        lendDepartment: null,
        lendBySponsor: null,
        lendPassingUnit: null,
        reason: null,
        preparedBy: null,
        preparedDepartment: null,
        corporation: null,
        recordStatus: null,
      };
    },
    onSelectopenDetail(order) {
      this.orderInfo = order
      console.log('单据信息:', order)
      this.openDetail = true
    },
    handleAudit(row, recordStatus) {
      this.reset();
      this.form.id = row.id;
      this.form.orderNumber = row.orderNumber;
      this.form.orderType = row.orderType;
      this.form.recordStatus = recordStatus;
      auditOrder(this.form).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.getList();
      });
    },
  },
};
</script>

<style lang="less" scoped>
.hotel-index {
  padding: 0 15px;
}
</style>