<!--
 * @Author: zhp
 * @Date: 2023-12-12 13:45:25
 * @LastEditTime: 2024-03-29 08:51:46
 * @LastEditors: zhp
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form :inline="true" :model="dataForm" class="blueTip">
      <el-form-item>
        <el-date-picker clearable v-model="reportTime" @change="changeTime" type="date" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-button type="primary" @click="getDataList()">查询</el-button>
      <!-- <el-button type="primary" icon="el-icon-edit-outline" @click="editDataList()">编辑</el-button>
      <el-button v-if="isSave" type="success" @click="saveDataList()">保存</el-button> -->
    </el-form>
    <el-row style="float: right; margin-bottom: 5px">
      <el-button v-if="!isSave" size="small" @click="editDataList()">编辑</el-button>
      <el-button v-if="isSave" size="small" @click="handleReturn()">返回</el-button>
      <el-button size="small" v-if="isSave" @click="saveDataList()">保存</el-button>
    </el-row>
    <el-table id="exportTable" :data="list" style="width: 100%" :header-cell-style="{
      background: '#F2F4F9',
      color: '#606266'
    }">
      <el-table-column :label="'许昌安彩成品发货及库存日报' + '(' + timeTips + ')'" align="center">
        <el-table-column prop="glassType" label="品种" align="center">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.glassType" :disabled="disabled"></el-input>
            <span v-else>{{ scope.row.glassType }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="productionH" label="厚度" align="center">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.productionH" :disabled="disabled"></el-input>
            <span v-else>{{ scope.row.productionH }} </span>
          </template>
        </el-table-column>
        <el-table-column label="规格" align="center">
          <el-table-column width="180"  label="成品" align="center">
            <!-- <el-table-column :show-header="false"> -->
            <template slot-scope="scope">
              <el-input v-if="!disabled" v-model="scope.row.productionGlassSize" :disabled="disabled"></el-input>
              <span v-else>{{ scope.row.productionGlassSize }} </span>
            </template>
            <!-- </el-table-column> -->
            <!-- <el-table-column :show-header="false">
              <template slot-scope="scope">
                <el-input v-if="!disabled" v-model="scope.row.productionW" :disabled="disabled"></el-input>
                <span v-else>{{ scope.row.productionW }} </span>
              </template>
            </el-table-column>
            <el-table-column :show-header="false">
              <template slot-scope="scope">
                <el-input v-if="!disabled" v-model="scope.row.productionH" :disabled="disabled"></el-input>
                <span v-else>{{ scope.row.productionH }} </span>
              </template>
            </el-table-column> -->
          </el-table-column>
          <el-table-column width="180" prop="originalGlassSize" label="原片" align="center">
            <!-- <el-table-column prop="originalGlassSize"> -->
            <template slot-scope="scope">
              <el-input v-if="!disabled" v-model="scope.row.originalGlassSize" :disabled="disabled">
              </el-input>
              <span v-else>{{ scope.row.originalGlassSize }} </span>
            </template>
            <!-- </el-table-column> -->
          </el-table-column>
        </el-table-column>

        <!-- <el-table-column label="完成良品产量"> -->
        <el-table-column prop="customerName" align="center" label="用户">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.customerName" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.customerName }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="packageType" align="center" label="包装形式">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.packageType" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.packageType }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="orderNum" align="center" label="订单数量">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.orderNum" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.orderNum }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="orderFinish" align="center" label="订单完成">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.orderFinish" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.orderFinish }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="orderRemaining" align="center" label="未完成订单">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.orderRemaining" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.orderRemaining }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="stockLastMonth" align="center" label="上月库存">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.stockLastMonth" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.stockLastMonth }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="productNumThisMonth" align="center" label="本月生产成品">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.productNumThisMonth" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.productNumThisMonth }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="deliveNum" align="center" label="发货量">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.deliveNum" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.deliveNum }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="undeliveNum" align="center" label="未发货量">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.undeliveNum" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.undeliveNum }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="undeliveArea" align="center" label="未发货面积">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.undeliveArea" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.undeliveArea }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="deliveArea" align="center" label="发货面积">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.deliveArea" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.deliveArea }} </span>
          </template>
        </el-table-column>

        <el-table-column prop="productAreaThisMonth" align="center" label="本月生产面积">
          <template slot-scope="scope">
            <el-input v-if="!disabled" v-model="scope.row.productAreaThisMonth" :disabled="disabled">
            </el-input>
            <span v-else>{{ scope.row.productAreaThisMonth }} </span>
          </template>
        </el-table-column>
      </el-table-column>

      <!-- </el-table-column> -->
      <!-- <div style="height: 50px;" class="remark" slot="append">
        <el-input placeholder="备注" v-if="!disabled" v-model="remark" :disabled="disabled">
        </el-input>
        <span v-else>{{ remark }} </span>
      </div> -->
    </el-table>
    <div style="height: 50px;" class="remark">
      <el-input placeholder="备注" v-model="remark" :disabled="disabled">
      </el-input>
    </div>
    <!-- 搜索工作栏 -->
    <!-- <SearchBar :formConfigs="searchBarFormConfig" ref="search-bar" @headBtnClick="handleSearchBarBtnClick" /> -->

    <!-- 列表 -->
    <!-- <base-table :table-props="tableProps" :page="1" :limit="10" :summary-method="getSummaries" show-summary
      :table-data="list">
    </base-table> -->
    <!-- 分页组件 -->
    <!-- <pagination
			v-show="total > 0"
			:total="total"
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			@pagination="getList" /> -->
  </div>
