<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="资产名称" prop="propName">
        <el-input v-model="queryParams.propName" placeholder="请输入资产名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="资产编号" prop="propNo">
        <el-input v-model="queryParams.propNo" placeholder="请输入资产编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="供应商" prop="supplier">
        <el-input v-model="queryParams.supplier" placeholder="请输入供应商" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="资产分类" prop="propCatg">
        <el-select v-model="queryParams.propCatg" placeholder="请选择资产分类" class="customerSelectWidth">
          <el-option v-for="dict in companyPropertyCatg" :key="dict.code"
                     :label="dict.name" :value="dict.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="价值类型" prop="costType">
        <el-select v-model="queryParams.costType" placeholder="请选择价值类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMPANY_COST_TYPE)" :key="dict.value"
                     :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资产状态" prop="propStatus">
        <el-select v-model="queryParams.propStatus" placeholder="请选择资产状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMPANY_PROP_STATUS)" :key="dict.value"
                     :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="凭证号" prop="voucherNo">
        <el-input v-model="queryParams.voucherNo" placeholder="请输入凭证号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="生产厂家" prop="factory">
        <el-input v-model="queryParams.factory" placeholder="请输入生产厂家" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="存放位置" prop="posId">
        <el-select v-model="queryParams.posId" clearable filterable placeholder="请选择存放位置">
          <el-option v-for="item in positionList" :key="item.id" :label="item.posName" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="使用人" prop="userId">
        <el-select filterable v-model="queryParams.userId" placeholder="请选择使用人" clearable>
          <el-option v-for="item in staffList" :key="item.id" :label="item.name" :value="item.userId">
            <div class="options">
              <span>姓名:{{ item.name }}</span>
              <span>工号:{{ item.workNo }}</span>
              <span>手机:{{ item.tel }}</span>
            </div>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="取得日期" prop="getDateArray">
        <el-date-picker v-model="queryParams.getDateArray" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="启用日期" prop="openDateArray">
        <el-date-picker v-model="queryParams.openDateArray" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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="openForm(undefined)"
                   v-hasPermi="['company:property:create']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading" v-hasPermi="['company:property:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="num">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{
              queryParams.pageSize * (queryParams.pageNo - 1) + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="资产名称" align="center" prop="propName" width="150px"/>
      <el-table-column label="存放位置" align="center" prop="posName" width="150px"/>
      <el-table-column label="资产编号" align="center" prop="propNo" width="150px"/>
      <el-table-column label="资产分类" align="center" prop="propCatg" width="150px">
        <template v-slot="scope">
          {{scope.row.propCatg !== null ? companyPropertyCatg.find(item => item.code === scope.row.propCatg).name : ''}}
        </template>
      </el-table-column>
      <el-table-column label="价值类型" align="center" prop="costType" width="150px">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMPANY_COST_TYPE" :value="scope.row.costType"/>
        </template>
      </el-table-column>
      <el-table-column label="预计使用年限" align="center" prop="useYear" width="150px"/>
      <el-table-column label="资产数量" align="center" prop="propNum" width="150px">
        <template v-slot="scope">
          {{ scope.row.propNum + ' ' + scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="资产原值（元）" align="center" prop="costTotal"width="150px"/>
      <el-table-column label="资产状态" align="center" prop="propStatus" width="150px">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.COMPANY_PROP_STATUS" :value="scope.row.propStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brand" width="150px"/>
      <el-table-column label="规格型号" align="center" prop="spec" width="150px"/>
      <el-table-column :show-overflow-tooltip="true" label="生产厂家" align="center" prop="factory" width="150px"/>
      <el-table-column label="出厂序列号/编号" align="center" prop="serialNo" width="150px"/>
      <el-table-column label="出厂日期" align="center" prop="prodDate" width="150px">
        <template v-slot="scope">
          {{ scope.row.prodDate }}
        </template>
      </el-table-column>
      <el-table-column label="取得日期" align="center" prop="getDate" width="150px"/>
      <el-table-column :show-overflow-tooltip="true" label="供应商" align="center" prop="supplier" width="150px"/>
      <el-table-column label="启用日期" align="center" prop="openDate" width="150px"/>
      <el-table-column label="净值（元）" align="center" prop="netValue" width="150px"/>
      <el-table-column label="凭证号" align="center" prop="voucherNo" width="150px"/>
      <el-table-column label="上次维修/保养日期" align="center" prop="lastKeepDate" width="150px"/>
      <el-table-column label="维修/保养周期" align="center" prop="keepCycle" width="150px">
        <template v-slot="scope">
          {{ scope.row.keepCycle + ' '}}
          <dict-tag :type="DICT_TYPE.CYCLE_DICT" :value="scope.row.keepCycleType"/>
        </template>
      </el-table-column>
      <el-table-column label="下次维修/保养日期" align="center" prop="nextKeepDate" width="150px"/>
      <el-table-column label="使用人" align="center" prop="staffName" width="150px" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['company:property:update']"
          >修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['company:property:delete']"
          >删除
          </el-button>
          <el-dropdown @command="(command) => handleCommand(command, scope.$index, scope.row)"
                       v-hasPermi="['company:property:query']"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handlePropertyKeepCrate" size="mini" type="text" icon="el-icon-setting"
                                v-hasPermi="['company:property-keep:create']"
              >登记维修\保养记录
              </el-dropdown-item>
              <el-dropdown-item command="handlePropertyKeepQuery" size="mini" type="text" icon="el-icon-search"
                                v-hasPermi="['company:property-keep:query']"
              >查看维修\保养记录
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
    <!-- 对话框(添加 / 修改) -->
    <PropertyForm ref="formRef" @success="getList"/>
    <PropertyKeepCreate ref="propertyKeepCreateRef" @success="getList" />
    <PropertyKeepQuery ref="propertyKeepQueryRef" @success="getList" />
  </div>
</template>

<script>
import * as PropertyApi from '@/api/company/property/property'
import PropertyForm from './form/PropertyForm.vue'
import { listCategory } from '@/api/system/category'
import { DICT_TYPE } from '@/utils/dict'
import { getStaffList } from '@/api/company/staff'
import { getPropertyPosList } from '@/api/company/property/position'
import PropertyKeepCreate from '@/views/company/property/propertykeep/PropertyKeepCreate'
import PropertyKeepQuery from '@/views/company/property/propertykeep/PropertyKeepQuery'

export default {
  name: 'Property',
  components: {
    PropertyForm,
    PropertyKeepCreate,
    PropertyKeepQuery
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业管理-资产列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        corpId: null,
        deptId: null,
        posId: null,
        userId: null,
        propNo: null,
        propName: null,
        propCatg: null,
        useYear: null,
        costType: null,
        propNum: null,
        costTotal: null,
        propStatus: null,
        brand: null,
        spec: null,
        factory: null,
        serialNo: null,
        unit: null,
        getDateArray: [],
        prodDate: [],
        supplier: null,
        openDateArray: [],
        netValue: null,
        voucherNo: null,
        keepCycleType: null,
        keepCycle: null,
        lastKeepDate: [],
        nextKeepDate: [],
        createTime: []
      },
      companyPropertyCatg: [],
      staffList: [],
      positionList: []
    }
  },
  created() {
    this.getList()
    this.getCompanyPropertyCatg()
    this.listStaff()
    this.listPropertyPos()
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true
        const res = await PropertyApi.getPropertyPage(this.queryParams)

        this.list = res.data.records
        this.total = res.data.total
      } finally {
        this.loading = false
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs['formRef'].open(id, this.companyPropertyCatg, this.staffList, this.positionList)
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id
      const propNo = row.propNo
      await this.$modal.confirm('是否确认删除企业管理-资产编号为"' + propNo + '"的数据项?')
      try {
        await PropertyApi.deleteProperty(id)
        await this.getList()
        this.$modal.msgSuccess('删除成功')
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有企业管理-资产数据项?')
      try {
        this.exportLoading = true
        const data = await PropertyApi.exportPropertyExcel(this.queryParams)
        this.$download.excel(data, '企业管理-资产.xls')
      } catch {
      } finally {
        this.exportLoading = false
      }
    },
    // 获取分类字典
    async getCompanyPropertyCatg() {
      listCategory().then(res => {
        this.companyPropertyCatg = this.handleTree(res.data, 'id', 'pid', 'children', '0').find(item => item.code === DICT_TYPE.COMPANY_PROPERTY_CATG).children
      })
    },
    // 拿当前登录用户的部门下的员工们
    async listStaff() {
      getStaffList().then(res => {
        this.staffList = res.data
      })
    },
    // 拿到位置列表
    async listPropertyPos() {
      getPropertyPosList().then(res => {
        this.positionList = res.data
      })
    },
    // 更多操作
    handleCommand(command, index, row) {
      switch (command) {
        case 'handlePropertyKeepCrate':
          this.handlePropertyKeepCrate(row)
          break
        case 'handlePropertyKeepQuery':
          this.handlePropertyKeepQuery(row)
          break
        default:
          break
      }
    },
    handlePropertyKeepCrate(row){
      this.$refs['propertyKeepCreateRef'].open(row)
    },
    handlePropertyKeepQuery(row){
      this.$refs['propertyKeepQueryRef'].open(row)
    }
  }
}
</script>
