<template>
  <modal v-model="modal" :footer-hide="true" width="1000px">
    <div class="title" slot="header">异常物料</div>
    <div class="content">
      <div class="tabs">
        <div
          @click="tabClick(index, item.value)"
          :class="tabsIndex === index ? 'active' : ''"
          class="tab-item"
          v-for="(item, index) in tabs"
          :key="index">
          {{ item.text }}
        </div>
      </div>
      <div class="table-filter">
        <div class="filter-item">
          <Button
            :type="menuIndex === '' ? 'primary' : 'default'"
            style="width: 90px;" @click="menuTabClick()">
            全部
          </Button>
        </div>
        <div class="filter-item" v-for="(item, index) in data.purposeList" :key="index">
          <Button
            :type="menuIndex === index ? 'primary' : 'default'"
            style="min-width: 90px;"
            @click="menuTabClick(index, item)">
            {{ item }}
          </Button>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table-content">
        <my-table :data="data.checkRecordDetialList && data.checkRecordDetialList.list" :border="true" :tooltip="false" :columns="columns" :isShowFilter="false" :table-page="params" @on-change="changePage"></my-table>
      </div>
    </div>
    <tag-form v-model="show" :form-data="currentData"></tag-form>
  </modal>
</template>

<script>

import MyTable from '_c/my-table/my-table'
import { GetCheckRecordDetailList } from '@/api/stock'
import TagForm from '@/view/store-manage/store-manage/tag-form'
import { GetTechnologyTypList } from '@/api/baseData'
export default {
  name: 'user-form',
  components: {
    MyTable,
    TagForm
  },
  data () {
    return {
      show: false,
      currentData: {},
      modal: false,
      data: {},
      tabs: [],
      tabsIndex: 0,
      menuIndex: '',
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 80,
          align: 'center'
        },
        {
          title: '类型',
          key: 'categoryName',
          minWidth: 150
        },
        {
          title: '用途',
          key: 'purpose',
          minWidth: 150
        },
        {
          title: '物料编码',
          key: 'materialCode',
          minWidth: 150
        },
        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 150
        },
        {
          title: '库存件数',
          key: 'stockCount',
          minWidth: 150
        },
        {
          title: '扫描件数',
          key: 'scanfCount',
          minWidth: 150,
          render: (h, params) => {
            return h('span', {
              style: {
                color: params.row.scanfCount !== params.row.stockCount ? 'red' : ''
              }
            }, params.row.scanfCount)
          }
        },
        {
          title: '盘点件数',
          key: 'handCount',
          minWidth: 150,
          render: (h, params) => {
            return h('span', {
              style: {
                color: params.row.handCount !== params.row.stockCount ? 'red' : ''
              }
            }, params.row.handCount)
          }
        },
        {
          title: '盘点结果',
          key: 'result',
          minWidth: 150,
          render: (h, params) => {
            return h('span', {
              style: {
                color: params.row.result !== '正常' ? 'red' : ''
              }
            }, params.row.scanfCount)
          }
        },
        {
          title: '标签详情',
          key: 'detialId',
          minWidth: 150,
          render: (h, params) => {
            let _this = this
            return h('a', {
              on: {
                click () {
                  _this.currentData.detialId = params.row.detialId
                  _this.currentData.materialName = params.row.materialName
                  _this.show = true
                }
              }
            }, '查看详情')
          }
        },
        {
          title: '盘点时间',
          key: 'checkTime',
          minWidth: 150
        },
        {
          title: '盘点人',
          key: 'checkUser',
          minWidth: 150
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false,
      require: true
    },
    month: {
      type: String | Date,
      default: ''
    }
  },
  watch: {
    value: {
      handler (n) {
        this.modal = n
      },
      immediate: true
    },
    modal (n) {
      this.$emit('input', n)
      if (n) {
        GetTechnologyTypList().then(res => {
          if (res.code === 0) {
            this.tabs = res.result
            this.tabs.unshift({
              text: '全部',
              value: ''
            })
            this.tabClick(0)
          }
        })
      }
    }
  },
  methods: {
    /* 单选框切换 */
    stateChange () {
      this.params.pageIndex = 1
      this.getList()
    },
    menuTabClick (index, item) {
      this.menuIndex = index
      this.params.pageIndex = 1
      this.params.purpose = item
      this.getList()
    },
    /* 选项卡切换 */
    tabClick (index, value) {
      this.tabsIndex = index
      this.menuIndex = ''
      this.params.purpose = ''
      this.params.pageIndex = 1
      this.params.technologyType = value
      this.getList()
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getList () {
      /* 盘点记录 */
      GetCheckRecordDetailList({
        ...this.params,
        month: this.$dayjs(this.month).format('YYYY-MM')
      }).then(res => {
        if (res.code === 0) {
          this.data = res.result
          this.params.total = res.result.checkRecordDetialList.totalCount
        }
      })
    }

  }
}
</script>

<style lang="less" scoped>
.content {
  height: 500px;
  display: flex;
  flex-direction: column;
  border: 1px solid #dcdee2;
  .tabs {
    box-sizing: border-box;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .tab-item {
      box-sizing: border-box;
      flex: 1;
      overflow: auto;
      border-right: 1px solid #dcdee2;
      border-bottom: 1px solid #dcdee2;
      text-align: center;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      font-weight: bold;
      cursor: pointer;
      &:last-child {
        border-right: none;
      }
      &.active {
        color: #3F92F9;
        border-bottom: 3px solid #3F92F9;
      }
    }
  }
  .table-filter {
    margin: 10px;
    margin-bottom: 0;
    overflow: hidden;
    .filter-item {
      margin-right: 10px;
      margin-bottom: 10px;
      float: left;
      //&:last-child {
      //  margin-right: 0;
      //}
    }
  }
  .table-content {
    padding: 5px;
    overflow: hidden;
    flex: 1;
    .table-item {
      width: 100%;
      .row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .col {
          padding: 0 5px;
          flex: 1;
          display: flex;
          align-items: center;
          height: 30px;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          word-break: break-all;
          &.red {
            color: red;
          }
          &.orange {
            color: orange;
          }
          &.blue {
            color: #169bd5;
          }
        }
      }
    }
    .content {
      padding-left: 30px;
    }
  }
}
/deep/ .ivu-modal-body {
  background-color: #FFFFFF;
}
</style>
