<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="退货单号" prop="orderNo">
            <el-input
              v-model="queryParams.orderNo"
              placeholder="请输入退货单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="签收状态" prop="signStatus">
            <DictSelect v-model="queryParams.signStatus" placeholder="请选择签收状态" dict-type="WMS_SIGN_STATUS" />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <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>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>

    <!-- 列表 -->
    <el-table
      v-if="columnsList.length > 0"
      v-loading="loading"
      :data="list"
      border
    >
      <el-table-column
        v-if="columnsList[0].visible"
        :key="Math.random()"
        label="退货单号"
        align="center"
        prop="orderNo"
        min-width="120"
      />
      <el-table-column
        v-if="columnsList[1].visible"
        :key="Math.random()"
        label="退货状态"
        align="center"
        prop="status"
        width="110"
      >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_SALES_RETURN_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnsList[2].visible"
        :key="Math.random()"
        label="退货原因"
        align="center"
        prop="remark"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="columnsList[3].visible"
        :key="Math.random()"
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnsList[4].visible"
        :key="Math.random()"
        label="签收状态"
        align="center"
        prop="signStatus"
      >
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_SIGN_STATUS" :value="scope.row.signStatus" />
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnsList[5].visible"
        :key="Math.random()"
        label="签收人"
        align="center"
        prop="signEmployee"
      />
      <el-table-column
        label="操作"
        width="240"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:sale-return-detail:query']"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-hasPermi="['wms:sale-return-detail:query']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            class="text-danger"
            :disabled="scope.row.status === 1"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            v-hasPermi="['wms:sale-return-master:update']"
            size="mini"
            type="text"
            icon="el-icon-thumb"
            :disabled="scope.row.status === 1"
            @click="handleSign(scope.row)"
          >签收</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh-right"
            :loading="cancelLoading"
            class="text-danger"
            :disabled="scope.row.status === 0"
            @click="cancelSign(scope.row)"
          >撤销签收</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 退货扫码 -->
    <SalesReturnToGetProduct ref="salesReturnToGetProduct" @complete="getList" />
    <!-- 退货明细 -->
    <SalesReturnDetail ref="salesReturnDetail" />
    <!-- 签收 -->
    <SalesReturnSign ref="salesReturnSign" @signComplete="getList" />
  </div>
</template>

<script>
import { getSaleReturnMasterPage, deleteSaleReturnMaster, cancelSignSaleReturn } from '@/api/wms/output/salesReturn'
import SalesReturnToGetProduct from './components/SalesReturnToGetProduct.vue'
import SalesReturnDetail from './components/SalesReturnDetail.vue'
import SalesReturnSign from './components/SalesReturnSign.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'SaleReturnMaster',
  components: {
    SalesReturnToGetProduct,
    SalesReturnDetail,
    SalesReturnSign
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售退货列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        status: null,
        remark: null,
        createTime: [],
        signStatus: null,
        signEmployee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '退货单号', visible: true },
        { key: 1, label: '退货状态', visible: true },
        { key: 2, label: '退货原因', visible: true },
        { key: 3, label: '创建时间', visible: true },
        { key: 4, label: '签收状态', visible: true },
        { key: 5, label: '签收人', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: [],
      cancelLoading: false
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSaleReturnMasterPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        status: undefined,
        remark: undefined,
        signStatus: undefined,
        signEmployee: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$refs.salesReturnToGetProduct.open = true
    },
    //  查看明细
    handleView(row) {
      const { id } = row
      this.$refs.salesReturnDetail.queryParams.masterId = id
      this.$refs.salesReturnDetail.open = true
    },
    // 签收
    handleSign(row) {
      const { id } = row
      this.$refs.salesReturnSign.queryParams.masterId = id
      this.$refs.salesReturnSign.open = true
    },
    // 撤销签收
    async cancelSign(row) {
      const { id } = row
      this.cancelLoading = true
      try {
        await this.$modal.confirm('您正在进行撤销的操作,请确认是否继续?')
        await cancelSignSaleReturn({ id })
        this.$modal.msgSuccess('撤销成功!')
        this.getList()
      } finally {
        this.cancelLoading = false
      }
    },
    // 删除
    handleDelete(row) {
      const { id, orderNo } = row
      this.$modal.confirm('是否确认删除退货单号为"' + orderNo + '"的数据项?').then(function() {
        return deleteSaleReturnMaster(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null
      const params = {
        userId: this.userId,
        vueForm: componentName
      }
      getFormColumnsList(params).then(res => {
        if (isArray(res.data)) {
          this.columnsList = res.data.length === 0 ? this.columns : res.data
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      })
    }
  }
}
</script>
