<template>
  <a-modal
    :title="title"
    :width="modelStyle.width"
    :bodyStyle="bodyStyle"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <page-layout :title="'需求单号：'+this.code3" :noBreadcrumb='true'>
      <detail-list slot="headerContent" size="small" class="detail-layout">
        <detail-list-item term="发单人">{{this.code1}}</detail-list-item>
        <detail-list-item term="发单时间">{{this.code2}}</detail-list-item>
      </detail-list>

<!--      <template slot="action">-->
<!--        <a-button @click="generateorderMany()" type="primary" v-has="'require:generateorderMany'">批量生成订单</a-button>-->
<!--        <template v-if="requirestatus=='2' && requireison=='1'">-->
<!--          <a-button @click="handleImportExcel1(code)" type="primary"  style="margin-left: 8px" v-has="'require:importenrol'">导入报名</a-button>-->
<!--        </template>-->
<!--      </template>-->
<!--      <a-card :bordered="false">-->
        <!-- table区域-begin -->
        <div class='table'>
<!--          <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
<!--          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
<!--          <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
<!--          </div>-->

          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange">

            <span slot="action1" slot-scope="text, record">
              <j-ellipsis :value="record.brightPoints" :length="20"/>
            </span>

            <span slot="action" slot-scope="text, record">
               <template v-if="record.orderNumber==null && requirestatus=='2' && requireison=='1'">
              <a @click="generateOrder(record)" v-has="'require:generateorder'">生成订单</a>
               </template>
               <template v-if="record.orderNumber!=null">
                 <span>已创建订单{{record.orderNumber}}</span>
               </template>
            </span>

          </a-table>
        </div>


        <hw-require-enrollment-export-modal ref="modalSelectRequireForm"
                                            @ok="modalFormOk"></hw-require-enrollment-export-modal>
<!--      </a-card>-->
    </page-layout>
    <template #footer>
      <a-button key="back" @click="handleCancel">关闭</a-button>
<!--      <a-button key="submit" type="primary" :loading="loading" @click="handleOk">Submit</a-button>-->
    </template>
  </a-modal>