</template>

<script>
import {
  getAutoDeliveDataList,
  // updateSumAutoDeliveDataList,
  // updateAutoDeliveDataList
  updateAutoDeliveDataList,
  updateSumAutoDeliveDataList
} from '@/api/report/production';
// import Editor from '@/components/Editor';
import moment from 'moment';

// import DialogForm from './dialogForm.vue';
import { getCorePLList } from '@/api/base/coreProductionLine'
// import basicPageMixin from '@/mixins/lb/basicPageMixin';
const tableProps = [
    {
      // width: 128,
    prop: 'lineId',
    label: '生产线',
    },
    {
      // width: 128,
      prop: '',
      label: '投入数㎡',
      align: 'center',
      children: [
        {
          prop: 'inputNow',
          label: '本周'
        },
        {
          prop: 'inputHis',
          label: '上周'
        },
        {
          prop: 'inputTrend',
          label: '增减'
        }
      ]
    },
  {
    // width: 128,
    prop: '',
    label: '完成良品产量',
    align: 'center',
    children: [
      {
        prop: 'goodProductNow',
        label: '本周'
      },
      {
        prop: 'goodProductHis',
        label: '上周'
      },
      {
        prop: 'goodProductTrend',
        label: '增减'
      }
    ]
  },
  {
    // width: 128,
    prop: '',
    label: '原片漏检率',
    align: 'center',
    children: [
      {
        prop: 'missCheckNow',
        label: '本周'
      },
      {
        prop: 'missCheckHis',
        label: '上周'
      },
      {
        prop: 'missCheckTrend',
        label: '增减'
      }
    ]
  },
  {
    // width: 128,
    prop: '',
    label: '综合良品率',
    align: 'center',
    children: [
      {
        prop: 'goodProductPassNow',
        label: '本周'
      },
      {
        prop: 'goodProductPassHis',
        label: '上周'
      },
      {
        prop: 'goodProductPassTrend',
        label: '增减'
      }
    ]
  },
  ];
