<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form :model="queryParams" ref="queryForm" size="small" inline @submit.native.prevent>
        <el-form-item label="工具名称" prop="parentTypeName">
          <el-input
            v-model="queryParams.parentTypeName"
            placeholder="请输入工具名称"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="typeName">
          <el-input
            v-model="queryParams.typeName"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item label="管理模式" prop="manageType">
          <el-select
            v-model="queryParams.manageType"
            placeholder="请选择管理模式"
            clearable
            filterable
            style="width: 240px"
          >
            <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </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="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <el-row :gutter="10" class="mb8" justify="end">
        <el-col :span="4">
          <span style="font-size: 20px; font-weight: 800">工具录入申请列表</span>
        </el-col>
        <el-col v-if="!routerParams.isView" :span="20" style="display: flex; justify-content: flex-end">
          <el-button type="primary" @click="handleNumDialog">新增数量工具</el-button>
          <el-button type="primary" @click="handleCodeDialog">新增编码工具</el-button>
          <el-button type="primary" @click="submit">提交申请</el-button>
        </el-col>
      </el-row>

      <el-table
        v-loading="isLoading"
        :data="tableList"
        highlight-current-row
        border
        stripe
        :max-height="650"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="55"
          label="序号"
          align="center"
          :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        />
        <el-table-column
          v-for="(column, index) in tableColumns"
          show-overflow-tooltip
          :key="index"
          :label="column.label"
          :prop="column.prop"
          align="center"
        >
          <!-- 插槽 -->
          <template v-slot="{ row }">
            <component :is="column.render ? { render: (h) => column.render(h, { row }) } : 'span'">
              {{ !column.render ? row[column.prop] : '' }}
            </component>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" v-if="!routerParams.isView">
          <template slot-scope="{ row }">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" style="color: red"
              >删除</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-card>

    <!-- 数量 -->
    <AddNum ref="addNum" @getList="getList" />
    <!-- 编码 -->
    <AddCode ref="addCode" @getList="getList" />
  </div>
</template>

<script>
import { getListByApplyIdApi, deleteToolApi, updateToolApplyApi } from '@/api/toolsManage'
import AddNum from './components/AddNum'
import AddCode from './components/AddCode'

export default {
  name: 'AddEditTools',
  components: { AddNum, AddCode },
  data() {
    return {
      routerParams: {},
      isLoading: false,
      showSearch: true,
      timeRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentTypeName: null,
        typeName: null,
        manageType: null,
        applyId: null,
      },
      typeList: [
        { label: '数量管理', value: '1' },
        { label: '编码管理', value: '0' },
      ],
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '工具专业', prop: 'fourthParentName' },
        { label: '施工类型', prop: 'greatGrandparentName' },
        { label: '工具类型', prop: 'grandparentTypeName' },
        { label: '工具名称', prop: 'parentTypeName' },
        { label: '规格型号', prop: 'typeName' },
        {
          label: '管理模式',
          prop: 'manageType',
          render: (h, { row }) => {
            return row.manageType == '1' ? h('span', {}, '数量管理') : h('span', {}, '编码管理')
          },
        },
        { label: '申请数量', prop: 'applyNum' },
      ],
      // 表格数据
      tableList: [],
    }
  },
  created() {
    this.routerParams = this.$route.query
    let title = '新增工具'
    if (this.routerParams.isView) {
      title = '查看工具'
    } else if (this.routerParams.isEdit) {
      title = '编辑工具'
    }
    this.queryParams.applyId = this.routerParams.applyId || ''
    const obj = Object.assign({}, this.$route, { title })
    this.$tab.updatePage(obj)
    this.getList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.queryParams.parentId = '0'
      this.queryParams.level = '1'
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList(emit = {}) {
      console.log('🚀 ~ emit:', emit)
      if (emit.applyId) {
        this.queryParams.applyId = emit.applyId
        // 存到 route
        this.$route.query.applyId = this.queryParams.applyId
      }
      console.log('列表-查询', this.queryParams)
      this.isLoading = true
      try {
        const params = { ...this.queryParams }
        const res = await getListByApplyIdApi(params)
        this.tableList = res.rows || []
        this.total = res.total || 0
      } catch (error) {
        this.tableList = []
        this.total = 0
      } finally {
        this.isLoading = false
      }
    },
    // 删除
    handleDelete(row) {
      console.log('🚀 ~ row:', row)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        this.isLoading = true
        try {
          await deleteToolApi({ typeId: row.typeId, applyId: this.queryParams.applyId })
          // 提示
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.getList()
        } catch (error) {
          console.log('🚀 ~ error:', error)
        } finally {
          this.isLoading = false
        }
      })
    },
    handleNumDialog() {
      const applyId = this.queryParams.applyId
      this.$refs.addNum.openDialog(applyId)
    },
    handleCodeDialog() {
      const applyId = this.queryParams.applyId
      this.$refs.addCode.openDialog(applyId)
    },
    // 提交
    async submit() {
      this.$confirm('是否确定提交申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        this.isLoading = true
        try {
          await updateToolApplyApi({ id: this.queryParams.applyId, status: '1' })
          // 提示
          this.$message({
            type: 'success',
            message: '操作成功!',
          })
          this.$tab.closeOpenPage({ path: '/toolsManage/applicantList' })
        } catch (error) {
          console.log('🚀 ~ error:', error)
        } finally {
          this.isLoading = false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
