const mixins = require('@/mixins/index.js')
const api = require('@/utils/api.js');
const config = require('@/config/config.js');
const Constants = require('@/utils/Constants');
const util = require('@/utils/util.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    tableData: [],
    showSearch: true,
    initSearchFlag: false,
    buttonSaveList: [
      { code: 'add', title: mixins.$t('costCheck'), },
    ],
    contentSaveList: [{
      code: 'flag',
      title: mixins.$t("allChoose"),
      type: 'checkbox'
    },],
    cardList: ['main'],
    contentObj: {
      main: [
        { code: 'intoType', name: 'intoType', type: 'chooseAndStr', required: true, title: mixins.$t('intType'), urlKey: 'chooseIntType' },
        { code: 'supName', type: 'str', required: false, title: mixins.$t('supplier') },
        { code: 'enterWhDate', name: 'enterWhDate', type: 'dateDay', required: false },
        { code: 'createTime', name: 'createTime', type: 'dateDay', required: false },
        { code: 'staffId', name: 'staffName', type: 'choose', required: false, title: mixins.$t('staffId'), urlKey: 'chooseStaff' },
        { code: 'orgId', name: 'orgName', type: 'choose', required: false, title: mixins.$t('orgTwo'), urlKey: 'chooseOrg' }
      ]
    },
    // 查询条件
    searchContent: [{
      code: 'intoDate',
      title: mixins.$t('pastThirthtyDays'),//近30天',
      defaultValue: 5,
      searchType: Constants.searchType.date
    },
    {
      code: 'choose',
      title: mixins.$t('choose'),//'筛选',
      searchType: Constants.searchType.pick
    }],
    // 查询条件-筛选
    pullMenuList: [
      {
        code: 'intoStatus',
        pullType: 'mSelect',
        typeName: 'intoStatus'
      },
      {
        code: 'staffName',
      },
      {
        code: 'orgName',
      },
      {
        code: 'supName',
      },
      {
        code: 'intoDate',
        dataType: 'date'
      },
      {
        code: 'viewNoCostDocuments',
        dataType: 'checkbox'
      },
    ],
    statusImage: 'verified.png',
    // 内容一定
    content: [
      {
        left: { tag: 'brandName', title: 'skuModel', type: 'title' },
      },
      {
        left: { name: 'skuName', },
      },
      {
        left: { name: 'priceInto', type: 'price' },
        right: { name: 'intoQty', type: 'qty' }
      },
      {
        left: { name: 'nonStdCode', title: '色号：' },
        right: { name: 'whName' },

      },
      {
        left: { name: 'skuSpec', title: '规格：' },
        right: { name: 'seriesName', title: '系列：' },
      }
    ],
    contentObjList: {
      '已核对':
        [
          {
            left: { tag: 'brandName', title: 'skuModel', type: 'title' },
          },
          {
            left: { name: 'skuName', },
          },
          {
            left: { name: 'costPrice', type: 'price' },
            right: { name: 'intoQty', type: 'qty' }
          },
          {
            left: { name: 'nonStdCode', title: '色号：' },
            right: { name: 'whName' },

          },
          {
            left: { name: 'skuSpec', title: '规格：' },
            right: { name: 'seriesName', title: '系列：' },
          }
        ],
        '未核对':
        [
          {
            left: { tag: 'brandName', title: 'skuModel', type: 'title' },
          },
          {
            left: { name: 'skuName', },
          },
          {
            left: { name: 'priceInto', type: 'price' },
            right: { name: 'intoQty', type: 'qty' }
          },
          {
            left: { name: 'nonStdCode', title: '色号：' },
            right: { name: 'whName' },

          },
          {
            left: { name: 'skuSpec', title: '规格：' },
            right: { name: 'seriesName', title: '系列：' },
          }
        ]
    },
    // 路由
    routeObjName: 'inboundItem',
  },

  /**
  * @desc : 全选
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  allChoose(e) {
    let btnFormData = this.data.btnFormData ? JSON.parse(this.data.btnFormData) : ''
    let tableData = this.data.tableData
    tableData.forEach(res => {
      res.checked = btnFormData.flag
    })
    this.setData({
      tableData: tableData
    })
  },
  /**
  * @desc : 选择事件
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  listChoose(e) {
    let tableData = this.data.tableData
    tableData[e.detail.index].checked = e.detail.checked
    this.setData({
      tableData: tableData
    })
  },
  /**
  * @desc : 核对成本跳转
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toAdd() {
    let tableData = this.data.tableData
    let chooseList = []
    chooseList = tableData.filter(res => {
      return res.checked
    })
    if (chooseList.length == 0) {
      util.showToast('请选择一条数据');
      return
    }
    wx.navigateTo({
      url: this.data.route.add.url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
        res.eventChannel.emit('params', {
          chooseList: chooseList
        })
      }
    })
  },
  /**
  * @desc : 选择数据源
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  chooseData(e) {
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }
    if (code == "intoType") { //部门
      formData.intoType = data.name
    }
    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  /**
    * @desc : 查询
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  doSearch() {
    let formData = JSON.parse(this.data.formData)
    if (!formData.intoType) {
      util.showToast('请输入/选择入库类型');
      return
    }
    this.setData({
      btnFormData: JSON.stringify({ flag: false }),
      showSearch: false
    })
    // 重新查询
    this.searchData()
  },
  /**
    * @desc : 重置
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  resetting() {
    this.setData({
      formData: null
    })
  },

  /**
    * @desc : 设置查询参数
    * @date : 2024年3月8日
    * @author : 于继渤
    */
  setSearchParams(params) {
    let formData = JSON.parse(this.data.formData)

    if (formData.createTime) {
      delete formData['createTime']
    }
    if (formData.enterWhDate) {
      delete formData['enterWhDate']
    }
    params.intoTypeName = formData.intoType
    params.supName = formData.supName
    params.intoDateStart = formData.enterWhDateStart ? formData.enterWhDateStart : params.intoDateStart
    params.intoDateEnd = formData.enterWhDateEnd ? formData.enterWhDateEnd : params.intoDateEnd
    params.createTimeStart = formData.createTimeStart
    params.createTimeEnd = formData.createTimeEnd
    params.staffId = formData.staffId
    params.orgId = formData.orgId
    return params;
  },
  /**
   * @desc : 加载数据
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  getData(params) {

    return this.excute(this.data.service, this.data.service.selectCostCheck, params);
  },


})