export default {
  name: 'statisticalData',
	// components: {
	// 	DialogForm,
	// },
	// mixins: [basicPageMixin],
	data() {
    return {
      list: [],
      remark: '',
      monthValue:[],
      // dynamicProps: [],
      tableProps,
      // dataForm: {
      //   reportTime:undefined
      // },
      isSave:false,
      disabled: true,
      sumArr: [],
      timeTips:'',
      searchBarFormConfig: [
        // {
        //   type: 'select',
        //   label: '工单名称',
        //   placeholder: '请选择工单名称',
        //   param: 'workOrderIdList',
        //   selectOptions: [],
        //   multiple: true,
        //   labelField: 'name',
        //   valueField: 'id',
        //   defaultSelect: []
        // },
				// {
				// 	type: 'select',
				// 	label: '产品',
        //   placeholder: '请选择产品',
        //   param: 'productionId',
				// 	selectOptions: [],
				// },
				// {
				// 	type: 'input',
				// 	label: '检测内容',
				// 	placeholder: '请输入检测内容',
				// 	param: 'inspectionDetContent',
				// },
				{
					type: 'datePicker',
					label: '时间段',
					dateType: 'daterange', // datetimerange
					// format: 'yyyy-MM-dd HH:mm:ss',
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
					rangeSeparator: '-',
					startPlaceholder: '开始日期',
					endPlaceholder: '结束日期',
					defaultTime: ['00:00:00', '23:59:59'],
					param: 'checkTime',
					// width: 350,
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				// {
				// 	type: this.$auth.hasPermi(
				// 		'base:quality-inspection-record:create'
				// 	)
				// 		? 'button'
				// 		: '',
				// 	btnName: '新增',
				// 	name: 'add',
				// 	plain: true,
				// 	color: 'success',
				// },
      ],
      pickerOptions: {
        disabledDate(date) {
          return date.getTime() > Date.now()
        }
      },
			// tableBtn: [
			// 	this.$auth.hasPermi('base:quality-inspection-record:update')
			// 		? {
			// 				type: 'edit',
			// 				btnName: '修改',
			// 		  }
			// 		: undefined,
      //   this.$auth.hasPermi('base:quality-inspection-record:delete')
			// 		? {
			// 				type: 'delete',
			// 				btnName: '删除',
			// 		  }
			// 		: undefined,
			// ].filter((v) => v),
			// tableProps: [
			// 	{
			// 		prop: 'createTime',
			// 		label: '添加时间',
			// 		fixed: true,
			// 		width: 180,
			// 		filter: (val) => moment(val).format('yyyy-MM-DD HH:mm:ss'),
			// 	},
			// 	{
			// 		// width: 128,
			// 		prop: 'inspectionDetContent',
			// 		label: '检测内容',
			// 	},
			// 	{
			// 		// width: 128,
			// 		prop: 'lineName',
			// 		label: '产线',
			// 	},
			// 	{
			// 		// width: 128,
			// 		prop: 'checkPerson',
			// 		label: '检测人员',
			// 	},
			// 	{
			// 		// width: 160,
			// 		prop: 'checkTime',
			// 		label: '检测时间',
			// 		filter: (val) =>
			// 			val != null ? moment(val).format('yyyy-MM-DD HH:mm:ss') : '-',
			// 	},
			// 	{
			// 		width: 90,
			// 		prop: 'source',
			// 		label: '来源',
			// 		filter: (val) => ['未知', '手动', '自动'][val],
			// 	},
			// ],
      // 搜索框需要的 keys, 与上面 queryParams 的除 pageNo, pageSize 之外的 key 一一对应

			// searchBarKeys: ['inspectionDetContent', 'checkTime', 'productionLineId'],
			// form: {
			// 	id: undefined,
			// 	inspectionDetId: undefined,
			// 	inspectionDetContent: undefined,
			// 	productionLineId: undefined,
			// 	sectionId: undefined,
			// 	checkPerson: undefined,
			// 	checkTime: undefined,
			// 	source: undefined,
			// 	explainText: undefined,
			// 	remark: undefined,
			// },
      // 查询参数
      reportTime:'',
      dataForm: {
        // pageSize: 100,
        // pageNo:1,
        // workOrderIdList:undefined,
        // productionId: undefined,
        // startTime: undefined,
        // endTime: undefined,
        reportTime: [],
        // reportType:4,
				// productionLineId: null,
			},
		};
  },
	created() {
		// this.getProductLineList();
  },
  mounted() {
    this.getTodayStartTimeAndEndTime()
    this.getDataList()
    this.getDict()
  },
  methods: {
    handleReturn() {
      this.disabled = true
      this.isSave = false
      this.getDataList()
    },
    format(shijianchuo) {
      //shijianchuo是整数，否则要parseInt转换
      var time = moment(new Date(shijianchuo)).format('YYYY-MM-DD HH:mm:ss')
      // console.log(time)
      // var y = time.getFullYear();
      // var m = time.getMonth() + 1;
      // var d = time.getDate();
      // var h = time.getHours();
      // var mm = time.getMinutes();
      // var s = time.getSeconds();
      return time
    },
    getTodayStartTimeAndEndTime() {
      this.reportTime = new Date()
      this.changeTime(this.reportTime)
    },
    changeTime(val) {
      console.log(val)
      if (val) {
        // console.log(val)
        // console.log(val.setHours(7, 0, 0))
        // console.log(val.setHours(7, 0, 0) + 24 * 60 * 60 * 1000)
        // let time = this.format(val.setHours(7, 0, 0))
        this.dataForm.reportTime[0] = this.format(val.setHours(7, 0, 1)) //+ ' 00:00:00' //new Date(this.startTimeStamp + ' 00:00:00').getTime() / 1000
        this.dataForm.reportTime[1] = this.format(val.setHours(7, 0, 0) + 24 * 60 * 60 * 1000) //+ ' 23:59:59' //new Date(this.endTimeStamp + ' 23:59:59').getTime() / 1000
        console.log(this.dataForm.reportTime);
      } else {
        this.dataForm.reportTime = []
      }
    },
    async getDict() {
      // 产线列表
      const res = await getCorePLList()
      this.proLineList = res.data;
    },
    timeSelect() {
      // switch (this.queryParams.timeDim) {
        // case '1':
        //   if (this.timeValue[1] - this.timeValue[0] > 7 * 24 * 3600000) {
        //     this.$modal.msgError('最大时间范围为7天，请重新选择')
        //     this.timeValue = []
        //   }
        //   break
        // case '2':
      if (this.monthValue[1] - this.monthValue[0] > 729 * 24 * 3600000) {
        this.$modal.msgError('最大时间范围为24个月，请重新选择')// 同理上面
        this.monthValue = []
      }
        //   break
        // case '4':
        //   if (this.monthValue[1] - this.monthValue[0] > 729 * 24 * 3600000) {
        //     this.$modal.msgError('最大时间范围为24个月，请重新选择')// 同理上面
        //     this.monthValue = []
        //   }
        //   break
        // default:
    },
    transformTime(timeStamp) {// 本月最后一天
      let year = moment(timeStamp).format('YYYY')
      let month = moment(timeStamp).format('MM')
      let newData = moment(new Date(year, month, 0)).format('YYYY-MM-DD') + ' 23:59:59'
      let value = newData
      console.log(value);
      return value
    },
    // selectTime() {
    //   switch (this.queryParams.type) {
    //     case 1:
    //       this.queryParams.searchTime = this.monthValue
    //       break;
    //     case 2:
    //       this.queryParams.searchTime = this.weekValue
    //       break;
    //     default:
    //       this.queryParams.searchTime = this.dateValue
    //   }
    // },
    editDataList() {
      this.disabled = false
      this.isSave = true
    },
    async saveDataList() {
      let obj = {}
      this.list.forEach((ele, index) => {
        if (ele.det === false) {
          this.list[index].lineId = ''
          this.list[index].remark = this.remark
          obj = ele
          this.list.splice(index,1)
        }
      })
      let updateArr = this.list
      // console.log(JSON.stringify(updateArr[1]))
      const result = await Promise.all([
        await updateAutoDeliveDataList(obj),
        await updateSumAutoDeliveDataList(updateArr),
      ]);
      if (result[0].data == true && result[1].data == true) {
        // console.log(res)
        this.disabled = true
        this.isSave = false
        this.getDataList()
      } else {
        this.$modal.msgError('更新失败');
      }
    },
    async getDataList() {
      this.timeTips = moment(new Date()).format('YYYY-MM-DD') + ' - ' + moment(new Date().setHours(7, 0, 0) + 24 * 60 * 60 * 1000).format('YYYY-MM-DD')
      console.log(this.dataForm);
      if (this.dataForm.reportTime.length == 0) {
        this.$message({
          message: '请选择时间',
          type: 'warning'
        })
        return false
      }
      const res = await this.$axios({
        url: 'base/report-auto-delive/listPlus',
        method: 'get',
        params: this.dataForm
      })

      console.log(res)
      // let sum = undefined
      // res.data.list.forEach((ele, index) => {
      //   if (ele.det === false) {
      //    sum = res.data.list.splice(index, 1)
      //   }
      // })
      res.data.forEach((ele,index) => {
        if (ele.det === false) {
          res.data[index].glassType = '合计'
          this.remark = res.data[index].remark
        }
      });
      // res.data.list.forEach(item => {
      //   this.proLineList.forEach(it => {
      //     if (item.lineId === it.id) {
      //       console.log(item)
      //       item.lineName = it.name
      //     }
      //   })
      // })

      this.list = res.data
    },
	},
};
</script>
<style scoped>
/* .blueTip { */
	/* padding-bottom: 10px; */
/* } */
.blueTip::before{
  display: inline-block;
  content: '';
  width: 4px;
  height: 18px;
  background: #0B58FF;
  border-radius: 1px;
  margin-right: 8PX;
  margin-top: 8px;
}

</style>
