<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="assetNumber">
        <el-input v-model="queryParams.assetNumber" placeholder="请输入资产编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="调整原因" prop="reason">
        <el-input v-model="queryParams.reason" placeholder="请输入调整原因" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="调整单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入调整单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="制单人" prop="preparedBy">
        <el-input v-model="queryParams.preparedBy" placeholder="请输入制单人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="制单部门" prop="preparedDepartment">
        <el-input
          v-model="queryParams.preparedDepartment"
          placeholder="请输入制单部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="记录状态" prop="recordStatus">
        <el-select v-model="queryParams.recordStatus" placeholder="请选择记录状态" clearable>
          <el-option v-for="dict in dict.type.asset_record_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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="['asset:adjustment: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="['asset:adjustment:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-position" size="mini" :disabled="single" @click="submit" v-hasPermi="['asset:adjustment:submit']">提交</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-check" size="mini" :disabled="single" @click="audit" v-hasPermi="['asset:adjustment:audit']">审核</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="['asset:adjustment: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="['asset:adjustment:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="adjustmentList" @selection-change="handleSelectionChange" v-if="arr">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="资产编号" align="center" prop="assetNumber">
        <template slot-scope="scope">
          <span>{{ scope.row.assetNumber != null ? assetName(scope.row.assetNumber) : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资产原信息" align="center" prop="originalAssetInfo">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-search" @click="test(scope.row.originalAssetInfo)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="调整原因" align="center" prop="reason" />
      <!-- <el-table-column label="调整单号" align="center" prop="orderNumber" /> -->
      <el-table-column label="制单人" align="center" prop="preparedBy" />
      <el-table-column label="制单部门" align="center" prop="preparedDepartment">
        <template slot-scope="scope">
          <span v-if="scope.row.preparedDepartment">{{ scope.row.preparedDepartment != null ? deptName(scope.row.preparedDepartment) : '' }}</span>
        </template>
      </el-table-column>
      <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="handleUpdate(scope.row)" v-hasPermi="['asset:adjustment:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['asset:adjustment: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="assetNumber">
          <el-select v-model="form.assetNumber" filterable placeholder="请选择资产编号">
            <el-option v-for="item in informationList" :key="item.number" :label="item.name" :value="item.number"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产原信息" prop="originalAssetInfo">
          <el-input v-model="form.originalAssetInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="调整原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入调整原因" />
        </el-form-item>
        <!-- <el-form-item label="调整单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入调整单号" />
        </el-form-item> -->
        <el-form-item label="制单人" prop="preparedBy">
          <el-input v-model="form.preparedBy" placeholder="请输入制单人" />
        </el-form-item>
        <el-form-item label="制单部门" prop="preparedDepartment">
          <treeselect v-model="form.preparedDepartment" :options="deptOptions" :normalizer="normalizer" placeholder="选择制单部门" />
        </el-form-item>
        <!-- <el-form-item label="记录状态" prop="recordStatus">
          <el-select v-model="form.recordStatus" placeholder="请选择记录状态">
            <el-option
              v-for="dict in dict.type.asset_record_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </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 { listAdjustment, getAdjustment, delAdjustment, addAdjustment, updateAdjustment, submitAdjustment, auditAdjustment } from '@/api/asset/adjustment'
import { listDept } from '@/api/system/dept'
import { listInformation } from '@/api/asset/information'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Adjustment',
  dicts: ['asset_record_status'],
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: [],
      arr: [],
      // 资产调整表格数据
      adjustmentList: [],
      // 资产信息表格数据
      informationList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetNumber: null,
        originalAssetInfo: null,
        reason: null,
        orderNumber: null,
        preparedBy: null,
        preparedDepartment: null,
        recordStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // assetNumber: [
        //   { required: true, message: "资产编号不能为空", trigger: "blur" }
        // ],
        originalAssetInfo: [{ required: true, message: '资产原信息', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '调整单号不能为空', trigger: 'blur' }],
        preparedBy: [{ required: true, message: '制单人不能为空', trigger: 'blur' }],
        recordStatus: [{ required: true, message: '记录状态不能为空', trigger: 'change' }],
      },
      // 资产原信息
      originalAssetInfo: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    test(val) {
      const json = JSON.parse(val)
      console.log('资产原信息:', json)
    },
    /** 查询资产调整列表 */
    getList() {
      this.loading = true
      listDept().then((response) => {
        this.deptOptions = this.handleTree(response.data, 'deptId')
        this.arr = response.data
      })
      listInformation().then((response) => {
        this.informationList = response.rows
      })
      listAdjustment(this.queryParams).then((response) => {
        this.adjustmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      }
    },
    //获取公司名
    deptName(val) {
      let num = parseInt(val)
      let arr2 = this.arr.filter((item) => item.deptId === num)
      if (arr2) {
        return arr2[0].deptName
      }
    },
    //获取资产名
    assetName(val) {
      let arr2 = this.informationList.filter((item) => item.number === val)
      return arr2[0].name
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetNumber: null,
        originalAssetInfo: null,
        reason: null,
        orderNumber: null,
        preparedBy: null,
        preparedDepartment: null,
        recordStatus: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加资产调整'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getAdjustment(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改资产调整'
      })
    },
    submit(row) {
      this.reset()
      const id = row.id || this.ids
      getAdjustment(id).then((response) => {
        submitAdjustment(response.data).then((response) => {
          this.$modal.msgSuccess('提交成功')
          this.open = false
          this.getList()
        })
      })
    },
    /**审核按钮 */
    audit(row) {
      this.reset()
      const id = row.id || this.ids
      getAdjustment(id).then((response) => {
        auditAdjustment(response.data).then((response) => {
          this.$modal.msgSuccess('审核成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAdjustment(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addAdjustment(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除资产调整编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAdjustment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'asset/adjustment/export',
        {
          ...this.queryParams,
        },
        `adjustment_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
