<template>
  <div class="app-container">
    <div class="app-container-search main-bgcolor main-radius">
      <h1 class="">疑似窜货</h1>
      <br />

      <!-- 搜索工作栏 -->
      <RecordForm
      ref="searchForm"
      :searchFormParam="{queryParams, dataSource}"
      :showSearch.sync="showSearch"
      @handleQuery="handleQuery"
      @resetQuery="resetQuery"
      ></RecordForm>
    </div>

    <div class="app-container-list main-bgcolor main-radius">
      <h1>疑似窜货列表</h1>
      <el-button
        style="float: right; margin-bottom: 10px"
        type="primary"
        @click="handleExport"
        :loading="exportLoading"
        v-hasPermi="['identity:diversion-record:export']"
        >导出</el-button>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list">
        <el-table-column label="编号" align="center" type="index" width="80" >
          <template v-slot="{ $index }">
            <span>{{($index+1)+ (queryParams.pageNo-1)*queryParams.pageSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="经销商编码" align="center" prop="dealerId" >
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.dealerId" placement="top">
              <div class="flex-acenter">
                <span class="more-width-20">{{row.dealerId}}</span>
                <el-button type="text" icon="el-icon-copy-document" @click.stop="copyContent(row.dealerId)"></el-button>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="经销商名称" align="center" prop="dealerName" />
        <el-table-column label="所属商户" align="center" prop="belongBusinessName"/>
        <el-table-column label="批次号" align="center" prop="batchId">
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.batchId" placement="top">
              <div class="flex-acenter">
                <span class="more-width-full">{{row.batchId}}</span>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="商品编号" align="center" prop="goodsId" >
          <template slot-scope="{ row }">
            <el-tooltip class="item" effect="dark" :content="row.goodsId" placement="top">
              <div class="flex-acenter">
                <span class="more-width-full">{{row.goodsId}}</span>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="扫码地点" align="center" prop="scanLocation" width="100"/>
        <el-table-column label="窜货等级" align="center" prop="diversionLevel" width="100">
          <template v-slot="{ row }">
            <span>{{row.diversionLevel+1}}级</span>
          </template>
        </el-table-column>
        <el-table-column label="扫码时间" align="center" prop="scanTime" width="170">
          <template v-slot="{ row }">
            <span>{{parseTime(row.scanTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否窜货" align="center" prop="diversionState" width="100">
          <template v-slot="{ row }">
            <span>{{filterLevel(row.diversionState)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100">
          <template v-slot="{ row }">
            <span>{{filterState(row.status)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remarks" >
          <template v-slot="{ row }">
            <span>{{row.remarks?row.remarks:'-'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template v-slot="{ row }">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleDetail(row)"
              v-hasPermi="['identity:diversion-record:update']"
              >详情</el-button>
            <el-button
              v-if="row.diversionState == 0"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handle(row)"
              v-hasPermi="['identity:diversion-record:update']"
              >处理</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <EmptyTable />
        </div>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"/>
    </div>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      v-dialogDrag
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <el-form-item
          label="是否窜货"
          prop="diversionState">
          <el-select class="w100" v-model="form.diversionState" placeholder="请选择">
            <el-option
            v-for="(item,index) in diversionStateList"
            :key="index"
            :label="item.label"
            :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item  v-if="form.diversionState == 1" label="窜货等级" prop="diversionLevel">
          <el-select class="w100" v-model="form.diversionLevel" placeholder="请选择">
            <el-option
            v-for="(item,index) in diversionLevelList"
            :key="index"
            :label="item.label"
            :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </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 RecordForm from '@/components/Customer/SearchForm'
    import EmptyTable from '@/components/EmptyTable'
    import {
      createDiversionRecord,
      updateDiversionRecord,
      deleteDiversionRecord,
      getDiversionRecord,
      getDiversionRecordPage,
      exportDiversionRecordExcel,
    } from '@/api/identity/diversionRecord'

    export default {
      name: 'DiversionRecord',
      components: {
        RecordForm,
        EmptyTable,
      },
  data() {
    return {
      // foldBoole: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疑似窜货记录列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        dealerId: null,
        dealerName: null,
        belongBusinessName: null,
        batchId: null,
        goodsId: null,
        goodsName: null,
        scanLocation: null,
        scanTime: [],
        diversionState: null,
        diversionLevel: null,
        status: null,
        remarks: null,
        belongBusinessId: null,
        belongBusinessName: null,
        createTime: [],
      },
      //表单对象
      dataSource: [
        {
          key: 'dealerId', //经销商编码
          itemType: 'input',
          label: '经销商编码',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'dealerName', //经销商名称
          itemType: 'input',
          label: '经销商名称',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'belongBusinessName', //所属商户
          itemType: 'input',
          label: '所属商户',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'batchId', //批次号
          itemType: 'input',
          label: '批次号',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'scanTime', //扫码时间
          itemType: 'datePicker',
          label: '扫码时间',
          onEnterKeyUp: this.handleQuery,
        },
        {
          key: 'diversionState', //是否窜货
          itemType: 'select',
          label: '是否窜货',
          placeholder: '请输入',
          onEnterKeyUp: this.handleQuery,
          options: [
            {value: 0, label:'疑似'},{value: 1, label:'是'},{value: 2, label:'不是'}
          ],
        },
         {
          key: 'status', //状态
          itemType: 'select',
          label: '状态',
          placeholder: '请选择',
          onEnterKeyUp: this.handleQuery,
          options: [
            {value: 0, label:'未处理'},{value: 1, label:'已处理'}
          ],
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dealerId: [
          { required: true, message: '经销商编码不能为空', trigger: 'blur' },
        ],
        dealerName: [
          { required: true, message: '经销商名称不能为空', trigger: 'blur' },
        ],
        batchId: [
          { required: true, message: '批次id不能为空', trigger: 'blur' },
        ],
        goodsId: [
          { required: true, message: '商品id不能为空', trigger: 'blur' },
        ],
        goodsName: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' },
        ],
        scanLocation: [
          { required: true, message: '扫码地点不能为空', trigger: 'blur' },
        ],
        scanTime: [
          { required: true, message: '扫码时间不能为空', trigger: 'blur' },
        ],
        diversionState: [
          {
            required: true,
            message: '是否窜货不能为空',
            trigger: 'blur',
          },
        ],
        // diversionLevel: [
        //   {
        //     required: true,
        //     message: '窜货等级不能为空',
        //     trigger: 'blur',
        //   },
        // ],
        status: [
          {
            required: true,
            message: '状态 0：未处理  1：已处理不能为空',
            trigger: 'blur',
          },
        ],
        belongBusinessId: [
          { required: true, message: '所属商户id不能为空', trigger: 'blur' },
        ],
        belongBusinessName: [
          { required: true, message: '所属商户名称不能为空', trigger: 'blur' },
        ],
      },
      levelList: [],
      diversionStateList: [
        {value: 1, label:'是'},
        {value: 2, label:'不是'}
      ],
      diversionLevelList: [
        {value: 0, label:'1级'},
        {value: 1, label:'2级'},
        {value: 2, label:'3级'},
      ],
    }
  },
  created() {
    this.getList()
  },
  methods: {
    filterLevel(level){
      switch(level){
        case 0:
         return '疑似';
        case 1:
         return '是';
        case 2:
         return '不是';
      }
    },
    filterState(state){
      switch(state){
        case 0:
         return '未处理';
        case 1:
         return '已处理';
      }
    },
    foldfun() {
      this.foldBoole = !this.foldBoole
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getDiversionRecordPage(this.queryParams).then((response) => {
        this.list = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        dealerId: undefined,
        dealerName: undefined,
        batchId: undefined,
        goodsId: undefined,
        goodsName: undefined,
        scanLocation: undefined,
        scanTime: undefined,
        diversionState: undefined,
        diversionLevel: undefined,
        status: undefined,
        remarks: undefined,
        belongBusinessId: undefined,
        belongBusinessName: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm',"searchForm");
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加疑似窜货记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getDiversionRecord(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改疑似窜货记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        // 修改的提交
        if (this.form.id != null) {
          if(this.form.filterState == 1 && !this.form.filterLevel){
            this.$message.error("确认串货时串货等级不能为空！");
            return;
          }
          updateDiversionRecord(this.form).then((response) => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
          return
        }
        // 添加的提交
        createDiversionRecord(this.form).then((response) => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除疑似窜货记录编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteDiversionRecord(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal
        .confirm('是否确认导出所有疑似窜货记录数据项?')
        .then(() => {
          this.exportLoading = true
          return exportDiversionRecordExcel(params)
        })
        .then((response) => {
          this.$download.excel(response, '疑似窜货记录.xls')
          this.exportLoading = false
        })
        .catch(() => {})
    },
    // 详情
    handleDetail(row){
      this.$router.push({
        path: '/identity/setting/generateDetail',
        query:{
          id: row.id,
        }
      })
    },
    // 处理
    handle(row){
      let data = JSON.parse(JSON.stringify(row));
      data.diversionState = undefined;
      this.form = data;
      this.open = true;
      this.title = '处理';
    }
  },
}
</script>