</template>
<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import HwEnrollmentModal from './modules/HwEnrollmentModal'
  import HwRequireEnrollmentExportModal from './modules/HwRequireEnrollmentExportModal'
  import { getAction, deleteAction, putAction, postAction } from '@/api/manage'
  import qs from 'qs'

  const generateOrder = (params) => putAction('/ordermanager/hwOrder/generateOrder', params)
  const generateOrderMany = (params) => putAction('/ordermanager/hwOrder/generateOrderMany', params)

  import DetailList from '@/components/tools/DetailList'
  import PageLayout from '@/components/page/PageLayout'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import JEllipsis from '@/components/jeecg/JEllipsis'

  const DetailListItem = DetailList.Item
  export default {
    name: 'HwEnrollmentList',
    mixins: [JeecgListMixin],
    components: {
      ATextarea,
      HwEnrollmentModal,
      DetailList,
      PageLayout,
      DetailListItem,
      HwRequireEnrollmentExportModal,
      JEllipsis
    },
    data() {
      return {
        code1: '',
        code2: '',
        code3: '',
        code: '',
        requirestatus: '5',
        requireison: '0',
        record1: '',
        companyName: '',
        hwRequirement: {},
        description: 'hw_enrollment管理页面',
        headers: {},
        fileList: [],
        form: this.$form.createForm(this),
        title: '操作',
        bodyStyle: {
          padding: '0',
          height: (window.innerHeight * 0.66) + 'px',
          'overflow-y': 'auto'
        },
        modelStyle: {
          width: '80%',
          style: { top: '20px' },
          fullScreen: false
        },
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        accept: '.pdf',
        confirmLoading: false,
        validatorRules: {},
        selectedRowKeys: [],
        selectedRows: [],
        // 表头
        columns: [
          {
            title: '姓名',
            align: 'left',
            dataIndex: 'hwUser.name'
          },
          {
            title: '手机号',
            align: 'left',
            dataIndex: 'hwUser.phone'
          },
          {
            title: '身份证号',
            align: 'left',
            dataIndex: 'hwUser.idcardNumber'
          },
          {
            title: '类型',
            align: 'left',
            dataIndex: 'hwUser.type',
            customRender: function(value) {
              if (value == 0) {
                return '自然人'
              } else if (value == 1) {
                return '个体工商户'
              }
            }
          },
          {
            title: '订单状态',
            align: 'left',
            dataIndex: 'status_dictText',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'left',
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: '/requiremanager/hwEnrollment/list',
          delete: '/requiremanager/hwEnrollment/delete',
          deleteBatch: '/requiremanager/hwEnrollment/deleteBatch',
          exportXlsUrl: '/requiremanager/hwEnrollment/exportXls',
          importExcelUrl: 'requiremanager/hwRequirement/importExcel?id=',
          getColumns: '/requiremanager/hwEnrollment/list?requirementId='
        },
        dictOptions: {}
      }
    },


    computed: {},
    methods: {
      close() {
        this.$emit('ok')
        this.visible = false
      },
      edit(record) {
        this.form.resetFields()
        var record1 = qs.parse(record)
        this.code = record1.id
        this.code1 = record1.companyName
        this.code2 = record1.createTime
        this.code3 = record1.code
        this.requirestatus = record1.authenticationStatus
        this.requireison = record1.isOn
        this.visible = true
        this.loadData()

      },
      handleOk() {
        this.$emit('ok')
        this.visible = false
      },
      handleCancel() {
        this.close()
      },
      importExcelUrl(record) {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}` + record.id
      },

      handleImportExcel1(code) {
        // this.record1=record1
        // this.record1.id=record1.hwRequirement.id
        this.$refs.modalSelectRequireForm.edit(code)
        this.$refs.modalSelectRequireForm.title = '导入报名'
        this.$refs.modalSelectRequireForm.disableSubmit = false
      },
      loadData() {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        var params = this.getQueryParams()//查询条件
        params.requirementId = this.code
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      //生成订单
      generateOrder(record) {
        let that = this
        if (record.hwRequirement.isOn == 0) {
          that.$error({
            title: '错误',
            content: '需求已下架，无法生成订单',
            okText: '确定'
          })
        }  else {
          const modal = that.$confirm({
            title: '生成订单',
            closable: true,
            keyboard: false,
            maskClosable: true,
            content: '确定将当前选中的项生成订单吗',
            okText: '确定',
            cancelText: '取消',
            onOk: function() {
              that.loading = true;
              generateOrder({
                enrollId: record.id,
                requirementId: record.hwRequirement.id,
                userId: record.hwUser.id
              }).then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.selectedRowKeys = [];
                  that.selectedRows = [];
                  // 刷新参数放到这里去触发，就可以刷新相同界面了
                  that.loadData()
                } else {
                  that.$message.warning(res.message)
                }
                that.loading = false;
              })
            },
            onCancel() {
              modal.destroy()
            }
          })
        }
      },
      modalFormOk() {
        this.loadData()
      },
      //监听checkbox属性
      getCheckboxProps(record) {
        var b=false;
        if(record.orderNumber !=null){
          b=true;
        }
        if(record.hwRequirement.isOn == 0){
          b=true;
        }
        return {
          props: {
            disabled: b,
          }
        }
      },
      onSelectChange (selectedRowKeys,selectedRows) {
        this.selectedRowKeys = selectedRowKeys;
        this.selectedRows = selectedRows;
      },
      generateorderMany(){
        if(this.selectedRowKeys.length==0){
          this.$message.warning('请先选择报名')
          return
        }
        var list=[]
        for(var i=0;i<this.selectedRowKeys.length;i++){
          list.push(this.selectedRowKeys[i])
        }
        let that = this
        const modal = that.$confirm({
          title: '生成订单',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '确定将当前选中的项生成订单吗',
          okText: '确定',
          cancelText: '取消',
          onOk: function() {
            that.loading = true;
            var param={ids:list}
            generateOrderMany(
              {ids:list}
            ).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.selectedRowKeys = [];
                that.selectedRows = [];
                // 刷新参数放到这里去触发，就可以刷新相同界面了
                that.loadData()
              } else {
                that.$message.warning(res.message)
              }
              that.loading = false
            })
          },
          onCancel() {
            modal.destroy()
          }
        })
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
<style lang="scss" scoped>
  .title {
    color: rgba(0, 0, 0, .85);
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 16px;
  }

  .detail-layout div{
    //margin-left: 44px;
    width: auto;
    ::v-deep .term{
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #000000;
    }
    ::v-deep .content{
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.65);
    }
    &:last-child{
      margin-left: 12px;
    }
  }

  .ant-row {
    display: flex;
    flex-direction: column;
  }

  .text {
    color: rgba(0, 0, 0, .45);
  }

  .heading {
    color: rgba(0, 0, 0, .85);
    font-size: 20px;
  }

  .step-item {
    min-height: 148px !important;
  }

  .brightPoints1 {
    display: -webkit-box;
    /* -webkit-box-orient: vertical; /
    /! autoprefixer: ignore next */
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }

  .content {
    margin: 0 !important;
  }
  ::v-deep .page-header .detail .main .title{
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
  }
  ::v-deep .ant-table-pagination{
    float: none;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .table{
    padding: 0 15px;
    box-sizing: border-box;
  }
</style>