<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model:page="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #testType="{row}">
        <span>{{row.testType == 0 ? '零件' : '试片'}}</span>
      </template>
      <template #jobType="{row}">
        <span v-if="row.jobType == 1">{{row.jobTypeWeekValue.split(',').length}}次/周</span>
        <span v-if="row.jobType == 2">{{row.jobTypeMonthValue.split(',').length}}次/月</span>
        <span v-if="row.jobType == 3">{{row.jobTypeYearValue.split(',').length}}次/年</span>
      </template>
      <template #menu-left>
        <el-button @click="handleAdd" type="primary">新增</el-button>
        <el-button type="danger" @click="handleDeletes">删 除</el-button>

        <el-button type="success" @click="handleUser">人员设置</el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport">导入</el-button>
      </template>
      <template #menu="scope">
        <el-button type="text" @click="handleView(scope.row, 'view')">详情</el-button>
        <el-button type="text" @click="handleEdit(scope.row, 'add')">编辑</el-button>
        <el-button type="text" @click="handleDelete(scope.row, 'add')">删除</el-button>
        <el-button type="text" @click="handleIssue(scope.row, 'add')">下发</el-button>
      </template>
    </avue-crud>
    <!-- 新增 编辑 详情 -->
    <addProjectCycleDialog :title="title" v-if="showDialog" :showDialog="showDialog" @closeDialog="closeDialog"
    :row="projectForm"></addProjectCycleDialog>
    <!-- 人员设置 -->
    <el-dialog append-to-body :title="title1" v-model="issueDialog" width="550px">
      <el-form ref="issueForm" :model="issueForm" label-width="80px" :rules="formRules">
        <el-form-item label="工艺员" prop="processUserId">
          <el-select v-model="issueForm.processUserId">
            <el-option
              v-for="item in userData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下发时间" prop="oneTimeGenDate" v-if="title1 == '任务下发'">
          <el-date-picker
            v-model="issueForm.oneTimeGenDate"
            style="width:100%"
            type="date"
            :clearable="false"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="请选择下发时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="issueDialog = false">取 消</el-button>
          <el-button type="primary" @click="submitIssue">确 定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/QA/CycleTestItem/download-excel-template"
      templateName="试验项目模板.xlsx"
      importUrl="/blade-desk/QA/CycleTestItem/import-excel"
     @closeDialog="closeDialog"></basic-import>
  </basic-container>
</template>

