<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="danger" @click="handleDelete">删除</el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport">导入</el-button>
      </template>
    </avue-crud>
     <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/mesOemProcess/downloadExcelTemplate"
      templateName="外协工序模板.xlsx"
      importUrl="/blade-desk/mesOemProcess/importExcel"
     @closeDialog="closeDialog"></basic-import>
  </basic-container>
</template>
<script>
import basicImport from '@/components/basic-import/main.vue'
import {getList,addOutProcess,updateOutProcess,deleteOutProcess} from "@/api/outsourcingManagement/oemProcess"
export default {
  components: {
    basicImport
  },
  data() {
    return {
      isShowImport: false,
      loading: false,
      query:{},
      selectionList: [],
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        // size: "medium",
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: true,
        addBtn: true,
        editBtnText: "修改",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 90,
        menuWidth: 120,
        dialogWidth: 440,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "center",
        gridBtn: false,
        searchMenuPosition: "right",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        align: "center",
        column: [
          {
            label: "工序编码",
            prop: "oemProcessCode",
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            display: false,
          },
          {
            label: "工序",
            prop: "processIdStr",
            search: false,
            sortable: true,
            filter: true,
            span: 24,
            searchLabelWidth: 50,
            type: "select",
            filterable: true,
            dicUrl:"/api/blade-scheduling/processSet/findList",
            props:{
              label: "name",
              value:"id"
            },
            rules: [
              {
                required: true,
                message: "请输入工艺能力",
                trigger: "blur",
              },
            ],
            change:val =>{
              console.log('val==========',val)
              this.form.oemProcessCode = val.item.code
            }
          },
          {
            label: "工艺能力",
            prop: "craftIds",
            search: false,
            sortable: true,
            filter: true,
            span: 24,
            type: "select",
            multiple: true,
            filterable: true,
            dicUrl:"/api/blade-desk/BA/craftAbility/findList",
            props:{
              label:'caName',
              value:"id"
            },
            rules: [
              {
                required: true,
                message: "请输入工艺能力",
                trigger: "blur",
              },
            ],
          },
          {
            label:'手动结算',
            prop:"manual",
            type:'switch',
            span: 24,
            dicData:[
              {
                label:'是',
                value:'1'
              },
              {
                label:'否',
                value:'0'
              }
            ],
            rules: [
              {
                required: true,
                message: "请选择是否手动结算",
                trigger: "blur",
              },
            ],
          }
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
    };
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    closeDialog(val){
      this.isShowImport = false
      if(val){
        this.onLoad()
      }
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {});
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    searchChange(params, done){
      this.page.currentPage = 1
      this.query = params
      this.onLoad()
      done()
    },
    searchReset(){
      this.query = {}
      this.onLoad()
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize
    },
    refreshChange(){
      this.onLoad()
    },
    rowSave(row, done, loading){
      row.processId = row.processIdStr
      addOutProcess(row).then(res =>{
        if(res.data.code == 200){
          this.$message.success('添加成功')
          done()
          this.onLoad()
        }
      }).catch(err =>{
        done()
      })
    },
    rowUpdate(row, index, done, loading){
      row.processId = row.processIdStr
      updateOutProcess(row).then(res =>{
        if(res.data.code == 200){
          this.$message.success('修改成功')
          done()
          this.onLoad()
        }
      }).catch(err =>{
        done()
      })
    },
    handleDelete(){
      if(this.selectionList.length == 0){
        this.$message.error('请至少选择一条数据')
        return
      }
      this.$confirm("确定删除选中数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteOutProcess({
          ids:this.selectionList.map(item => item.id).join(",")
        }).then(res =>{
          if(res.data.code == 200){
            this.$message.success('删除成功')
            this.onLoad()
          }
        })
      })
    },
    rowDel(row){
      this.$confirm("确定删除选中数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteOutProcess({
          ids:row.id
        }).then(res =>{
          if(res.data.code == 200){
            this.$message.success('删除成功')
            this.onLoad()
          }
        })
      })
    },
    onLoad() {
      this.loading = true;
      getList({
        current:this.page.currentPage,
        size:this.page.pageSize,
        ...this.query
      }).then(res =>{
        this.data = res.data.data.records
        this.page.total = res.data.data.total
        this.loading = false
      })
      // this.data = [{}];
      // this.page.total = this.data.length;
      // this.loading = false;
      // setTimeout(() => {
      //   this.selectionClear();
      // }, 500);
    },
  },
};
</script>
<style lang="scss" scoped></style>
