<template>
    <div>
        <FullPage 
        title='查看详情' 
        :list='list'
        :logList='logList'
        @init="init"
         @searchData="search"
        @selectTable='selectTable'
        @changePage='changePage'
        @changeSize='changeSize'
        :tableColums='tableColums'
        :tableData='tableData'
        :pageIndex='pageIndex'
        :pageSize='pageSize'
        :total='total'
        >   
            <div slot='titleButton'>
                <Button @click="back" style="margin-right:10px;">返回</Button>
                <Button @click="batchPrint" type="primary" style="margin-right:10px;" ghost>打印派工单</Button>
                <Button @click="del(selectIds,2)" type="error" style="margin-right:10px;" ghost>批量删除</Button>
                <Button @click="finish(selectIds,1)" type="success"  ghost>批量完成</Button>
            </div>

            <template slot='set' slot-scope='{row}'>
                <div>
                   <a v-if="row.sub_state<3" class="map-margin" style="color:#32C800" @click="finish(row,1)">完成</a>
                   <a  class="map-margin" style="color:#ed4014" @click="del(row,1)">删除</a>
                </div>
            </template>
        </FullPage>
    </div>
</template>

<script>
export default {
    data(){
        return {
            form_modal:{
                nickname:[],
                user_id:'',
                rework_reason:'',
                id:[],
                procedure:[],
                procedure_title:[],
            },
            type:1,
            logList:[{title:'系统单号',value:'10998765'}],
            tableColums:[
                {type:'selection',fixed:'left',width:'90',align:'center'},
                {title:'房间号',align:'center',key:'number_detail',minWidth:150,
                    render:(h,params)=>{
                        const {row} = params
                        return h('span',`${row.house}-${row.unit}-${row.layer}-${row.number_detail}`)
                    }
                },
                {title:'产品',align:'center',minWidth:150,key:'product_title'},
                {title:'图号',align:'center',minWidth:150,key:'url_number'},
                {title:'位置',align:'center',minWidth:150,key:'position'},
                {title:'部件',align:'center',minWidth:150,key:'part_title'},
                {title:'零部件',align:'center',minWidth:150,key:'part_detail_title'},
                {title:'部件是否贴标签',align:'center',minWidth:150,
                    render:(h,params)=>h('span',{},params.row.label == '1' ? '是' : '否')
                },
                // {title:'贴标签零部件',align:'center',minWidth:150,key:'sub_part'},
                {title:'工序分类',align:'center',minWidth:150,key:'basic_title'},
                {title:'工序',align:'center',minWidth:150,key:'procedure_title'},
                {title:'班组',align:'center',minWidth:150,key:'nickname'},
                {title:'测量尺寸',align:'center',minWidth:150,key:'measure'},
                {title:'单位',align:'center',minWidth:100,key:'company'},
                {title:'芯片编号',align:'center',minWidth:150,key:'chip'},
                {title:'完工状态',align:'center',minWidth:100,
                    render:(h,params)=>h('span',{},params.row.state == 1 ? '已完工' : '未完工')
                },
                 {title:'返工原因',align:'center',minWidth:150,key:'rework_reason'},
                {title:'操作',align:'center',slot:'set',fixed:'right',width:'150'},
            ],
            tableData:[],
            pageIndex:1,
            pageSize:10,
            total:0,
            selectIds:[],
            tag:[],
            produce_id:[],
            id:[],
            modal8:false,
            proxyObj:{...this.$route.query},
            house_list:[],              //工序分类
            unit_list:[],              //工序
            room_list:[
                {label:'已完工',value:'1'},
                {label:'未完工',value:'0'}
            ],             //完工状态
            house:[],                //楼幢
            unit:[],                //单元
            storey:[],             //楼层
            room_number:[],       //房号
            produce:[],          //产品
            url_number:[],       //图号
            parts:[],           //部件
            tip_state:[
                {label:'是',value:'1'},
                {label:'否',value:'0'}
            ],      //部件时候贴标签
        }
    },
    computed:{
       list() {
      const array = [
          {
          title: "楼幢",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "house",
          optionName: "house",
          optionValue: "house",
          option: this.house,
        },
        {
          title: "单元",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "unit",
          optionName: "unit",
          optionValue: "unit",
          option: this.unit,
        },
        {
          title: "楼层",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "layer",
          optionName: "layer",
          optionValue: "layer",
          option: this.storey,
        },
        {
          title: "房号",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "number_detail",
          optionName: "number",
          optionValue: "number",
          option: this.room_number,
        },
        {
          title: "产品",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "product_title",
          optionName: "title",
          optionValue: "title",
          option: this.produce,
        },
        {
          title: "图号",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "url_number",
          optionName: "url_number",
          optionValue: "url_number",
          option: this.url_number,
        },
        {
          title: "部件",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "part_title",
          optionName: "title",
          optionValue: "title",
          option: this.parts,
        },
        {
          title: "部件是否贴标签",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "label",
          option: this.tip_state,
        },
        {
          title: "工序分类",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "basic_title",
          optionName: "title",
          optionValue: "id",
          option: this.house_list,
        },
        {
          title: "工序",
          filterable: true,
          name: "Select",
          value: "",
          serverName: "procedure_title",
          optionName: "title",
          optionValue: "id",
          option: this.unit_list,
        },
        {
          title: "完工状态",
          filterable: true,
          name: "Select",
          value: '',
          serverName: "state",
          option: this.room_list,
        }
      ];
      return array;
    },
    },
    mounted(){
        this.getData(this.$route.query)
    },
    methods:{
      search(row){
        console.log(row)
             row.page_size = this.pageSize
            row.page_index = this.pageIndex
             Object.assign(row, this.$route.query);
            this.axios('/api/orders_rework_detail',{params:row}).then(res=>{
                if(res.code == 200){
                    console.log(res)
                    this.tableData = res.data.list;
                    this.logList = res.data.detail;
                    this.total = res.data.total
                }
            })
        },
        init(row){
          this.pageIndex = 1;
      row.page_size = this.pageSize;
      row.page_index = this.pageIndex;
      Object.assign(row, this.$route.query);
      this.getData(row);
        },
       del(row,type){
      switch(type){
        case 1:
           
            let id = [];
            id.push(row.id)
            console.log(this.tableData)
   
         this.confirmDelete({
        content: "确认删除么？",
        then: () => {
          this.axios
            .post("/api/orders_rework_del", {
              order_no:this.logList[0].value,
              produce_id:this.produce_id,
              id:id
              })
            .then((res) => {
              if (res.code == 200) {
                this.$Message.success(res.msg);
                this.getData({order_no:this.logList[0].value,
                  produce_id:this.produce_id
                });
              }
            });
        },
      });
      break;
      case 2:
          if(row.length == 0){
              this.$Message.error('请选择要删除的产品')
          }else{   this.confirmDelete({
        content: "确认删除么？",
        then: () => {
          this.axios
            .post("/api/orders_rework_del", {
              order_no:this.logList[0].value,
              produce_id:this.produce_id,
              id:this.id
              })
            .then((res) => {
              if (res.code == 200) {
                this.$Message.success(res.msg);
                this.getData({order_no:this.logList[0].value,
                produce_id:this.produce_id});
              }
            });
        },
      });}
        
      }
    },
        back(){
            this.$router.go(-1)
        },
        getData(row){
            row.page_size = this.pageSize
            row.page_index = this.pageIndex
            this.axios('/api/orders_rework_detail',{params:row}).then(res=>{
                if(res.code == 200){
                    console.log(res)
                    this.tableData = res.data.list;
                    this.logList = res.data.detail;
                    this.total = res.data.total;
                    this.produce_id = res.data.produce_id;
                    this.house = res.data.house;
                    this.unit = res.data.unit;
                    this.storey = res.data.layer
                    this.room_number = res.data.number_detail
                    this.produce = res.data.product_title
                    this.url_number = res.data.url_number
                    this.parts = res.data.part_title
                    this.house_list = res.data.basic_title
                    this.unit_list = res.data.procedure_title
                }
            })
        },
        postData(data,type){
            let url = type == 1 ? '/api/orders_dispatch_confirm' : '/api/orders_plan_cancer'
            this.axios.post(url,data).then(res=>{
                if(res.code == 200){
                    this.$Message.success(res.msg);
                    this.getData(this.$route.query)
                }
            })
        },
        finish(row,type){
            if(!row||row.length<1){return this.$Message.warning('请至少选择一项')}
            let str = Array.isArray(row) ? row.join(',') : row.id
            this.confirmDelete({
                content:type == 1 ? '是否手动操作此订单生产完成' : '确认驳回？',
                title:type == 1 ? '生产完成' : '驳回',
                type:type == 1 ? 'primary' : 'error',
                then:()=>{
                    this.postData({id:str},type)
                    
                },
                cancel:()=>{}
            })
        },
        selectTable(e){
           
            let id = [];
            let result = [];
            e.map(v=>{
                result.push(v.id)
                id.push(v.id)
            });
            this.selectIds = result;
           this.id =id;
            console.log(this.id);
        },
        changePage(e){
            this.pageIndex = e;
            this.proxyObj.page_index = this.pageIndex;
            this.getData(this.proxyObj)
        },
        changeSize(e){
            this.pageSize = e;
            this.proxyObj.page_size = this.page_size;
            this.getData(this.proxyObj)
        },
        batchPrint(){
            this.confirmForm({
            title:'批量打印派工单',
            forms:[
                {name:'Select',title:'模板',value:'',serverName:'template_id',placeholder:'请选择模板',
                    option:[
                        {label:'木工',value:1},
                        {label:'开料',value:2},
                        {label:'贴皮',value:3},
                        {label:'油漆',value:4},
                        {label:'接、拼板',value:6}
                    ]
                },
            ],
            then:(result)=>{
                this.axios('/api/produce_export',{params:{...result,...this.$route.query}}).then(res=>{
                        if(res.code == 200){
                        let url = `${this.$store.state.ip}/api/storage/${res.data.file}`
                        location.href = url
                    }
                })
            }
        })
        }
    }
}
</script>

<style lang="scss" scoped>
.log-list{display: flex;flex-wrap:wrap;padding:10px 0;}
</style>