<script>
import { getList, remove,issueTask,getUserList,setUser } from '@/api/qualityManagement/periodicTesting/projectCycle.js';
import addProjectCycleDialog from './components/addProjectCycleDialog.vue'
import {dateFormat} from '@/utils/date'
import basicImport from '@/components/basic-import/main.vue'
export default {
  components:{
    addProjectCycleDialog,
    basicImport
  },
  data() {
    return {
      isShowImport: false,
      loading: false,
      data: [],
      projectForm: {},
      projectRules: {
        tpProject: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        testPieceTitle: [{ required: true, message: '请选择试验件', trigger: 'blur' }],
        tpStandard: [{ required: true, message: '请输入试验标准', trigger: 'blur' }],
        craftMan: [{ required: true, message: '请选择工艺员', trigger: 'blur' }],
        remDays: [{ required: true, message: '请选择超期提醒', trigger: 'blur' }],
        textCycle: [{ required: true, message: '请选择试验周期', trigger: 'blur' }],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      issueDialog: false,
      issueForm: {},
      formRules: {
        processUserId: [{ required: true, message: '请选择工艺员', trigger: 'blur' }],
      },
      title1: '任务下发',
      userData: [],
      selectionList: [],
      title: '新增',
      showDialog: false,
      monthTags: [],
      yearTags: [],
      option: {
        tip: false,
        height: 'auto',
        calcHeight: 32,
        columnSort: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        viewBtnText: '详情',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        labelWidth: 120,
        // menuWidth: 330,
        dialogWidth: 600,
        dialogClickModal: false,
        searchEnter: true,
        filterBtn: true,
        searchShowBtn: false,
        excelBtn: true,
        showOverflowTooltip: true,
        align: 'center',
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        column: [
          {
            label: '镀种',
            prop: 'plateName',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验件',
            prop: 'testType',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验项目',
            prop: 'cycleTestItemId',
            search: true,
            hide:true,
            type: 'select',
            sortable: true,
            filter: true,
            width: 200,
            dicUrl:'/api/blade-desk/QA/CycleTestItem/listForSelect',
            props: { label: 'name', value: 'id' },
            dicFormatter: (res) => {
              return res.data
            },
          },
          {
            label: '试验项目',
            prop: 'cycleTestItemName',
            search: false,
            type: 'select',
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验条件',
            prop: 'cycleTestItemCondition',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '试验标准',
            prop: 'cycleTestStandardName',
            search: false,
            sortable: true,
            width: 200,
          },

          {
            label: '工艺员',
            prop: 'processUserName',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
            dicUrl: '/blade-system/user/page?current=1&&size=99999',
            props: {
              label: 'name',
              value: 'id',
              res: 'data.records',
            },
          },
          {
            label: '试验周期',
            prop: 'jobType',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '提前下发天数',
            prop: 'genBefore',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '超期提醒天数',
            prop: 'remind',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
          {
            label: '更新时间',
            prop: 'updateTime',
            search: false,
            sortable: true,
            filter: true,
            width: 200,
          },
        ],
      },
    };
  },
  mounted() {
    this.getUsers()
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    // 获取工艺员列表
    getUsers(){
      getUserList('1993624442365915137').then(res =>{
        this.userData = res.data.data
      })
    },
    closeDialog(){
      this.showDialog = false;
      this.onLoad(this.page)
    },
    // 点击详情按钮
    handleView(row) {
      this.projectForm = row;
      this.title = '详情';
      this.showDialog = true;
    },
    // 点击编辑按钮
    handleEdit(row) {
      this.projectForm = row;
      this.title = '编辑';
      if (this.projectForm.textCycle == '3') {
        this.yearTags = this.projectForm.tags;
      } else if (this.projectForm.textCycle == '2') {
        this.monthTags = this.projectForm.tags;
      } else {
        this.projectForm.weekList = this.projectForm.tags;
      }
      this.showDialog = true;
    },
    // 点击下发按钮
    handleIssue(row) {
      this.title1 = '任务下发';
      let date = dateFormat(new Date(),'yyyy-MM-dd')
      this.issueForm = {}
      this.issueForm.cycleTestId = row.id
      this.issueForm.oneTimeGenDate = date
      this.issueDialog = true;
    },
    // 点击上方删除按钮，多条删除
    handleDeletes() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据!');
      } else {
        this.$confirm('确定删除所选数据?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          remove(this.selectionList.map(item => item.id).join(',')).then(res =>{
            if(res.data.code == 200){
              this.$message.success('删除成功')
              this.onLoad(this.page)
            }
          })
        });
      }
    },

    // 点击人员配置按钮
    handleUser() {
      if (this.selectionList.length == 0) {
        this.$message.error('请至少选择一条数据!');
      } else {
        this.title1 = '人员设置';
        this.issueDialog = true;
        this.issueForm = {}
      }
    },
    // 点击删除按钮
    handleDelete(row) {
      this.$confirm('确定删除此条数据?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        remove(row.id).then(res =>{
          if(res.data.code == 200){
            this.$message.success('删除成功')
            this.onLoad(this.page)
          }
        })
      });
    },
    // 点击新增按钮
    handleAdd() {
      this.title = '新增';
      this.showDialog = true;
      this.projectForm = {};
      this.monthTags = [];
      this.yearTags = [];
      this.$refs.form.resetFields();
    },
    // 切换周期
    changeCycle() {
      this.projectForm.monthValue = '';
      this.projectForm.weekList = [];
      this.monthTags = [];
      this.projectForm.yearMonth = '';
      this.projectForm.yearDays = '';
      this.yearTags = [];
    },
    // 添加月份日期
    addMonth() {
      let value = this.projectForm.monthValue + '日';
      if (this.monthTags.find(item => item == value)) return;
      this.monthTags.push(this.projectForm.monthValue + '日');
    },
    // 添加年份日期
    addYears() {
      let value = this.projectForm.yearMonth + '月' + this.projectForm.yearDays + '日';
      if (this.yearTags.find(item => item == value)) return;
      if (this.projectForm.yearMonth == '' || this.projectForm.yearDays == '') return;
      this.yearTags.push(this.projectForm.yearMonth + '月' + this.projectForm.yearDays + '日');
    },
    // 删除月份日期
    closeMonthTag(item, index) {
      this.monthTags = this.monthTags.filter(tags => tags != item);
    },
    // 删除年份日期
    closeYearsTag(item, index) {
      this.yearTags = this.yearTags.filter(tags => tags != item);
    },
    // 新增确定按钮
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.showDialog = false;
        }
      });
    },
    // 下发弹窗确定按钮
    submitIssue() {
      this.$refs.issueForm.validate(valid => {
        if (valid) {
          if(this.title1 == '任务下发'){
            console.log('issueForm-------------',this.issueForm)
            issueTask(this.issueForm).then(res =>{
              console.log('res---------------',res)
              this.$message.success('下发成功')
              this.onLoad(this.page)
            })
          }else{
            setUser({
              ids:this.selectionList.map(item => item.id).join(','),
              processUserId:this.issueForm.processUserId
            }).then(res =>{
              if(res.data.code == 200){
                this.$message.success('设置成功')
                this.onLoad(this.page)
              }
            })
          }
          this.issueDialog = false;
        }
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;

      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        this.data = res.data.data.records;

        this.loading = false;
        this.page.total = res.data.data.total
        // this.selectionClear();
      });
    },
  },
};
</script>

<style></style>
